I made a simple dropdown menu which opens when clicked, and closes when the user clicks anywhere outside the menu. The following is the codebase:
function deactivateAllDropdownTriggers() {
   document.querySelectorAll('.dropdown-trigger.active').forEach(function(elem) {
        elem.classList.remove('active');
    }) 
}
function handleDropdownClicks(event) {
    if (event.target.matches('.dropdown-trigger')) {
        if (event.target.classList.contains('active')) {
            event.target.classList.remove('active');
        } else {
            deactivateAllDropdownTriggers();
            event.target.classList.add('active');
        }
    } else {
        if (!event.target.matches('.dropdown-menu *')) {
            deactivateAllDropdownTriggers();
        }
    }
}
document.addEventListener('click', handleDropdownClicks, false);.dropdown {
    display: inline-block;
    position: relative;
}
.dropdown .dropdown-menu {
    display: none;
    position: absolute;
    -moz-box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
    -webkit-box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
    width: auto;
    border: 1px solid rgba(0, 0, 0, 0.15);
    border-radius: var(--border-radius);
    background-color: #ffffff;
    z-index: 1;
}
.dropdown .dropdown-menu ul {
    padding: 0;
    margin: 0;
}
.dropdown .dropdown-menu ul > li {
    list-style: none;
    margin: 0;
}
.dropdown .dropdown-menu ul > li.dropdown-menu-content {
    padding: 0.6rem 1.2rem;
    white-space: nowrap;
}
.dropdown .dropdown-menu ul > li.dropdown-menu-divider {
    height: 1px;
    background-color: rgba(0, 0, 0, 0.05);
}
.dropdown .dropdown-menu ul > li > a {
    display: block;
    text-decoration: none;
    padding: 0.6rem 1.2rem;
    color: rgba(0, 0, 0, 0.8);
    cursor: pointer;
    white-space: nowrap;
    min-width: 12rem;
}
.dropdown .dropdown-menu ul > li > a:hover {
    background-color: rgba(0, 0, 0, 0.025);
}
.dropdown .dropdown-trigger.on-click.active + .dropdown-menu {
    display: block;
}<div class="dropdown">
    <button class="dropdown-trigger on-click">Pick your weapon</button>
    <div class="dropdown-menu">
        <ul>
            <li class="dropdown-menu-content">
                Weapons
            </li>
            <li class="dropdown-menu-divider"></li>
            <li><a href="#">Sword</a></li>
            <li><a href="#">Lance</a></li>
            <li><a href="#">Axe</a></li>
            <li><a href="#">Bow</a></li>
        </ul>
    </div>
</div>
<div class="dropdown">
    <button class="dropdown-trigger on-click">Pick your class</button>
    <div class="dropdown-menu">
        <ul>
            <li class="dropdown-menu-content">
                Classes
            </li>
            <li class="dropdown-menu-divider"></li>
            <li><a href="#">Fighter</a></li>
            <li><a href="#">Archer</a></li>
            <li><a href="#">Thief</a></li>
            <li><a href="#">Ninja</a></li>
        </ul>
    </div>
</div>One thing that concerns me is the open event listener. I could not create a system where an event listener is added to the document when a menu is opened, and the event listener is removed when the menu is closed. Perhaps one of you could help me with that.
Anyway, would appreciate a review of this code.
