Sub topic 15
Making the app accessible
This chapter assumes you have the to-do app from the earlier chapters: a controlled form, a keyed list, and TodoItem components that receive callbacks through props. You should know basic HTML forms and how React renders JSX. No assistive technology experience is needed.
Accessibility (often shortened to a11y) means people can use your app with a keyboard, a screen reader, voice control, or zoom. A screen reader is software that reads the page aloud or sends it to a braille display. Most fixes cost little if you make them while building. They cost much more to retrofit.
How assistive technology sees your app
Browsers build an accessibility tree from the DOM. Each node has a role (button, checkbox, list), a name (the text a user hears), and a state (checked, disabled). Screen readers and testing tools read this tree, not your pixels.
Native HTML elements fill in the tree for you. A <button> has the role button, can be focused with Tab, and fires on Enter and Space. A <div onClick> has none of these. Use the right element first, and reach for ARIA attributes only when no native element fits.
Use semantic elements
The app already uses several. Check each one against this table.
| Need | Use | Avoid |
|---|---|---|
| Page title | `<h1>` | `<div class="title">` |
| Main content region | `<main>` | `<div id="app">` |
| List of tasks | `<ul>` with `<li>` | Repeated `<div>`s |
| Submit a form | `<button type="submit">` | `<span onClick>` |
| Done state | `<input type="checkbox">` | A styled `<div>` |
| Delete action | `<button type="button">` | A clickable icon with no role |
Lists matter because a screen reader announces "list, 3 items" and lets the user jump between items. If you replace <ul> with <div>s, that information is lost.
Label every form control
A control needs a name. The to-do form wraps the input in a <label>, which gives the input the name "New task". Clicking the label text also focuses the input, which enlarges the click target.
A placeholder is not a label. It disappears when the user types, and many screen readers do not treat it as a reliable name. If your design has no visible label, hide it visually but keep it in the DOM.
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
margin: -1px;
padding: 0;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}In React, the for attribute is written htmlFor. When the label and input are not nested, connect them with an id. Use useId so ids stay unique if a component renders more than once.
import { useId } from "react";
function NewTaskField({ value, onChange }) {
const id = useId();
return (
<div>
<label htmlFor={id} className="visually-hidden">
New task
</label>
<input
id={id}
value={value}
onChange={(e) => onChange(e.target.value)}
placeholder="What needs doing?"
/>
</div>
);
}Give buttons names that make sense out of context
Screen reader users often list every button on the page. Ten buttons all named "Delete" are indistinguishable. Add the task title to the accessible name with aria-label.
function TodoItem({ todo, onToggle, onDelete }) {
return (
<li>
<label>
<input
type="checkbox"
checked={todo.done}
onChange={() => onToggle(todo.id)}
/>
<span style={{ textDecoration: todo.done ? "line-through" : "none" }}>
{todo.title}
</span>
</label>
<button
type="button"
onClick={() => onDelete(todo.id)}
aria-label={`Delete ${todo.title}`}
>
Delete
</button>
</li>
);
}Now the button is announced as "Delete Buy milk, button". The visible text "Delete" is contained in the accessible name, so voice control users can still say "click Delete".
If a button shows only an icon, such as a trash can, aria-label is the only source of its name. Mark the icon itself aria-hidden="true" so it is not read twice.
Do not rely on style alone
The strikethrough on completed tasks is only visual. The checkbox already exposes the done state, so a screen reader user hears "checked". That is why a real checkbox beats a styled <span>.
Color has the same limit. If you mark errors only in red, color-blind users miss them. Add text or an icon. Keep text contrast at least 4.5:1 against its background for normal-size text, which is the WCAG AA level (WCAG is the Web Content Accessibility Guidelines).
Keyboard support and focus
Everything must work with Tab, Shift+Tab, Enter, and Space. Test this by unplugging your mouse for five minutes. Common failures:
- Removing the focus outline with
outline: noneand nothing in its place. - Click handlers on non-interactive elements.
- A positive
tabIndex, which forces an odd tab order. Use0or-1only.
If you hide the outline, replace it:
button:focus-visible,
input:focus-visible {
outline: 3px solid #1a5fd0;
outline-offset: 2px;
}:focus-visible shows the ring for keyboard users and usually hides it for mouse clicks.
Moving focus after a change
When an element disappears, focus is lost and falls back to the page body. A keyboard user then has to tab from the top. After deleting a task, send focus somewhere sensible. A simple choice is the new-task input, held in a ref.
import { useRef, useState } from "react";
export default function App() {
const [todos, setTodos] = useState([]);
const [text, setText] = useState("");
const inputRef = useRef(null);
function addTodo(event) {
event.preventDefault();
const title = text.trim();
if (!title) return;
setTodos([...todos, { id: crypto.randomUUID(), title, done: false }]);
setText("");
inputRef.current.focus();
}
function deleteTodo(id) {
setTodos(todos.filter((t) => t.id !== id));
inputRef.current.focus();
}
return (
<main>
<h1>To-do</h1>
<form onSubmit={addTodo}>
<label>
New task
<input
ref={inputRef}
value={text}
onChange={(e) => setText(e.target.value)}
/>
</label>
<button type="submit">Add</button>
</form>
<ul>
{todos.map((todo) => (
<li key={todo.id}>
{todo.title}
<button
type="button"
onClick={() => deleteTodo(todo.id)}
aria-label={`Delete ${todo.title}`}
>
Delete
</button>
</li>
))}
</ul>
</main>
);
}The same idea applies to inline editing. When the user presses Edit, focus the edit field. When they save or cancel, return focus to the Edit button.
Announce changes with live regions
A sighted user sees the list grow after pressing Add. A screen reader user hears nothing unless you announce it. A live region is an element whose text changes are read aloud automatically. Use role="status" for polite updates. The region must exist in the DOM before its text changes.
function RemainingCount({ remaining }) {
return (
<p role="status">
{remaining} {remaining === 1 ? "task" : "tasks"} left
</p>
);
}When the count changes, the screen reader reads the new text without moving focus. Avoid aria-live="assertive" for routine updates, because it interrupts whatever the user was hearing.
Filter buttons and state
The filter buttons from the derived-data chapter change what the list shows. Expose which one is active with aria-pressed.
function FilterBar({ filter, onChange }) {
const options = ["all", "active", "completed"];
return (
<div role="group" aria-label="Filter tasks">
{options.map((name) => (
<button
key={name}
type="button"
aria-pressed={filter === name}
onClick={() => onChange(name)}
>
{name}
</button>
))}
</div>
);
}React renders aria-pressed={true} as the string "true", which is what ARIA expects. The role="group" with a label gives the three buttons a shared name.
Common mistakes
- Using
<div onClick>instead of<button>. You then have to rebuild focus, key handling, and roles by hand. - Putting
aria-labelon a plain<div>or<span>. Most screen readers ignore names on elements with no role. - Adding ARIA that contradicts native meaning, such as
role="button"on a link. - Skipping the heading hierarchy. Use one
<h1>, then<h2>for sections, without jumping levels for style. - Treating automated checks as complete. Tools like axe catch roughly a third of issues. Keyboard and screen reader testing catch the rest.
Checking your work
- Press Tab through the whole app and confirm the order and the visible focus ring.
- Turn on a screen reader (VoiceOver on macOS, NVDA on Windows) and add, toggle, and delete a task.
- Run the axe browser extension or Lighthouse accessibility audit.
- Add the
eslint-plugin-jsx-a11yrules to your linter. Vite's React templates use ESLint, and this plugin flags missing labels and click handlers on non-interactive elements as you type.
Exercise
A teammate replaces the Delete button with <span onClick={() => onDelete(todo.id)}>🗑</span>. Name three accessibility problems this causes and how to fix them.
Show answer
The span has no role, so a screen reader does not announce it as a control. It is not focusable, so keyboard users cannot reach it, and Enter and Space do nothing. The emoji has no reliable accessible name. Fix it by using <button type="button" onClick={...} aria-label={Delete ${todo.title}}> and marking the emoji aria-hidden="true" inside it.
Exercise
After the user adds a task, the screen reader says nothing and the count text "2 tasks left" changes on screen. What single change makes the new count announced, and why must the element already exist before the text changes?
Show answer
Add role="status" to the element that holds the count. Screen readers register live regions when the page loads, then watch them for changes. If the element is created at the same moment as its text, there is no earlier registration and the text may not be announced.