Skip to content

Announcing Changes

The Course
•
15 min study

A sighted user sees the count change as they type, and notices "Saved" appear. Without an announcement, a screen reader user typing in the text box may not notice changes elsewhere. The count and the saved message need a way to be discovered without moving focus.

Changes nobody hears React · 6 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; import "./liveLog.js"; createRoot(document.getElementById("root")).render(<App />); import { useState } from "react"; import { topics } from "./topics.js"; const save = () => new Promise((resolve) => setTimeout(resolve, 600)); export default function App() { const [query, setQuery] = useState(""); const [status, setStatus] = useState(""); const matches = topics.filter((t) => t.toLowerCase().includes(query.trim().toLowerCase())); async function saveFilter() { setStatus("Saving…"); await save(); setStatus(`Saved "${query}" as a favourite filter.`); } return ( <main> <h1>Topics</h1> <div className="toolbar"> <input aria-label="Filter topics" value={query} onChange={(e) => setQuery(e.target.value)} /> <button onClick={saveFilter} disabled={query === ""}>Save filter</button> </div> <p className="muted">{matches.length} topics</p> <p className="muted">{status}</p> <ul className="cards"> {matches.map((t) => <li key={t} className="card">{t}</li>)} </ul> </main> ); } // Stands in for a screen reader: logs each change to a live region, the way a // screen reader would announce it. Elements with aria-live, role="status" or // role="alert" count as live regions. Like many screen readers, it ignores a // region that appears with its text already inside. const LIVE = '[aria-live]:not([aria-live="off"]), [role="status"], [role="alert"]'; new MutationObserver((records) => { const changed = new Set(); for (const record of records) { const node = record.target.nodeType === Node.TEXT_NODE ? record.target.parentElement : record.target; const region = node?.closest(LIVE); if (region) changed.add(region); } for (const region of changed) { const text = region.textContent.trim(); if (text) console.log(`announced: ${text}`); } }).observe(document.body, { subtree: true, childList: true, characterData: true }); export const topics = [ "Compound components", "Controlled props", "Render props", "Imperative handles", "External stores", "Selectors", "State machines", "Suspense", "Request cache", "Lazy loading", "Profiling", "Windowing", "Activity", "Dialogs", "Comboboxes", ]; import { useEffect, useState } from "react"; // Returns value, but only after it has stopped changing for `delay` ms. export function useDebouncedValue(value, delay) { const [debounced, setDebounced] = useState(value); useEffect(() => { const id = setTimeout(() => setDebounced(value), delay); return () => clearTimeout(id); }, [value, delay]); return debounced; } body { font: 15px/1.5 system-ui, sans-serif; margin: 0; background: #f4f6f9; color: #1c2430; } main { max-width: 56rem; margin: 1rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.5rem; font-size: 1.4rem; } input, select, button, textarea { font: inherit; } input, select, textarea { padding: 0.3rem 0.5rem; border: 1px solid #c4ccd6; border-radius: 6px; } button { padding: 0.25rem 0.7rem; border: 1px solid #c4ccd6; border-radius: 6px; background: white; cursor: pointer; } .toolbar { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin-bottom: 0.75rem; } .board { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0.75rem; } .column { background: #e9edf2; border-radius: 10px; padding: 0.6rem; min-height: 6rem; } .column h2 { margin: 0 0 0.5rem; font-size: 0.95rem; } .count { color: #5c6878; font-weight: normal; } .cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.4rem; } .card { background: white; border: 1px solid #d6dde6; border-radius: 8px; padding: 0.45rem 0.6rem; } .card button.title { border: 0; padding: 0; background: none; text-align: left; font-weight: 600; } .card.selected { outline: 2px solid #087ea4; } .muted { color: #5c6878; font-size: 0.85rem; } .panel { margin-top: 0.75rem; background: white; border: 1px solid #d6dde6; border-radius: 10px; padding: 0.75rem 1rem; } .panel h2 { margin: 0 0 0.25rem; font-size: 1.05rem; } .error { color: #b42318; } [role="tablist"] { display: flex; gap: 0.25rem; border-bottom: 1px solid #d6dde6; margin-bottom: 0.75rem; } [role="tab"] { border: 0; border-bottom: 2px solid transparent; border-radius: 0; background: none; padding: 0.4rem 0.8rem; } [role="tab"][aria-selected="true"] { border-bottom-color: #087ea4; color: #06617f; font-weight: 600; } .stars button { border: 0; background: none; padding: 0 0.1rem; font-size: 1.3rem; color: #b9c2cd; } .stars button[aria-checked="true"], .stars button.on { color: #d99a00; } dialog { border: 1px solid #d6dde6; border-radius: 12px; padding: 1rem 1.25rem; width: min(28rem, 90vw); } dialog::backdrop { background: rgb(22 35 45 / 0.45); } [role="listbox"] { list-style: none; margin: 0.25rem 0 0; padding: 0.25rem; border: 1px solid #d6dde6; border-radius: 8px; max-height: 12rem; overflow-y: auto; } [role="option"] { padding: 0.3rem 0.5rem; border-radius: 6px; cursor: pointer; } [role="option"][data-active="true"] { background: #e6f7fc; outline: 2px solid #087ea4; } [role="toolbar"] { display: flex; gap: 0.25rem; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.

src/liveLog.js stands in for a screen reader. It watches the page and logs announced: followed by the text whenever a live region changes. Type in the filter and press Save filter: the console stays empty, because nothing on this page is a live region yet.

Live regions

A live region is an element whose changes a screen reader announces, without moving focus there:

JSX
<p className="muted" aria-live="polite">{matches.length} topics</p>
<p className="muted" role="status">{status}</p>
  • aria-live="polite" announces changes when the screen reader has finished what it's saying.
  • role="status" is a ready-made polite live region, for status messages.
  • aria-live="assertive", or role="alert", interrupts. Use it only for urgent problems, such as a failed save.

Add both attributes to the lab, then type st and press Save filter.

Check what you see

The console now logs a line for each change: announced: 11 topics after s, announced: 3 topics after t, then announced: Saving… and announced: Saved "st" as a favourite filter.

The logger is a rough model. If you have a real screen reader, such as VoiceOver on a Mac or NVDA on Windows, try it too: it may merge or skip quick changes. Your browser's developer tools also have an Accessibility pane, which shows each element's role and whether it's live.

The region must already exist

Screen readers watch live regions for changes. A region that appears with its text already in it is often not announced at all. So this doesn't work reliably:

JSX
{status && <p role="status">{status}</p>}

Render the region always, empty when there's nothing to say, and change only its text, as the lab does. The lab's logger behaves the same way: change the status paragraph to the version above and save a filter, and Saving… is never logged.

Don't overdo it

Announce what users need to know and can't otherwise find out: results counts, saved or failed, errors after submitting. Announcing every keystroke's count is borderline. Some apps wait until typing pauses, with a debounced value, before updating the region.

Exercise

Announce only after a pause

Keep the visible count instant, but update the announced count only when typing has stopped for 500 ms. The lab includes src/useDebouncedValue.js, the hook from Intermediate Day 5: it returns its value only after the value has stopped changing for the delay you give it.

Check it in the console: typing st quickly should log one announced: 3 topics, half a second after you stop, not one line per letter.

Your working space React · 6 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; import "./liveLog.js"; createRoot(document.getElementById("root")).render(<App />); import { useState } from "react"; import { topics } from "./topics.js"; const save = () => new Promise((resolve) => setTimeout(resolve, 600)); export default function App() { const [query, setQuery] = useState(""); const [status, setStatus] = useState(""); const matches = topics.filter((t) => t.toLowerCase().includes(query.trim().toLowerCase())); async function saveFilter() { setStatus("Saving…"); await save(); setStatus(`Saved "${query}" as a favourite filter.`); } return ( <main> <h1>Topics</h1> <div className="toolbar"> <input aria-label="Filter topics" value={query} onChange={(e) => setQuery(e.target.value)} /> <button onClick={saveFilter} disabled={query === ""}>Save filter</button> </div> <p className="muted">{matches.length} topics</p> <p className="muted">{status}</p> <ul className="cards"> {matches.map((t) => <li key={t} className="card">{t}</li>)} </ul> </main> ); } // Stands in for a screen reader: logs each change to a live region, the way a // screen reader would announce it. Elements with aria-live, role="status" or // role="alert" count as live regions. Like many screen readers, it ignores a // region that appears with its text already inside. const LIVE = '[aria-live]:not([aria-live="off"]), [role="status"], [role="alert"]'; new MutationObserver((records) => { const changed = new Set(); for (const record of records) { const node = record.target.nodeType === Node.TEXT_NODE ? record.target.parentElement : record.target; const region = node?.closest(LIVE); if (region) changed.add(region); } for (const region of changed) { const text = region.textContent.trim(); if (text) console.log(`announced: ${text}`); } }).observe(document.body, { subtree: true, childList: true, characterData: true }); export const topics = [ "Compound components", "Controlled props", "Render props", "Imperative handles", "External stores", "Selectors", "State machines", "Suspense", "Request cache", "Lazy loading", "Profiling", "Windowing", "Activity", "Dialogs", "Comboboxes", ]; import { useEffect, useState } from "react"; // Returns value, but only after it has stopped changing for `delay` ms. export function useDebouncedValue(value, delay) { const [debounced, setDebounced] = useState(value); useEffect(() => { const id = setTimeout(() => setDebounced(value), delay); return () => clearTimeout(id); }, [value, delay]); return debounced; } body { font: 15px/1.5 system-ui, sans-serif; margin: 0; background: #f4f6f9; color: #1c2430; } main { max-width: 56rem; margin: 1rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.5rem; font-size: 1.4rem; } input, select, button, textarea { font: inherit; } input, select, textarea { padding: 0.3rem 0.5rem; border: 1px solid #c4ccd6; border-radius: 6px; } button { padding: 0.25rem 0.7rem; border: 1px solid #c4ccd6; border-radius: 6px; background: white; cursor: pointer; } .toolbar { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin-bottom: 0.75rem; } .board { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0.75rem; } .column { background: #e9edf2; border-radius: 10px; padding: 0.6rem; min-height: 6rem; } .column h2 { margin: 0 0 0.5rem; font-size: 0.95rem; } .count { color: #5c6878; font-weight: normal; } .cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.4rem; } .card { background: white; border: 1px solid #d6dde6; border-radius: 8px; padding: 0.45rem 0.6rem; } .card button.title { border: 0; padding: 0; background: none; text-align: left; font-weight: 600; } .card.selected { outline: 2px solid #087ea4; } .muted { color: #5c6878; font-size: 0.85rem; } .panel { margin-top: 0.75rem; background: white; border: 1px solid #d6dde6; border-radius: 10px; padding: 0.75rem 1rem; } .panel h2 { margin: 0 0 0.25rem; font-size: 1.05rem; } .error { color: #b42318; } [role="tablist"] { display: flex; gap: 0.25rem; border-bottom: 1px solid #d6dde6; margin-bottom: 0.75rem; } [role="tab"] { border: 0; border-bottom: 2px solid transparent; border-radius: 0; background: none; padding: 0.4rem 0.8rem; } [role="tab"][aria-selected="true"] { border-bottom-color: #087ea4; color: #06617f; font-weight: 600; } .stars button { border: 0; background: none; padding: 0 0.1rem; font-size: 1.3rem; color: #b9c2cd; } .stars button[aria-checked="true"], .stars button.on { color: #d99a00; } dialog { border: 1px solid #d6dde6; border-radius: 12px; padding: 1rem 1.25rem; width: min(28rem, 90vw); } dialog::backdrop { background: rgb(22 35 45 / 0.45); } [role="listbox"] { list-style: none; margin: 0.25rem 0 0; padding: 0.25rem; border: 1px solid #d6dde6; border-radius: 8px; max-height: 12rem; overflow-y: auto; } [role="option"] { padding: 0.3rem 0.5rem; border-radius: 6px; cursor: pointer; } [role="option"][data-active="true"] { background: #e6f7fc; outline: 2px solid #087ea4; } [role="toolbar"] { display: flex; gap: 0.25rem; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea

One number can't be both instant and delayed. So the page needs two copies of the count: the visible one, which isn't live, and an announced one, which is a live region fed by a value that waits for typing to pause. The announced copy can be hidden from sight but must stay in the accessibility tree.

Hint 2: a first step

Remove aria-live from the visible count, and add a second paragraph with aria-live="polite" that shows the same count. Type st: the console logs a line per letter, so the new region works. Now change what that second paragraph shows, using the hook in src/useDebouncedValue.js, and import it in App.jsx.

Show one solution
JavaScript
const announced = useDebouncedValue(matches.length, 500);
JSX
<p className="muted">{matches.length} topics</p>
<p aria-live="polite" style={{ position: "absolute", width: 1, height: 1, overflow: "hidden", clipPath: "inset(50%)" }}>
  {announced} topics
</p>

Import it with import { useDebouncedValue } from "./useDebouncedValue.js";. The visually hidden style keeps the region out of sight but in the accessibility tree; display: none would hide it from screen readers too.

Was this article helpful?

Your response is saved on this device.