Sub topic 1
What accessible components must do
An accessible component works for people who use a keyboard, a screen reader, or other assistive technology. This chapter explains the three duties every interactive component has: expose the right role, manage focus, and respond to the keyboard. Later chapters show how headless libraries handle these for you.
Prerequisites
This chapter assumes you have:
- Working knowledge of React function components, props, and hooks.
- Basic HTML knowledge, including form elements and attributes.
- A browser with developer tools, so you can inspect the accessibility tree.
Roles, names, and states
Assistive technology does not read pixels. It reads the accessibility tree, a structure the browser builds from your DOM. Each node in that tree has a role, a name, and a state.
- Role: what the element is, such as
button,dialog, orcheckbox. - Name: the label announced to the user, such as "Delete item".
- State: the current condition, such as
aria-expanded="true"oraria-checked="false".
Native HTML elements supply all three for free. A <button> has the button role, takes its name from its text, and is focusable by default. A <div onClick> has none of these.
// Inaccessible: no role, not focusable, ignores Enter and Space
export function BadButton() {
return <div onClick={() => alert("Saved")}>Save</div>
}
// Accessible: the browser supplies role, focus, and key handling
export function GoodButton() {
return <button onClick={() => alert("Saved")}>Save</button>
}Use ARIA attributes only when no native element fits. ARIA changes what assistive technology is told. It does not add behavior, so you must still write the keyboard handling yourself.
Focus management
Focus is the element that currently receives keyboard input. Components must keep it predictable.
- Every interactive element must be reachable with
Tab, and the focus indicator must be visible. - When a dialog opens, focus moves inside it.
- While the dialog is open, focus is trapped there, so
Tabcycles through its contents only. - When the dialog closes, focus returns to the element that opened it.
Skipping the last step is a common bug. The user closes the dialog and lands at the top of the page.
Keyboard interaction
Users expect the same keys to work across components. The WAI-ARIA Authoring Practices Guide documents these patterns.
| Component | Expected keys |
|---|---|
| Button | `Enter` and `Space` activate it |
| Dialog | `Escape` closes it, `Tab` stays inside |
| Menu | Arrow keys move between items, `Escape` closes |
| Tabs | Arrow keys switch tabs, `Tab` moves into the panel |
| Checkbox | `Space` toggles it |
Custom components must implement these keys. This is the work that makes building a select, menu, or dialog from scratch expensive.
How the pieces fit together
Common errors
- Using
divorspanfor clickable elements instead ofbuttonora. - Removing the focus outline with
outline: noneand giving no replacement. - Icon-only buttons with no accessible name. Add an
aria-labelor visually hidden text. - Opening a modal without moving or trapping focus.
- Using ARIA roles that contradict the element, such as
role="button"on a link that navigates.
Why this matters for the rest of the tutorial
Getting roles, focus, and keys right in every component is repetitive and easy to get wrong. Headless component libraries such as Radix UI implement these behaviors once. The next chapter shows how they separate that behavior from styling, so you only write the visuals.
Exercise
A teammate builds a "Delete" control as <div className="btn" onClick={remove}>Delete</div>. Name three accessibility problems and the simplest fix.
Show answer
The div has no button role, so screen readers do not announce it as a control. It is not focusable, so keyboard users cannot reach it with Tab. It does not respond to Enter or Space. The simplest fix is to use <button type="button" onClick={remove}>Delete</button>, which provides the role, focusability, and key handling natively.
Exercise
A user closes a modal with Escape, and keyboard focus jumps to the top of the page. Which duty was skipped, and what should happen instead?
Show answer
Focus restoration was skipped. When the dialog closes, focus should return to the element that opened it, so the user continues from where they were.