Sub topic 12
Click, hover, and Escape behavior in mega menus
This chapter assumes you can write basic HTML, CSS, and JavaScript event handlers. It also assumes you know the disclosure pattern from the previous chapters: a button with aria-expanded and aria-controls that shows or hides a panel.
Click is the baseline
A mega menu trigger is a button. Clicking it, or pressing Enter or Space while it has focus, toggles the panel. Native buttons already fire click for both keys, so one listener covers mouse, keyboard, and touch.
The script keeps two things in sync: the aria-expanded value and the panel's hidden property. If they drift apart, screen reader users hear the wrong state.
const button = document.querySelector('[aria-controls="products-panel"]');
const panel = document.getElementById('products-panel');
function setOpen(open) {
button.setAttribute('aria-expanded', String(open));
panel.hidden = !open;
}
button.addEventListener('click', () => {
setOpen(button.getAttribute('aria-expanded') !== 'true');
});Escape closes and returns focus
Pressing Escape should close an open panel. Focus must then go back to the trigger button. Without that step, focus sits on an element that has just become hidden, and keyboard users lose their place.
document.addEventListener('keydown', (event) => {
if (event.key === 'Escape' && button.getAttribute('aria-expanded') === 'true') {
setOpen(false);
button.focus();
}
});The listener is on document so Escape works whether focus is on the button or on a link inside the panel.
Hover is an optional extra
Many sites open menus on hover. If you add it, treat it as an enhancement and never as the only way in. Touch screens have no hover. Keyboard users cannot hover at all.
Hover has some known problems:
- A pointer that drifts slightly outside the panel closes it, which is frustrating.
- Opening on hover and toggling on click can conflict. A user hovers (the panel opens), then clicks, and the click closes it.
- Users with tremors or low precision find hover menus hard to control.
If you keep hover, add a short close delay of a few hundred milliseconds. Make sure click still toggles reliably. A simpler choice is click only, which behaves the same for every input type.
const item = button.parentElement;
let closeTimer;
item.addEventListener('mouseenter', () => {
clearTimeout(closeTimer);
setOpen(true);
});
item.addEventListener('mouseleave', () => {
closeTimer = setTimeout(() => setOpen(false), 250);
});Here the listeners sit on the list item that contains both the button and the panel. Moving the pointer from the button into the panel therefore does not count as leaving.
Summary of behaviors
| Input | Expected result |
|---|---|
| Click, Enter, or Space on the button | Toggle the panel and update `aria-expanded` |
| Escape while the panel is open | Close the panel and focus the button |
| Pointer hover (optional) | Open with a close delay, never required |
| Tab out of the panel | Optionally close it so it does not stay open behind other content |
Exercise
A keyboard user opens a mega menu, tabs to a link inside the panel, and presses Escape. The panel closes, but the next Tab press starts from the top of the page. What was left out of the Escape handler, and why does it matter?
Show answer
The handler did not call button.focus() after closing the panel. The focused link became hidden, so the browser lost the focus position. Returning focus to the trigger button lets the user continue from where they were in the navigation.