Accessible dropdown navigation menus
Dropdown menus that open on :hover only are unusable with a keyboard and unreliable on touch screens. The pattern that works for everyone is a disclosure: each parent item gets a real <button> with aria-expanded that opens and closes its submenu. CSS does the presentation, and a short script toggles the state and closes the menu with Escape.
Problem: The theme’s submenus appear only on mouse hover, keyboard users tab through invisible links, and on phones the first tap on a parent item follows the link instead of opening the submenu.
Solution: Put a <button aria-expanded="false" aria-controls> next to each parent link, show the submenu with CSS when the button’s aria-expanded is true (and on hover for mouse users), toggle it with a delegated click handler, and close it on Escape and when focus leaves the item. Do not use role="menu": it is meant for application menus and changes how screen readers handle the links.
The markup
<nav class="ha-nav" aria-label="Main">
<ul>
<li><a href="/">Home</a></li>
<li class="has-submenu">
<a href="/services/">Services</a>
<button type="button" class="ha-nav__toggle" aria-expanded="false" aria-controls="sub-services">
<span class="screen-reader-text">Services submenu</span>
</button>
<ul id="sub-services" class="ha-nav__sub">
<li><a href="/services/development/">Development</a></li>
<li><a href="/services/support/">Support</a></li>
</ul>
</li>
<li><a href="/contact/">Contact</a></li>
</ul>
</nav>
The parent link still goes to its page, and the separate button opens the submenu. In WordPress the button can be added after each parent item with the walker_nav_menu_start_el filter, checking for the menu-item-has-children class, or in a custom walker (see different walkers for header and footer).
The CSS
.ha-nav .has-submenu {
position: relative;
}
.ha-nav__sub {
position: absolute;
top: 100%;
left: 0;
min-width: 14rem;
background: #fff;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
visibility: hidden;
opacity: 0;
transition: opacity 0.15s, visibility 0s 0.15s;
}
.ha-nav__toggle[aria-expanded="true"] + .ha-nav__sub {
visibility: visible;
opacity: 1;
transition: opacity 0.15s;
}
@media (hover: hover) {
.ha-nav .has-submenu:hover > .ha-nav__sub {
visibility: visible;
opacity: 1;
transition: opacity 0.15s;
}
}
.ha-nav__toggle::after {
content: "";
display: inline-block;
border: 0.3em solid transparent;
border-top-color: currentColor;
}
visibility: hidden removes the links of a closed submenu from the tab order, unlike opacity: 0 alone, and the delay on visibility lets the fade-out finish. The hover rule is limited to devices with a real hover, so a tap on a phone does not open and close the menu at the same time.
The script
document.querySelectorAll( '.ha-nav' ).forEach( ( nav ) => {
const close = ( button ) => button.setAttribute( 'aria-expanded', 'false' );
nav.addEventListener( 'click', ( event ) => {
const button = event.target.closest( '.ha-nav__toggle' );
if ( ! button ) {
return;
}
const open = button.getAttribute( 'aria-expanded' ) !== 'true';
nav.querySelectorAll( '.ha-nav__toggle[aria-expanded="true"]' ).forEach( close );
button.setAttribute( 'aria-expanded', String( open ) );
} );
nav.addEventListener( 'keydown', ( event ) => {
if ( event.key !== 'Escape' ) {
return;
}
const item = event.target.closest( '.has-submenu' );
const button = item && item.querySelector( '.ha-nav__toggle' );
if ( button && button.getAttribute( 'aria-expanded' ) === 'true' ) {
close( button );
button.focus();
}
} );
nav.addEventListener( 'focusout', ( event ) => {
const item = event.target.closest( '.has-submenu' );
if ( item && ! item.contains( event.relatedTarget ) ) {
close( item.querySelector( '.ha-nav__toggle' ) );
}
} );
} );
Escape closes the submenu and returns focus to its button; tabbing out of the item closes it as well. Only one submenu is open at a time.
NOTE: Test with the keyboard alone (Tab, Shift+Tab, Enter, Escape) and with a screen reader: the button must announce “Services submenu, collapsed” and “expanded”. The focus outline of links and buttons must stay visible; the sticky header from position: sticky is a typical place where it gets clipped.