Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 38 ยท Forms and Accessibility

Accessible Widget Patterns: Tabs, Dialogs and Menus

Build keyboard-accessible web component widgets in Lit: roving tabindex tabs, a modal dialog with focus return, a live-region toast and a listbox with aria-activedescendant.

Buttons and inputs are easy to make accessible because the browser does the work. Composite widgets are harder: tabs, menus, listboxes, dialogs. They have no native equivalent (or a native one that is limited), so you implement the behavior from the WAI-ARIA Authoring Practices yourself.

This lesson turns the principles of the previous lesson into four reusable patterns: tabs with a roving tabindex, a modal dialog with focus management, a live-region announcer, and a listbox with aria-activedescendant.

The shape of a composite widget

Every widget needs four things

Roles tablist, tab, tabpanel
States aria-selected, aria-expanded
Keys Arrows, Home, End, Escape
Focus One tab stop, managed inside

Skip any one and the widget fails for someone.

1 / 2

Tabs with a roving tabindex

The pattern: the tab list is one Tab stop. Left and Right arrows move between tabs, Home and End jump to the ends, and only the selected tab has tabindex="0"; all others have -1. Each tab has role="tab", aria-selected and aria-controls; each panel has role="tabpanel" and aria-labelledby.

Accessible tabs

Try it by hand: Tab into the tab list (one stop), use the arrow keys, then press Tab again to jump straight into the panel. Notice how aria-selected, tabindex and hidden are all driven by one property, selected. The pattern here activates the tab as soon as it receives focus ("automatic activation"), which is fine when panels render instantly.

A modal dialog with focus management

For modals, use the native <dialog> element and showModal() whenever possible. The browser then gives you the top layer, a backdrop, Escape to close and inert background content. The spec also moves focus back to the previously focused element when the dialog closes, but you still decide where focus lands first, and returning focus explicitly is a cheap safeguard when the opener is re-rendered or removed.

Dialog that returns focus

Checklist for any dialog: it has an accessible name (aria-labelledby), focus moves inside on open (autofocus on the safest button; for destructive dialogs that is Cancel), Escape closes, and focus ends up back on the opener. Avoid building a modal from a div plus role="dialog" unless you must: you would have to trap focus and make the rest of the page inert yourself.

Announcing changes with a live region

When content changes without moving focus (a toast, "3 results found", a form error), screen readers will not notice unless it is in a live region. role="status" (polite) or role="alert" (assertive) does it.

A status announcer

Two tips: the live region element must already exist in the DOM before its text changes (so render it always, only change the text), and keep announcements short. Use alert only for urgent errors; status is polite and waits for the user.

A listbox with aria-activedescendant

Not every composite widget moves real focus. A combobox or autocomplete keeps focus in the text field so typing continues, and points assistive tech at the highlighted option with aria-activedescendant. Arrow keys change which option is active; Enter selects it.

Listbox with aria-activedescendant

Because the aria-activedescendant target and the listbox are in the same shadow tree, the ID reference resolves. This is another reason to keep related parts of a widget inside one component.

Testing accessibility

  • Keyboard test: put your mouse away and operate every widget. If you ever lose focus or cannot reach something, fix it.
  • Screen reader pass: try NVDA or Narrator on Windows, VoiceOver on macOS and iOS. Listen for names, roles and state changes.
  • Browser devtools: the Accessibility tree pane shows the computed role and name of any node, shadow DOM included.
  • Automated checks (axe-core, Lighthouse) catch missing names and bad contrast but find only a fraction of real problems; see Testing Lit Components.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In a tab list using a roving tabindex, which tabs have tabindex="0"?

    This gives the tab list a single Tab stop; arrow keys then move between tabs.
  2. 2. Which statement about native <dialog>.showModal() is true?

    showModal provides the top layer, backdrop, Escape handling and an inert background. You still give the dialog a name and choose the initial focus.
  3. 3. A live region must...

    Assistive technology watches existing live regions; one inserted together with its text is often missed.
  4. 4. When is aria-activedescendant a good choice?

    Comboboxes keep focus in the input and point at the active option with aria-activedescendant.

Summary

  • Composite widgets need roles, state attributes, keyboard handling and managed focus.
  • Tabs use a roving tabindex and arrow keys; listboxes can use aria-activedescendant.
  • Prefer native <dialog>; manage opener focus and the accessible name.
  • Use live regions (role="status" or alert) for changes that do not move focus.
  • Keep related elements in one shadow tree so ID references resolve.

Next: Testing Basics for Web Components