Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 18.4 — Accessible Interactive Components
React · Example 3 of 4

Menu button: arrows, Escape and focus return

Enter and Space on the button already open the menu, because they fire a click on a real <button>. Arrow Up from the button opens on the last item; Escape returns focus to the button; Enter on an item runs it and closes. Letter keys (type-ahead) are left out to keep the example short: add them in Try it.

Dialogs, disclosures, tabs, menu buttons and comboboxes each have a standard keyboard pattern in the WAI-ARIA Authoring Practices; build them with the right roles, states and keys, prefer native elements where they exist, and reach for a tested headless library for the hard ones.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write an accessible Accordion from three disclosures where opening one closes the others.

02

Add Home and End support to the menu button example.

Explain it without notes

01

Describe the keyboard interaction of the WAI-ARIA tabs pattern and how roving tabindex implements it in React.

02

When would you use a disclosure, a menu button and a combobox? Give a native alternative for each where one exists.

03

Why does a combobox use aria-activedescendant instead of moving focus into the list?

04

What does a headless component library give you, and name two.

Menu button: arrows, Escape and focus return

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

ArrowDown  → focus: Rename
ArrowDown  → focus: Duplicate
ArrowUp    → focus: Rename
ArrowUp    → focus: Delete
Escape     → focus: Actions ▾
ArrowUp    → focus: Delete
chose Delete
Enter      → focus: Actions ▾