Sub topic 9
Submenus as disclosure buttons
This chapter assumes you can write semantic HTML and CSS, handle basic JavaScript events, and have read the earlier chapters on nav landmarks and keyboard focus. Here you will build a submenu using the disclosure pattern.
Why a disclosure and not an ARIA menu
A submenu on a website is a list of links that is sometimes hidden. The disclosure pattern fits that case. A button shows or hides a block of content, and the button reports whether the content is open.
ARIA role="menu" is meant for application menus, like the File menu in a desktop editor. It changes how screen readers and keyboards behave, and it requires you to script arrow-key handling yourself. Site navigation does not need it. Plain links in a list, shown and hidden by a button, work with Tab and Enter without extra scripting.
The working model
A disclosure has three parts:
- A
buttonthat controls the panel. - An
aria-expandedattribute on the button. Its value is"true"when the panel is open and"false"when it is closed. - An
aria-controlsattribute that holds theidof the panel.
The panel stays in the page and is hidden with the hidden attribute. A hidden panel is removed from the accessibility tree and from the Tab order, so keyboard users cannot land on invisible links.
<nav aria-label='Primary'>
<ul>
<li>
<button type='button' aria-expanded='false' aria-controls='products-panel'>Products</button>
<div id='products-panel' hidden>
<ul>
<li><a href='/platform/overview'>Overview</a></li>
<li><a href='/platform/security'>Security</a></li>
</ul>
</div>
</li>
<li><a href='/pricing'>Pricing</a></li>
</ul>
</nav>The visible label stays the same whether the panel is open or closed. Screen readers announce the state ("Products, button, collapsed"), so do not change the text to "Open products".
Keeping state in sync
JavaScript has two jobs: toggle the panel, and update aria-expanded to match. Do both in one function so they cannot drift apart.
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');
});
document.addEventListener('keydown', (event) => {
if (event.key === 'Escape' && button.getAttribute('aria-expanded') === 'true') {
setOpen(false);
button.focus();
}
});Escape closes the panel and returns focus to the button, so the keyboard user stays where they were. Clicking a button also fires on Enter and Space, so you do not need separate key handlers for those.
Common errors
- Using a link (
<a href="#">) as the trigger. It announces as a link and navigates, but it acts as a button. Use<button type="button">. - Forgetting to update
aria-expanded. Screen reader users then hear the wrong state. - Hiding the panel with only CSS such as
opacity: 0. The links stay focusable. Usehiddenordisplay: none. - Setting
displayon the panel in CSS, which overrideshidden. Add.panel[hidden] { display: none; }if you do. - Making the parent label both a link and a toggle. Keep them separate, or put the parent page link inside the panel.
Exercise
A submenu button has aria-expanded="false", but the panel is visible on screen because the CSS sets display: flex on it. What is the likely cause, and how do you fix it?
Show answer
The author's display rule overrides the browser's default hidden styling, so the hidden attribute no longer hides the panel. Add a rule such as .panel[hidden] { display: none; }, and make sure the script sets panel.hidden together with aria-expanded.