JSFiddle - React, Tailwind, and code Playground
HTML
<div class="dropdown">
<button class="dropbtn" id="dropdown">Dropdown
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content" id="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
CSS
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: grey;
}
JavaScript
/**
* ugly if-else click listener
* @type {boolean}
*/
let active = false;
document.getElementById('dropdown').addEventListener('click', function() {
if(active) {
document.getElementById('dropdown-content').style.display = 'none';
active = false;
} else {
document.getElementById('dropdown-content').style.display = 'block';
active = true;
}
});