Skip to content

Lesson 29 of 52Accessibility in depth

ARIA Widget Patterns: Tabs, Menus, Comboboxes and Live Regions

Build disclosures, tabs, menu buttons and a combobox in plain JavaScript, wire names and live regions correctly, and avoid the ARIA that breaks pages.

20 min read0/4 questions

An audit comes back on your design system with forty findings, and thirty of them are WCAG 4.1.2 Name, Role, Value. The tabs announce as a row of plain buttons. The account menu says "button" with no hint it opens anything. The search box suggests results that a screen reader user never hears about. Every one of these components looks fine and works with a mouse, which is why nobody noticed. This lesson is about the contract between your markup and assistive technology: how a name is computed, which roles, states and keys each common widget needs, and when to stop building and use a vetted library.

How a control gets its name

Accessibility basics said every control needs a name. Browsers compute it with a fixed algorithm (the W3C "accname" spec), and the order matters because the first source that produces text wins and the rest are ignored.

Accessible name: the first source that gives text wins
  1. aria-labelledbytext of the referenced elements, even if they are hidden
  2. aria-labela string on the element itself
  3. Native labellinglabel for, alt, caption, legend, figcaption
  4. Contentthe text inside, for buttons, links, tabs, options
  5. title or placeholderlast resort, poorly supported

aria-labelledby beats everything. It can point at several ids (aria-labelledby="a b") and joins their text, which is how a 'Delete' button can be named 'Delete Invoice 1042'.

1/5

A description is separate from the name. aria-describedby points at hint text or an error message, and screen readers read it after the name and role, usually after a short pause: "Email, edit text, invalid entry, Enter an email like name@example.com". Names should be short and stable. Descriptions carry the extra detail.

You do not have to guess what the browser computed. In Chrome DevTools, select an element and open the Accessibility pane next to Styles. It shows the computed role, the name and where the name came from, with the losing sources crossed out. Firefox has the same in its Accessibility Inspector.

Predict the outputHTML

What does this print?

<span id="t" hidden>Remove item</span>
<button aria-labelledby="t" aria-label="Close" title="Dismiss">
X
</button>
Need a nudge? Hints walk you through it.

One rule from WCAG ties names to what people see: 2.5.3 Label in Name (A). If a control has visible text, its accessible name must contain that text, ideally at the start. A button that shows "Send" but has aria-label="Submit form" breaks voice control: a user says "click Send" and nothing happens, because the software matches against "Submit form".

The pattern catalogue

You do not invent keyboard behaviour or ARIA wiring. The WAI-ARIA Authoring Practices Guide (APG) at w3.org/WAI/ARIA/apg documents each widget pattern: the roles, the states and properties, and every key. Treat it as the specification for your components. It is not a law and screen reader support varies, so test what you build, but when a reviewer asks "why does Home do that?", the APG is the answer.

Accessible React components built tabs with roving tabindex in React. Here you build the patterns in plain DOM, because that shows exactly which attributes change on each key press, and the same code works in any framework.

Disclosure: the pattern you need most

A disclosure is a button that shows and hides a region: FAQ answers, "Show filters", a mobile nav. It is the simplest pattern and the right answer for far more UI than people expect.

HTML & CSS
<style> .faq button { font: inherit; background: none; border: 0; padding: 8px 0; display: flex; gap: 8px; cursor: pointer; } .faq button:focus-visible { outline: 2px solid #1d4ed8; outline-offset: 2px; } .faq button[aria-expanded="true"] .icon { transform: rotate(90deg); } .icon { display: inline-block; transition: transform 120ms; } .faq p { margin: 0 0 8px 24px; }</style><div class="faq"> <h3><button aria-expanded="false" aria-controls="a1"><span class="icon" aria-hidden="true">&gt;</span>Can I change my plan later?</button></h3> <p id="a1" hidden>Yes. Changes apply from your next billing date.</p> <h3><button aria-expanded="false" aria-controls="a2"><span class="icon" aria-hidden="true">&gt;</span>Do you offer refunds?</button></h3> <p id="a2" hidden>Within 14 days of purchase, no questions asked.</p></div><script> document.querySelectorAll(".faq button").forEach((btn) => { btn.addEventListener("click", () => { const open = btn.getAttribute("aria-expanded") === "true"; btn.setAttribute("aria-expanded", String(!open)); document.getElementById(btn.getAttribute("aria-controls")).hidden = open; }); });</script>

Preview

Four details carry the pattern. It is a real button, so Enter and Space work for free. aria-expanded is the single source of truth, and the CSS rotates the icon from that attribute, so the visual and the announced state can never disagree. The button sits inside a heading so screen reader users can find questions by heading navigation. And the panel uses hidden, which removes it from the accessibility tree as well as from view. For static content, <details> and <summary> give you all of this with no script at all.

Tabs, and automatic versus manual activation

Tabs need three roles: tablist on the container, tab on each tab and tabpanel on each panel. The selected tab has aria-selected="true". Each tab points at its panel with aria-controls, and each panel is named by its tab with aria-labelledby. The tablist is a single Tab stop: arrows move between tabs, Home and End jump to the ends.

The APG describes two activation models. With automatic activation, arrowing to a tab shows its panel at once. With manual activation, arrows only move focus, and Enter or Space shows the panel. Automatic is better when panels render instantly. Manual is better when showing a panel is slow, such as a fetch, because otherwise every arrow press on the way to the fourth tab fires a request.

HTML & CSS
<style> [role="tablist"] { display: flex; gap: 4px; border-bottom: 1px solid #94a3b8; } [role="tab"] { font: inherit; border: 1px solid #94a3b8; border-bottom: 0; background: #f1f5f9; padding: 6px 12px; } [role="tab"][aria-selected="true"] { background: white; font-weight: 700; } [role="tab"]:focus-visible, [role="tabpanel"]:focus-visible { outline: 2px solid #1d4ed8; outline-offset: 2px; } [role="tabpanel"] { padding: 12px 4px; }</style><label><input type="checkbox" id="manual"> Manual activation</label><h3 id="tabs-label">Billing</h3><div role="tablist" aria-labelledby="tabs-label"> <button role="tab" id="t1" aria-controls="p1" aria-selected="true">Invoices</button> <button role="tab" id="t2" aria-controls="p2" aria-selected="false" tabindex="-1">Payment</button> <button role="tab" id="t3" aria-controls="p3" aria-selected="false" tabindex="-1">Usage</button></div><div role="tabpanel" id="p1" aria-labelledby="t1" tabindex="0">Three invoices this year.</div><div role="tabpanel" id="p2" aria-labelledby="t2" tabindex="0" hidden>Visa ending 4242.</div><div role="tabpanel" id="p3" aria-labelledby="t3" tabindex="0" hidden>8,120 API calls this month.</div><p id="log" style="font: 12px monospace; color: #475569;"></p><script> const tabs = [...document.querySelectorAll('[role="tab"]')]; const manual = document.getElementById("manual"); const log = document.getElementById("log"); function select(tab) { tabs.forEach((t) => { const on = t === tab; t.setAttribute("aria-selected", String(on)); t.tabIndex = on ? 0 : -1; document.getElementById(t.getAttribute("aria-controls")).hidden = !on; }); log.textContent = "selected: " + tab.textContent; } tabs.forEach((tab, i) => { tab.addEventListener("click", () => select(tab)); tab.addEventListener("keydown", (e) => { const last = tabs.length - 1; const next = { ArrowRight: i === last ? 0 : i + 1, ArrowLeft: i === 0 ? last : i - 1, Home: 0, End: last }[e.key]; if (next === undefined) return; e.preventDefault(); tabs[next].focus(); if (!manual.checked) select(tabs[next]); else log.textContent = "focused: " + tabs[next].textContent + " (press Enter or Space to show it)"; }); });</script>

Preview

In manual mode, notice that the roving tabindex stays on the selected tab, not the focused one. If the user arrows to "Usage" without activating it and tabs away, Shift+Tab brings them back to the tab whose panel is showing. The panels have tabindex="0" because these have no focusable content inside; if a panel starts with a link or field, leave the panel out of the tab order.

Quick check
Each tab panel loads data from an API when shown. Which activation model fits, and why?
Need a nudge? A hint rules out a wrong answer.

ARIA's menu role means an application menu, like File and Edit in a desktop app. When a screen reader meets role="menu", it switches into a mode where arrow keys go to the widget, and it expects menuitem children, nothing else. Links, headings and paragraphs inside it are not announced normally.

So the account dropdown in your header, which holds "Profile", "Billing" and "Sign out" links, is almost always better as a disclosure that shows a plain list of links. Users can Tab through it and it behaves like the rest of the page. Reach for role="menu" when the items are actions inside an app, such as "Duplicate", "Rename", "Delete" on a file, and you will implement the full keyboard model.

HTML & CSS
<style> .mb { position: relative; display: inline-block; } [role="menu"] { position: absolute; margin: 4px 0 0; padding: 4px; list-style: none; background: white; border: 1px solid #94a3b8; min-width: 160px; } [role="menuitem"] { padding: 6px 10px; cursor: default; } [role="menuitem"]:focus { outline: none; background: #1d4ed8; color: white; } button:focus-visible { outline: 2px solid #1d4ed8; outline-offset: 2px; }</style><div class="mb"> <button id="mbtn" aria-haspopup="menu" aria-expanded="false" aria-controls="menu">File actions</button> <ul role="menu" id="menu" aria-labelledby="mbtn" hidden> <li role="menuitem" tabindex="-1">Duplicate</li> <li role="menuitem" tabindex="-1">Rename</li> <li role="menuitem" tabindex="-1">Delete</li> </ul></div><p role="status" id="did"></p><script> const btn = document.getElementById("mbtn"); const menu = document.getElementById("menu"); const items = [...menu.querySelectorAll('[role="menuitem"]')]; function open(index) { menu.hidden = false; btn.setAttribute("aria-expanded", "true"); items[index].focus(); } function close(returnFocus) { menu.hidden = true; btn.setAttribute("aria-expanded", "false"); if (returnFocus) btn.focus(); } function run(item) { document.getElementById("did").textContent = item.textContent + " chosen"; close(true); } btn.addEventListener("click", () => (menu.hidden ? open(0) : close(false))); btn.addEventListener("keydown", (e) => { if (e.key === "ArrowDown") { e.preventDefault(); open(0); } if (e.key === "ArrowUp") { e.preventDefault(); open(items.length - 1); } }); menu.addEventListener("keydown", (e) => { const i = items.indexOf(document.activeElement); const last = items.length - 1; const moves = { ArrowDown: i === last ? 0 : i + 1, ArrowUp: i === 0 ? last : i - 1, Home: 0, End: last }; if (e.key in moves) { e.preventDefault(); items[moves[e.key]].focus(); } else if (e.key === "Escape") { e.preventDefault(); close(true); } else if (e.key === "Enter" || e.key === " ") { e.preventDefault(); run(items[i]); } else if (e.key === "Tab") close(false); }); items.forEach((item) => item.addEventListener("click", () => run(item)));</script>

Preview

Press Enter on the button: focus lands on the first item. Up arrow on the button opens at the last item. Escape returns focus to the button, Tab closes the menu and lets focus move on. That is a lot of behaviour for three actions, which is exactly why you should not use the pattern for navigation.

Roving tabindex or aria-activedescendant

A composite widget (tabs, menu, listbox, grid, toolbar) is one Tab stop with arrow keys inside. There are two ways to track the active item:

Roving tabindexaria-activedescendant
Where DOM focus isMoves to each itemStays on the container or input
How the item is markedtabindex="0" on it, -1 on the restThe container's attribute holds the item's id
Scrolling into viewFree, because focus movesYou scroll it yourself
Focus styling:focus-visible worksStyle a class or [aria-selected]
Best forTabs, toolbars, menus, radio-like groupsComboboxes, where typing must stay in the input

Roving tabindex is the default choice because real focus is what every screen reader and browser handles most consistently. aria-activedescendant exists for one main reason: in a combobox the user keeps typing while choosing, so focus cannot leave the text field.

Combobox: the hardest common widget

The ARIA 1.2 combobox puts role="combobox" on the input itself. The input has aria-expanded (is the list showing?), aria-controls (the listbox's id), aria-autocomplete="list" and, while an option is highlighted, aria-activedescendant pointing at that option. The popup is a role="listbox" of role="option" items.

Choosing 'Lisbon' with the keyboard
UserInputListboxScreen readertypes 'li'status: 3 resultsArrowDownaria-activedescendant=opt-lima'Lima, 1 of 3'ArrowDown, Enter

The input filters options and sets aria-expanded="true". Focus never leaves the input.

1/6
HTML & CSS
<style> .cb { position: relative; width: 260px; } .cb input { font: inherit; width: 100%; box-sizing: border-box; padding: 6px; } .cb input:focus-visible { outline: 2px solid #1d4ed8; outline-offset: 1px; } [role="listbox"] { position: absolute; left: 0; right: 0; margin: 2px 0 0; padding: 0; list-style: none; border: 1px solid #94a3b8; background: white; max-height: 150px; overflow: auto; } [role="option"] { padding: 6px; cursor: default; } [role="option"][aria-selected="true"] { background: #1d4ed8; color: white; } .sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }</style><label for="city">Destination</label><div class="cb"> <input id="city" type="text" role="combobox" aria-autocomplete="list" aria-expanded="false" aria-controls="cities" autocomplete="off"> <ul id="cities" role="listbox" aria-label="Destinations" hidden></ul></div><div role="status" class="sr" id="count"></div><p id="state" style="font: 12px monospace; color: #475569;"></p><script> const CITIES = ["Lima", "Lisbon", "Lille", "London", "Lyon", "Madrid", "Milan", "Oslo"]; const input = document.getElementById("city"); const list = document.getElementById("cities"); const count = document.getElementById("count"); const state = document.getElementById("state"); let matches = []; let active = -1; function render() { list.innerHTML = ""; matches.forEach((name, i) => { const li = document.createElement("li"); li.id = "opt-" + name.toLowerCase(); li.setAttribute("role", "option"); li.textContent = name; li.setAttribute("aria-selected", String(i === active)); li.addEventListener("mousedown", (e) => e.preventDefault()); li.addEventListener("click", () => choose(i)); list.append(li); }); const open = matches.length > 0; list.hidden = !open; input.setAttribute("aria-expanded", String(open)); if (active >= 0) { input.setAttribute("aria-activedescendant", list.children[active].id); list.children[active].scrollIntoView({ block: "nearest" }); } else input.removeAttribute("aria-activedescendant"); state.textContent = "expanded=" + open + " activedescendant=" + (input.getAttribute("aria-activedescendant") || "none"); } function filter() { const q = input.value.trim().toLowerCase(); matches = q ? CITIES.filter((c) => c.toLowerCase().startsWith(q)) : []; active = -1; render(); count.textContent = q ? matches.length + (matches.length === 1 ? " result" : " results") : ""; } function choose(i) { input.value = matches[i]; matches = []; active = -1; render(); } input.addEventListener("input", filter); input.addEventListener("keydown", (e) => { if (e.key === "ArrowDown") { e.preventDefault(); if (!matches.length) { matches = CITIES.slice(); count.textContent = matches.length + " results"; } active = (active + 1) % matches.length; render(); } else if (e.key === "ArrowUp" && matches.length) { e.preventDefault(); active = active <= 0 ? matches.length - 1 : active - 1; render(); } else if (e.key === "Enter" && active >= 0) { e.preventDefault(); choose(active); } else if (e.key === "Escape") { if (matches.length) { matches = []; active = -1; render(); } else input.value = ""; } }); input.addEventListener("blur", () => { matches = []; active = -1; render(); });</script>

Preview

Type "l", then use the arrows and watch the state line: aria-activedescendant changes while focus stays in the input. Three details are easy to miss. The options call preventDefault() on mousedown, so clicking one does not blur the input first and close the list before the click lands. The code scrolls the active option into view itself, because no real focus move happens. And the result count goes into a status region that exists from page load.

This demo still skips things a production combobox needs: debounced async results with a loading state, a "no results" message, grouping, right-to-left languages, IME composition for Chinese and Japanese input, and testing on VoiceOver for iOS, where aria-activedescendant support has historically been weaker than on desktop. That list is why teams use React Aria, Base UI, Radix or Headless UI for comboboxes, date pickers and nested menus, and build disclosures and tabs themselves.

Live regions

Some changes happen away from focus: "Saved", "3 results", "Upload failed". A live region is an element whose content changes are announced.

  • aria-live="polite", or role="status": announced when the screen reader finishes what it is saying. Use it for almost everything.
  • aria-live="assertive", or role="alert": interrupts. Keep it for errors that need attention now. Overusing it is like shouting every sentence.

The rule that breaks most implementations: the region must already be in the accessibility tree before its content changes. Screen readers watch existing regions for mutations. Inserting a brand new <div role="status">Saved</div> is often not announced, because the region and its text arrive together. Render an empty region at page load and change its text. To repeat an identical message ("Saved" twice), clear it first and set it again after a short delay, or append a changing detail.

HTML & CSS
<style>.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }</style><button id="save">Save draft</button><button id="fail">Simulate error</button><div role="status" class="sr" id="polite"></div><div role="alert" class="sr" id="rude"></div><p>What a screen reader would hear:</p><ol id="heard" style="font: 13px monospace;"></ol><script> const heard = document.getElementById("heard"); const mo = new MutationObserver((records) => { for (const r of records) { const el = r.target.nodeType === 1 ? r.target : r.target.parentElement; if (el.textContent) heard.insertAdjacentHTML("beforeend", "<li>[" + (el.id === "rude" ? "assertive" : "polite") + "] " + el.textContent + "</li>"); } }); ["polite", "rude"].forEach((id) => mo.observe(document.getElementById(id), { childList: true, characterData: true, subtree: true })); let n = 0; document.getElementById("save").addEventListener("click", () => { const region = document.getElementById("polite"); region.textContent = ""; setTimeout(() => { region.textContent = "Draft saved (" + (++n) + ")"; }, 100); }); document.getElementById("fail").addEventListener("click", () => { document.getElementById("rude").textContent = "Error: connection lost. Your changes are not saved."; });</script>

Preview

The list here is a simulation built with a MutationObserver, which is roughly how screen readers notice changes. For the real thing, turn on VoiceOver (Cmd+F5) or NVDA and press the buttons. Also avoid wrapping a big, frequently updating area such as a chat log or ticker in aria-live. Announcing every change makes the page unusable; announce a short summary instead.

Quick check
After a form saves, your code runs container.append(statusDiv), where statusDiv is a new div with role='status' and the text 'Saved'. Testers say NVDA often stays silent. What is the most reliable fix?
Need a nudge? A hint rules out a wrong answer.

States and the attributes that hide things

A handful of state attributes do most of the work in real apps:

  • aria-pressed="true|false" makes a toggle button ("Bold", "Mute"). Do not change the label as well; "Mute, toggle button, pressed" already says it all.
  • aria-current="page" marks the current link in navigation (also step, date, location).
  • aria-invalid="true" on a field with an error, plus aria-describedby pointing at the message.
  • aria-busy="true" on a region while it updates, so assistive tech can wait for the final content.
  • aria-hidden="true" removes an element and all its children from the accessibility tree. It does not remove them from the tab order, so a focusable element inside lands the user on something with no name and no role. To hide a whole region from everyone, including keyboard users, use the inert attribute.
Match the pairs0/6 matched

Match each piece of markup to the problem it causes.

Choose an item, then its match on the other side.
Key terms
accessible name
The text assistive tech uses to identify a control, computed from aria-labelledby, aria-label, native labels or content, in that order.
APG
The WAI-ARIA Authoring Practices Guide: the W3C's reference for roles, states and keys of each widget pattern.
disclosure
A button with aria-expanded that shows and hides a related region.
composite widget
A widget such as tabs, a menu or a listbox that is one Tab stop with arrow keys inside.
aria-activedescendant
An attribute on the focused element pointing at the id of the item that is active, without moving DOM focus.
live region
An element whose content changes are announced, set with aria-live, role='status' or role='alert'.
inert
An HTML attribute that makes a subtree unfocusable, unclickable and hidden from assistive tech.