Sub topic 10
Making state changes accessible
This chapter assumes you can read basic HTML and TypeScript. It also assumes you know how a screen moves between loading, empty, error, and success states. Here you will learn how people who use screen readers, keyboards, or magnification find out that a state changed.
Why state changes are an accessibility problem
A sighted mouse user sees a spinner appear, a list fill in, or a red banner show up. A screen reader user gets none of that unless you tell the browser what happened. A screen reader is software that reads the page aloud or sends it to a braille display. It follows the user's focus or reading position. It does not watch the whole page.
State changes break this model in three ways:
- Content changes somewhere the user is not looking, and nothing announces it.
- Content is replaced, so the element that had keyboard focus disappears.
- A state is shown only by color or an icon, so some users cannot tell it apart from the others.
Each of the four states (loading, empty, error, success) needs a plan for all three. The tools are live regions, the aria-busy attribute, focus management, and text that does not depend on color.
Live regions
A live region is an element that the browser watches. When its content changes, assistive technology announces the new content without moving the user's focus. You mark one with an ARIA attribute or a role that implies one.
Two settings matter most:
aria-live="polite"waits until the user is idle, then announces. Use it for information that is useful but not urgent.aria-live="assertive"interrupts whatever is being read. Use it only for problems the user must hear now.
Some roles carry these settings by default:
| Role | Implied behavior | Use it for |
|---|---|---|
| `status` | Polite live region | Loading messages, "Saved", result counts |
| `alert` | Assertive live region | Errors that block progress |
| `log` | Polite, keeps history | Chat or activity feeds |
The example below shows the three pieces most state screens need.
<div role="status" aria-live="polite">Loading projects…</div>
<ul aria-busy="true">
<li class="skeleton-row"></li>
<li class="skeleton-row"></li>
</ul>
<div role="alert">
Couldn't save your changes. Check your connection and try again.
</div>The status element announces the loading message politely. The aria-busy="true" attribute tells assistive technology that the list is still updating, so it should wait and not read half-built content. The alert element announces the error at once.
The live region must exist before the change
This is the most common mistake. Browsers announce changes to a live region. They often do not announce a live region that is inserted into the page already holding its text. If you render <div role="alert">Error</div> as part of a new view, some screen readers stay silent.
The reliable pattern is to keep an empty region in the page from the start and then change its text.
<div id="announcer" role="status" aria-live="polite"></div>function announce(message: string): void {
const region = document.getElementById("announcer");
if (!region) return;
// Clear first so repeating the same message is announced again.
region.textContent = "";
window.setTimeout(() => {
region.textContent = message;
}, 50);
}
announce("12 projects loaded.");The short delay after clearing matters. Setting the same text twice in one tick can look like no change, so the second announcement is lost.
role="alert" is the exception that often works when inserted, because it is widely implemented that way. Do not rely on it for everything. Use it sparingly, and test with a real screen reader.
Announcing loading
Loading needs care because a screen reader user cannot see a spinner. You have three goals: say that loading began, avoid noise while it continues, and say what happened when it ends.
- Announce the start once, in a polite region: "Loading projects."
- Set
aria-busy="true"on the region being filled. Remove it when the content is complete. - Announce the result when it ends: "12 projects loaded." or "No projects found."
Do not announce progress every second. A progress bar that speaks "10 percent, 11 percent, 12 percent" makes the page unusable. For a determinate progress bar, use the progressbar role with aria-valuenow, aria-valuemin, and aria-valuemax. Screen readers report those values on request and at sensible intervals. Add an aria-label so the bar has a name.
<div
role="progressbar"
aria-label="Uploading report.pdf"
aria-valuemin="0"
aria-valuemax="100"
aria-valuenow="40"
></div>For an indeterminate wait (you do not know how long it will take), omit aria-valuenow. The role then tells users that work is in progress without inventing a number.
Skeletons and spinners
A skeleton screen is a gray placeholder shaped like the content to come. Skeleton rows are decorative. Mark them so they are not read as empty list items, and give the real information to the status region.
<ul aria-busy="true" aria-hidden="true">
<li class="skeleton-row"></li>
<li class="skeleton-row"></li>
</ul>
<p role="status">Loading projects…</p>A spinner drawn as an image or SVG needs the same treatment. Hide the graphic with aria-hidden="true" and put the words in a status region. Keep the text visible too, or use a visually hidden class, so the information is not missing for anyone.
Motion
Spinners and shimmer animations can cause discomfort for people with vestibular conditions. Respect the user's operating system setting.
@media (prefers-reduced-motion: reduce) {
.skeleton-row {
animation: none;
}
.spinner {
animation-duration: 3s;
}
}Replacing a shimmer with a static gray block keeps the meaning. A slower, steadier spinner is still a clear signal that work is happening.
Announcing errors
Errors are where announcement choices have the highest cost. If the user does not hear the error, they may think the action worked.
Choose the mechanism by error type:
| Situation | Mechanism |
|---|---|
| A save or request failed, and the user is still on the same screen | `role="alert"` region, plus keep focus where it is |
| A form was submitted with invalid fields | Error summary at the top, focus moved to it, and each field linked to its message |
| A whole page failed to load | Move focus to the error heading |
| A background refresh failed | Polite `status` message, not an alert |
Use alert for what blocks the user. If every minor issue interrupts, people learn to tune it out.
Linking errors to fields
For a form field error, connect the message to the input so it is read when the field gets focus.
<label for="email">Email</label>
<input
id="email"
type="email"
aria-invalid="true"
aria-describedby="email-error"
/>
<p id="email-error">Enter an email address like [email protected].</p>aria-invalid="true" marks the field as having a problem. aria-describedby makes the screen reader read the message after the field's label. Set aria-invalid only after validation fails, not on a fresh form.
Do not rely on color alone
A red border is not enough. About 1 in 12 men has some form of color vision deficiency, and low vision or a bright screen can wash out color for anyone. Pair color with at least one of these:
- An icon with a text equivalent
- A text prefix such as "Error:"
- A distinct shape or border style
Keep text contrast at or above 4.5:1 against its background for normal-size text, which is the WCAG 2.2 AA minimum.
Announcing empty states
An empty state can look like nothing happened. If a user types a search and the list goes blank, a screen reader user hears silence. Announce the result.
function onSearchResults(items: string[]): void {
if (items.length === 0) {
announce("No results. Try a different search term.");
} else {
announce(`${items.length} results found.`);
}
}The visible empty state should also be real text in the page, with a heading if it fills the main area. A heading lets screen reader users jump to it. An illustration should carry empty alt text (alt="") when it is decorative, so it is skipped.
If the empty state includes a call to action such as "Create your first project", make it a real button or a, so it can be reached with the keyboard.
Managing focus
Focus is the element that receives keyboard input. When state changes remove the focused element, focus falls back to the page body and the user loses their place. This is the second major failure after missing announcements.
Common cases and the fix for each:
- A "Retry" button is activated and the error view is replaced by a loading view. Move focus to the loading region or keep the button in place and disable it with
aria-disabled="true". Do not let it vanish. - A list finishes loading after a navigation. Move focus to the page's main heading, so the user starts reading from the top.
- An item is deleted and the list becomes empty. Move focus to the empty state heading or its call to action, not to nowhere.
- A dialog shows an error. Keep focus inside the dialog. Do not move it behind the dialog.
To move focus to a non-interactive element such as a heading, give it tabindex="-1". That makes it focusable by script without adding it to the tab order.
function focusHeading(id: string): void {
const heading = document.getElementById(id);
if (!heading) return;
heading.setAttribute("tabindex", "-1");
heading.focus();
}
focusHeading("projects-heading");Move focus only when the user's action caused the change, or when their current focus has disappeared. Do not steal focus from someone who is typing in a field because a background refresh finished.
Prefer `aria-disabled` over `disabled` for pending buttons
A button with the disabled attribute leaves the tab order, and its focus is lost. During a save, aria-disabled="true" keeps focus on the button. Your script must then ignore clicks while pending. Add a text change such as "Saving…", so the state is clear to everyone.
Connecting this to your state model
If you modeled state as a discriminated union, you can make each branch responsible for its own accessibility behavior. One function that maps state to an announcement keeps messages consistent and testable.
type AsyncState<T> =
| { status: "loading" }
| { status: "error"; message: string; retryable: boolean }
| { status: "success"; data: T };
function announcementFor(state: AsyncState<string[]>): string {
switch (state.status) {
case "loading":
return "Loading projects.";
case "error":
return state.message;
case "success":
return state.data.length === 0
? "No projects yet."
: `${state.data.length} projects loaded.`;
}
}Because the switch covers every case, adding a new status makes the compiler flag the missing announcement. The announcement cannot be forgotten.
The announcement flow
The diagram shows what should happen from the moment a request starts to the moment the user knows its result.
Common mistakes
- Using
assertivefor everything. It interrupts the user and teaches them to ignore announcements. - Inserting the live region with its text. The announcement may never be spoken. Render the region first.
- Announcing both the status and the content. Reading "Loading" and then reading every skeleton row is noise.
- Leaving
aria-busy="true"on. A forgotten flag can leave the region silent forever. Clear it in both success and error branches. - Removing the focused element. Always decide where focus goes next.
- Color-only errors. Add text and an icon.
- Testing only with a mouse. Many failures show up only with a keyboard or screen reader.
Quick checklist
- Is there a live region present in the page before state changes?
- Is loading announced once, and is the result announced when it ends?
- Does every error reach the user through
alert, a summary, or a field link? - Does the empty state have real text and a reachable action?
- Where does focus go after each state change?
- Is color never the only signal?
- Does reduced motion turn off the animations?
Exercise
A user searches a project list. The request succeeds but returns no items. The old results disappear, and the page shows the text "No projects match your search." in a new element added to the page. A screen reader user reports hearing nothing. Give two likely causes and one fix for each.
Show answer
Cause 1: the message was inserted as a new element, so no live region existed beforehand to detect a change. Fix: keep an empty role="status" region in the page and set its text to the message. Cause 2: the focused element (such as the old first result) was removed, so focus fell back to the page body and the user's position was lost. Fix: move focus to the empty-state heading (give it tabindex="-1") or return it to the search field, and announce the result count in the status region.
Exercise
A "Save" button uses the disabled attribute while saving. Keyboard users say they lose their place after pressing it. Explain why, and describe a safer approach.
Show answer
A button with disabled is removed from the tab order and cannot hold focus, so focus drops to the page body. Use aria-disabled="true" instead, which keeps focus on the button, ignore clicks in script while the request is pending, and change the label to "Saving…". Announce the outcome in a status region, or in an alert region if the save fails.
Exercise
You show a skeleton list with a status message "Loading projects…". The request fails, and you show an error inside the same list container, but the container still has aria-busy="true". What can go wrong, and what should the error branch do?
Show answer
Some screen readers skip content in a busy region, so the error may never be read. The error branch should remove aria-busy (or set it to "false"), announce the failure in a role="alert" region, and keep focus on or move it to a Retry control so keyboard users can act.