Sub topic 15
Making the app accessible
This chapter assumes you have the working app from the earlier chapters: a controlled form, a TodoItem component, filter state, and a remaining count. It also assumes you know basic HTML form elements. Accessibility here means people can use the app with a keyboard, a screen reader, or other assistive tools.
Start with the right elements
Native elements come with keyboard support and screen reader meaning built in. A <button> is focusable, responds to Enter and Space, and is announced as a button. A <div onClick> does none of that. Use <button>, <input>, <form>, <ul>, and <li> and you get most of the work for free.
ARIA attributes (extra attributes that describe roles and states to assistive tools) fill the gaps that native elements leave.
Label every control
Your add form already wraps the input in a <label>. That works. An explicit htmlFor and id pair does the same and lets you style the label separately. In React the attribute is htmlFor, not for.
Icon-only or terse buttons need a name that makes sense out of context. Ten buttons that all say "Delete" are hard to tell apart in a screen reader's list of buttons. Include the task title in the name:
<button
type="button"
aria-label={`Delete ${todo.title}`}
onClick={() => onDelete(todo.id)}
>
Delete
</button>The aria-label replaces the visible text as the accessible name. Keep the visible word inside it, so the name still matches what sighted users see.
Announce changes
A screen reader user cannot see that the remaining count changed. A live region is an element whose text changes are announced automatically. role="status" makes a polite one:
<p role="status">{remaining} {remaining === 1 ? "task" : "tasks"} remaining</p>Render the element on first load and change only its text. Live regions that are added to the page and filled at the same moment are often not announced.
Expose the filter state
Color or a CSS class alone does not tell a screen reader which filter is active. aria-pressed does:
function Filters({ 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>
);
}Manage focus
After a submit, the keyboard user expects to keep typing. Use a ref (a stored reference to the DOM element) to return focus to the input:
import { useRef, useState } from "react";
export function AddForm({ onAdd }) {
const [text, setText] = useState("");
const inputRef = useRef(null);
function handleSubmit(event) {
event.preventDefault();
const title = text.trim();
if (!title) return;
onAdd(title);
setText("");
inputRef.current.focus();
}
return (
<form onSubmit={handleSubmit}>
<label htmlFor="new-task">New task</label>
<input
id="new-task"
ref={inputRef}
value={text}
onChange={(e) => setText(e.target.value)}
/>
<button type="submit">Add</button>
</form>
);
}Common errors
| Mistake | Effect | Fix |
|---|---|---|
| Placeholder used instead of a label | The name disappears while typing and some tools ignore it | Add a real `<label>` |
| `onClick` on a `<div>` or `<span>` | No keyboard access | Use a `<button>` |
| Removing the focus outline in CSS | Keyboard users lose their position | Keep it or restyle it with `:focus-visible` |
| Status shown only by color or strikethrough | Invisible to screen readers | The checkbox's `checked` state already conveys it, so keep the real checkbox |
Test with the keyboard alone: Tab through the page, press Space on checkboxes, and press Enter on buttons. Every action should work, in a sensible order. A browser audit tool such as Lighthouse or the axe extension catches many of the remaining problems.
Exercise
Why does aria-label={Delete ${todo.title}} improve on a plain "Delete" button in a list of tasks, and what must you keep in mind when you choose the label text?
Show answer
A screen reader user often browses a list of all buttons on the page, and many identical "Delete" entries give no hint which task each one removes. Including the title makes each name unique. The label should still contain the visible text ("Delete") so that voice-control users who say what they see can activate the button.