Sub topic 11
Making step changes accessible
This chapter assumes you have the flow model from earlier chapters: a FlowState with current, history, data and errors, plus advance and back functions. It also assumes you can read basic HTML and know what ARIA attributes are. The code here is framework-neutral TypeScript that works against the DOM.
Why step changes need attention
In a guided flow, "next" does not load a new page. The script swaps the content of a container. A sighted user sees the change. A screen reader user hears nothing, because no page load happened and no announcement was made. Keyboard users have a related problem. If the button they just pressed disappears, focus falls back to the top of the document.
Three things need handling on every transition:
- Focus moves to a sensible place in the new step.
- Assistive technology learns that the step and its progress changed.
- Errors are reported and reachable, not only shown in red.
Move focus to the step heading
After rendering a new step, move focus to its heading. Headings are not interactive, so give the heading tabindex="-1". This lets script focus it without adding it to the Tab order.
function showStep(container: HTMLElement, title: string, progress: string): void {
container.innerHTML =
'<h2 id="step-heading" tabindex="-1"></h2>' +
'<p id="step-progress"></p>';
const heading = container.querySelector<HTMLElement>("#step-heading")!;
const status = container.querySelector<HTMLElement>("#step-progress")!;
heading.textContent = title;
status.textContent = progress;
heading.focus();
}A screen reader reads the focused heading, so the user hears the new step's title. The same function works for Back, because going back is also a step change. Using textContent instead of building HTML strings from data avoids injecting markup from user input.
Do not focus the first input by default. The user would hear the field label without the step title, and so would not know that the step changed.
Announce progress with a live region
Focus alone can miss context such as "Step 3". Use a live region, an element that screen readers watch and read aloud when its text changes. Create it once and keep it in the page. Do not recreate it on every step, because a newly inserted live region is often not announced.
const live = document.createElement("div");
live.setAttribute("role", "status");
live.setAttribute("aria-live", "polite");
live.className = "visually-hidden";
document.body.appendChild(live);
function announce(message: string): void {
live.textContent = "";
window.setTimeout(() => {
live.textContent = message;
}, 50);
}Clearing the text first, then setting it after a short delay, makes repeated identical messages get read again. Pair this with the progressLabel function from the progress chapter, so the message is "Step 2" and not an invented "Step 2 of 5" that branching could make wrong.
The visually-hidden class should hide the element visually but keep it available to assistive technology. Do not use display: none, which removes it from the accessibility tree.
Make errors reachable
When advance returns errors, the step does not change. The user needs to learn why. A common pattern is an error summary at the top of the step, plus a message next to each field.
function showErrors(errors: string[]): void {
const summary = document.getElementById("error-summary")!;
summary.setAttribute("role", "alert");
summary.tabIndex = -1;
summary.innerHTML = "";
const list = document.createElement("ul");
for (const message of errors) {
const item = document.createElement("li");
item.textContent = message;
list.appendChild(item);
}
summary.appendChild(list);
summary.focus();
}
function markInvalid(input: HTMLInputElement, messageId: string): void {
input.setAttribute("aria-invalid", "true");
input.setAttribute("aria-describedby", messageId);
}Focusing the summary makes the screen reader read the list. Setting aria-describedby means the message is read again when the user reaches the field. In the Step type from this tutorial, validate returns plain strings, so tying each message to a field requires either returning field ids with each message or keeping one field per message. Plan this when you design validation, since it is awkward to add later.
Common errors
- Focus lost after Next. The old button is removed and focus resets. Always set focus explicitly after rendering.
- Color-only errors. Red borders alone fail users who cannot see color. Include text, and optionally an icon.
- Placeholder as label. Placeholders vanish while typing. Use a real
<label>for every field. - Back does not restore focus. Treat Back like Next and focus the heading.
- Announcing everything. Using
aria-live="assertive"for routine progress interrupts the user. Reserverole="alert"for errors.
Exercise
After the user presses Next and the new step renders, which element should receive focus, and why is the first input a poor default?
Show answer
The new step's heading, given tabindex="-1" so script can focus it. The screen reader then announces the step title. Focusing the first input announces only the field label, so the user may not realize the step changed.
Exercise
Why should the live region be created once and kept in the page instead of being rendered inside each step?
Show answer
A live region inserted into the page at the same moment as its content is often not announced. A region that already exists and has its text changed is announced reliably.