Dropdown Menu
Dropdown menu with keyboard navigation and ARIA management.
Dropdown Menu
Section titled “Dropdown Menu”Use CSS classes with your own JavaScript for toggle behaviour. You manage ARIA attributes and keyboard navigation:
<div style="position: relative;">
<button
class="af-btn"
aria-haspopup="menu"
aria-expanded="false"
onclick="toggleDropdown(this)"
>
Menu
</button>
<ul
class="af-dropdown-menu"
role="menu"
hidden
id="dropdown-menu"
>
<li>
<a href="#" role="menuitem">
Item 1
</a>
</li>
<li>
<a href="#" role="menuitem">
Item 2
</a>
</li>
<li>
<a href="#" role="menuitem">
Item 3
</a>
</li>
</ul>
</div>
<script>
function toggleDropdown(button) { const menu = document.getElementById('dropdown-menu'); const isOpen = !menu.hidden; menu.hidden = isOpen; button.setAttribute('aria-expanded', !isOpen); }
</script>CSS classes
Section titled “CSS classes”.af-dropdown-menu- Dropdown menu container.af-dropdown-menu-open- Show menu (alternative to removinghidden)[role="menu"]- ARIA role for menu pattern
Required ARIA
Section titled “Required ARIA”role="menu"on the<ul>elementrole="menuitem"on each<a>or<button>insidearia-haspopup="menu"on trigger buttonaria-expandedon trigger button (true/false)hiddenattribute to show/hide menu
Keyboard navigation (you must implement)
Section titled “Keyboard navigation (you must implement)”- Arrow keys to navigate items
- Enter/Space to activate
- Escape to close
- Home/End to jump to first/last item