Sub topic 13
Mega menu accessibility pitfalls
This chapter assumes you have built the disclosure-button mega menu from the earlier chapters: a button with aria-expanded and aria-controls, a panel with the hidden attribute, and a click handler. The mistakes below are the ones that most often break that pattern.
Pitfall 1: Using menu roles
Developers often add role="menu" and role="menuitem" to a mega menu because it is called a "menu". Those roles belong to application-style menus, such as a right-click menu. They switch screen readers into a mode that expects arrow-key navigation, and a list of links does not support that mode. Keep the plain nav, ul, and a markup, and use the button's aria-expanded to report state.
Pitfall 2: Hover as the only trigger
A panel that opens only on mouseover is unreachable by keyboard and by touch. Click or Enter on the button must always work. Hover can be added as an extra for mouse users, but it must never be the only way to open the panel.
WCAG 1.4.13 (Content on Hover or Focus) adds three rules for content that appears on hover:
- The user can dismiss it, usually with Escape, without moving the pointer.
- The pointer can move onto the panel without it closing, so there should be no dead gap between the trigger and the panel.
- The panel stays open until the user dismisses it or moves away.
Pitfall 3: Focus that goes nowhere
Escape should close the panel and return focus to the button, as in the earlier script. A second case is a panel that stays open while the user tabs past its last link. Keyboard users then land on page content with an open panel covering it. Close the panel when focus leaves the menu item:
const button = document.querySelector('[aria-controls="products-panel"]');
const panel = document.getElementById('products-panel');
const item = button.closest('li');
function setOpen(open) {
button.setAttribute('aria-expanded', String(open));
panel.hidden = !open;
}
item.addEventListener('focusout', (event) => {
// relatedTarget is the element receiving focus, or null if focus left the page.
if (!item.contains(event.relatedTarget)) {
setOpen(false);
}
});Do not trap focus inside the panel. A mega menu is not a modal dialog, and the user must be able to tab out of it.
Pitfall 4: State that drifts out of sync
If you toggle visibility in CSS or with a class but forget aria-expanded, a screen reader announces "collapsed" for a visible panel. Put the update in one function, like setOpen above, so the attribute and the hidden property always change together. Also keep the .mega-panel[hidden] { display: none; } rule, because display: grid overrides the hidden attribute and leaves closed panels visible and focusable.
Pitfall 5: Small screens and zoom
A wide multi-column panel can overflow at 400% zoom or on a phone. Collapse the grid to one column, give touch targets at least 44 by 44 CSS pixels, and make sure the panel scrolls with the page instead of sitting in a fixed-height box that clips links.
Quick reference
| Pitfall | Symptom | Fix |
|---|---|---|
| `role="menu"` on links | Screen reader expects arrow keys | Use plain `nav` and lists |
| Hover-only opening | Keyboard and touch users blocked | Button toggles on click |
| No Escape or focus return | Focus is lost after closing | Close and call `button.focus()` |
| Panel stays open after tabbing out | Panel covers page content | Close on `focusout` |
| Missing `[hidden]` rule | Closed panel still visible | Add `display: none` for `[hidden]` |
Exercise
A mega menu panel opens when the user clicks the button, and Escape closes it. A keyboard user tabs through the last link in the panel, and the panel stays open over the page content. Which event should you handle, and how do you decide whether to close the panel?
Show answer
Handle focusout on the menu item that contains the button and the panel. Check event.relatedTarget. If the element receiving focus is not inside the item (item.contains(event.relatedTarget) is false), call setOpen(false).