Sub topic 11
Making step changes accessible
A guided flow replaces part of the screen each time the user presses Next or Back. Sighted mouse users see the change at once. Keyboard users and screen reader users may not. Their focus can stay on a button that no longer exists, or the new content can appear without any signal. This chapter covers focus, announcements, and error reporting for step changes.
This chapter assumes you have read the earlier chapters on flow state, navigation, validation, and error messaging. It reuses FlowState, advance, and back as they were defined there. You should know basic DOM APIs and what a screen reader does at a high level. The code is framework-neutral, so you can apply it in any UI library.
What goes wrong without it
Consider a flow that swaps the contents of a container when the user clicks Next. Several things can happen:
- The Next button is removed from the DOM. Keyboard focus falls back to the top of the page, and the user must tab through the navigation again.
- The button stays in place, but its text changes. A screen reader user hears nothing and does not know the step changed.
- Validation fails and error text appears above the fold. A user who cannot see it does not know the submit failed.
- The browser tab title still reads "Sign up" on every step, so history and window lists give no hint about where the user is.
Each failure comes from the same cause. The user took an action, the page changed, and nothing told assistive technology where to look.
Decide where focus goes
Each transition in the flow has one right answer for focus. Decide it once, in code that does not depend on rendering, and apply it after every render.
| Event | Where focus goes | Why |
|---|---|---|
| Advance succeeds | Heading of the new step | The user starts reading from the top of the new content. |
| Back | Heading of the previous step | Same reason. Do not restore focus to the old Next button. |
| Advance fails validation | Error summary at the top of the step | The user learns what failed and can jump to each field. |
| Initial load | Normal page order | Do not steal focus from the page. |
The decision depends only on the previous and next FlowState. That makes it a pure function, so you can unit test it.
type FocusTarget = "heading" | "summary" | "none";
type FlowState = {
current: string;
history: string[];
data: Record<string, string>;
errors: string[];
};
function focusTargetFor(prev: FlowState, next: FlowState): FocusTarget {
if (next.current !== prev.current) return "heading";
if (next.errors.length > 0) return "summary";
return "none";
}The last step needs a note. When advance is called on the final step, next returns null, so the step does not change and no errors exist. The result is "none". In a real flow, submission is the action that follows, and it has its own focus rule, which is covered later.
Move focus to the step heading
A heading is not focusable by default. To focus it from script, give it tabindex="-1". This makes it focusable programmatically but keeps it out of the Tab order. Moving focus to the heading also makes screen readers read it aloud, so the heading serves as the announcement of the new step.
The same function should update document.title. A distinct title per step helps screen reader users who check the title, and everyone who has several tabs open.
function enterStep(heading: HTMLElement, stepLabel: string, flowName: string): void {
document.title = stepLabel + " - " + flowName;
heading.setAttribute("tabindex", "-1");
heading.focus();
}Call it after the new step is in the DOM. If you call it before the browser has inserted the element, focus() does nothing, and nothing reports the failure. In frameworks, this usually means running it in a post-render hook rather than in the click handler.
You do not need to remove the focus outline from the heading. Browsers show an outline on programmatic focus of non-interactive elements only in some cases, and users who rely on a visible focus position benefit when it shows. If a design review asks you to hide it, push back.
Announce progress without double speech
The previous chapter on progress indication recommended a label such as "Step 3" without a fixed total. Place that label inside the heading, for example "Step 3: Company details". Then focus on the heading announces the step number and the topic in one sentence.
Some teams also add a live region, an element with aria-live="polite" whose text changes are read aloud. This works, but combined with focus movement it can cause the screen reader to say the same thing twice, or to cut one announcement off with the other. Choose one mechanism:
- Use focus on the heading as the default. It works in every screen reader.
- Use a live region only when focus cannot move, for example when the step content updates in place and the user must stay on a control.
Report errors where users can find them
When validation fails, the user needs three things: to know that it failed, to see what failed, and to get to the field quickly. An error summary plus inline messages covers all three.
- The summary sits at the top of the step. It has a heading, such as "There is a problem", and a list of links, one per error.
- Each link points to the id of the field with the problem, so activating it moves focus to that field.
- Each field gets an inline message next to it, linked with
aria-describedby, and is marked witharia-invalid="true".
The earlier validate function returned string[]. To link messages to fields, return objects that carry the field id. This version extends that return type, and the rest of the flow code is unchanged otherwise.
type FieldError = { fieldId: string; message: string };
function renderErrors(summary: HTMLElement, errors: FieldError[]): void {
document.querySelectorAll(".field-error").forEach((el) => el.remove());
document.querySelectorAll("[aria-invalid]").forEach((el) => {
el.removeAttribute("aria-invalid");
el.removeAttribute("aria-describedby");
});
summary.replaceChildren();
summary.hidden = errors.length === 0;
if (errors.length === 0) return;
const title = document.createElement("h2");
title.textContent = "There is a problem";
const list = document.createElement("ul");
for (const error of errors) {
const field = document.getElementById(error.fieldId);
if (!field) continue;
const messageId = error.fieldId + "-error";
const inline = document.createElement("p");
inline.id = messageId;
inline.className = "field-error";
inline.textContent = "Error: " + error.message;
field.insertAdjacentElement("afterend", inline);
field.setAttribute("aria-invalid", "true");
field.setAttribute("aria-describedby", messageId);
const item = document.createElement("li");
const link = document.createElement("a");
link.href = "#" + error.fieldId;
link.textContent = error.message;
item.append(link);
list.append(item);
}
summary.append(title, list);
summary.setAttribute("tabindex", "-1");
summary.focus();
}A few details in this code matter:
- The function clears old errors first. Without that, a corrected field keeps its
aria-invalidflag and a stale message. - The inline message starts with "Error:" so the meaning does not depend on red text alone.
- The summary is focused after it is filled. Focusing an empty element gives the screen reader nothing to read.
aria-describedbyhere replaces any existing value. If a field also has a hint, such as "Use your work address", set it to both ids separated by a space, with the hint first.
Wire the pieces together
One function runs after each state change and connects the pure decision to the DOM work. The names steps, advance, and back come from earlier chapters.
function afterTransition(
prev: FlowState,
next: FlowState,
elements: { heading: HTMLElement; summary: HTMLElement },
labels: Record<string, string>,
): void {
const target = focusTargetFor(prev, next);
if (target === "heading") {
renderErrors(elements.summary, []);
const label = labels[next.current] ?? "Step " + (next.history.length + 1);
elements.heading.textContent = label;
enterStep(elements.heading, label, "Sign up");
}
// For "summary", renderErrors has already moved focus.
// For "none", leave focus alone.
}In this version, the code that renders errors for a failed advance is called from the same place that calls advance, using the errors in next.errors mapped to FieldError objects. The key point is the order: render the step, then run this function once.
Keyboard and control details
Several smaller points affect step changes:
- Use real
<button>elements for Next and Back. A clickabledivdoes not respond to Enter or Space and is not announced as a button. - Keep Next and Back in the same place in the DOM on every step. Users learn the pattern, and the Tab order stays predictable.
- Label Back clearly, and do not hide it on the first step if layout depends on it. Instead, disable it with the
disabledattribute only when you also explain why, or remove it from the DOM. A disabled button drops out of the Tab order, which is often fine here. - When a branch skips a step, do not render a step the user will never see. A hidden step that still takes focus confuses screen reader users.
- Pressing Enter in a text field should trigger the same action as Next. A real
<form>with a submit button does this for free.
Browser history is a separate matter. If each step updates the URL, the browser Back button changes steps. In that case, run the same focus logic when the popstate event fires, and keep the URL and history stack from earlier chapters consistent. If you do not use URLs, users who press the browser Back button leave the flow, and you may want a beforeunload warning when data is unsaved.
Focus after submission
Submission replaces the last step with a result, such as a confirmation or a server error. Treat it like any other step change. On success, focus the confirmation heading. On failure, render the problem in the summary and focus it. Also disable the submit button while the request runs, and set aria-busy="true" on the container so assistive technology knows content is pending. Remove it when the response arrives, then move focus.
Common mistakes
- Focus left on the removed button, because the code never moved it after a render.
- Calling
focus()before the element exists in the DOM. - Using color alone to mark invalid fields. Pair color with text and an icon, and keep sufficient contrast.
- Replacing the whole form on a validation error, which clears the user's input and focus. Update only the error elements.
- Auto-focusing the first field on every step. It can skip the heading, so the user never hears the step title. Focus the heading, and let the user press Tab to reach the first field.
- Testing only with a mouse. A single pass with the keyboard alone, then one with a screen reader such as NVDA, VoiceOver, or TalkBack, catches most of these problems.
Exercise
A user presses Next on a step with an empty required field. Validation fails and the step stays the same. Using the focus rules in this chapter, what does focusTargetFor return, what element should receive focus, and why is the step heading the wrong choice?
Show answer
It returns "summary", because next.current equals prev.current and next.errors is not empty. The error summary should receive focus. The heading is wrong because the user did not move to a new step, so reading the title again tells them nothing about the failure. The summary tells them what failed and offers links to each field.
Exercise
Your flow uses a live region that announces "Step 3" and also moves focus to a heading reading "Step 3: Company details". A screen reader user reports hearing the step announced twice, sometimes cut off. What should you change?
Show answer
Remove the live region and rely on focus moving to the heading. The heading already contains the step number and the topic, so the live region duplicates it. Keep a live region only for cases where focus cannot move.