Accessibility in a React application is mostly a focus problem wearing a checklist. A keyboard user moves through your interface by asking one question at every moment: where is focus right now, and where will the next press of Tab take me. When the answer is predictable, everything else follows, because correct semantic markup, labels and states are what screen readers read out loud in the first place. When the answer is not predictable, a person using a keyboard or a screen reader reaches a dead end, and no amount of ARIA sprinkled on top recovers the experience.
â ī¸ARIA is a repair tool
The first rule of accessible markup is not to use ARIA when a native element already does the job. A button element gives you focus, Enter and Space activation, and a role for free. A div with a click handler gives you none of that, and every ARIA attribute you add is an attempt to rebuild by hand what the browser already shipped.
Semantic structure before widgets
Start above the widget level, because a perfect dialog inside a page with no landmarks is still a page a screen reader user has to hunt through. One main element per page, a header that contains the site navigation, and headings that descend without skipping levels give a reader a map they can jump through with single-key shortcuts. React makes this easy to get wrong, because a component that renders a generic div with a click handler looks fine visually while contributing nothing to that map.
Lists are the clearest example. If items are related and order is meaningful, render them in an ordered or unordered list and let the reader hear how many items exist before hearing any of them. A row of divs styled with flexbox reads as unrelated fragments, and a screen reader user has no way to tell where the group begins or ends. The same principle covers forms: every input needs a label bound with htmlFor and a matching id, every group of related inputs belongs inside a fieldset with a legend, and error text should sit next to the field it describes and be referenced from the input with aria-describedby.
Finally, keep the DOM order aligned with the visual order. CSS can reposition anything, so a layout that reads top to bottom while the source reads left to right produces a focus order that contradicts what a sighted keyboard user sees. Fix the order in markup rather than compensating with a positive tabindex, because positive values create their own order and are discouraged for exactly that reason.
A modal that does not trap or strand the user
Dialogs are where most implementations fail. The three obligations are: move focus into the dialog when it opens, keep Tab cycling inside it while it is open, and return focus to the element that opened it when it closes. Miss the third and a keyboard user is dumped at the top of the document after every dialog, losing their place in the page.
import { useEffect, useRef } from 'react';
const FOCUSABLE =
'a[href], button:not([disabled]), input:not([disabled]), select, textarea, [tabindex]:not([tabindex="-1"])';
export function Dialog({ open, onClose, title, children }) {
const dialogRef = useRef(null);
const openerRef = useRef(null);
useEffect(() => {
if (!open) return;
openerRef.current = document.activeElement;
const node = dialogRef.current;
const first = node.querySelector(FOCUSABLE);
(first || node).focus();
function onKeyDown(e) {
if (e.key === 'Escape') {
e.stopPropagation();
onClose();
return;
}
if (e.key !== 'Tab') return;
const items = [...node.querySelectorAll(FOCUSABLE)];
if (!items.length) return;
const firstItem = items[0];
const lastItem = items[items.length - 1];
if (e.shiftKey && document.activeElement === firstItem) {
e.preventDefault();
lastItem.focus();
} else if (!e.shiftKey && document.activeElement === lastItem) {
e.preventDefault();
firstItem.focus();
}
}
document.addEventListener('keydown', onKeyDown, true);
const previousOverflow = document.body.style.overflow;
document.body.style.overflow = 'hidden';
return () => {
document.removeEventListener('keydown', onKeyDown, true);
document.body.style.overflow = previousOverflow;
openerRef.current?.focus();
};
}, [open, onClose]);
if (!open) return null;
return (
<div className="dialog-backdrop" onMouseDown={(e) => e.target === e.currentTarget && onClose()}>
<div
ref={dialogRef}
role="dialog"
aria-modal="true"
aria-labelledby="dialog-title"
tabIndex={-1}
onMouseDown={(e) => e.stopPropagation()}
>
<h2 id="dialog-title">{title}</h2>
{children}
<button type="button" onClick={onClose}>Close</button>
</div>
</div>
);
}
The backdrop handler uses mousedown rather than click so a drag that starts inside the dialog and ends on the backdrop does not close it by accident, and the escape listener is registered on the capture phase so it still fires when focus sits inside an embedded editor. Details like these are what separate a dialog that feels solid from one that occasionally strands the user.
Roving tabindex for composite widgets
A tab bar, a menu, a toolbar and a data grid are all composite widgets: many focusable elements acting as one control. Letting every entry receive Tab focus turns a grid into a two hundred stop journey, so the convention is roving tabindex. Exactly one element inside the widget has tabindex zero, the rest have tabindex minus one, and arrow keys move the single focus position while Tab exits the widget entirely.
import { useRef, useState } from 'react';
export function Menu({ items, onSelect }) {
const [activeIndex, setActiveIndex] = useState(0);
const menuRef = useRef(null);
function focusItem(index) {
const next = (index + items.length) % items.length;
setActiveIndex(next);
const nodes = menuRef.current.querySelectorAll('[role="menuitem"]');
nodes[next].focus();
}
function onKeyDown(e) {
switch (e.key) {
case 'ArrowDown': e.preventDefault(); focusItem(activeIndex + 1); break;
case 'ArrowUp': e.preventDefault(); focusItem(activeIndex - 1); break;
case 'Home': e.preventDefault(); focusItem(0); break;
case 'End': e.preventDefault(); focusItem(items.length - 1); break;
case 'Escape': e.preventDefault(); menuRef.current.focus(); break;
default: break;
}
}
return (
<div
ref={menuRef}
role="menu"
tabIndex={0}
aria-activedescendant={`menu-item-${activeIndex}`}
onKeyDown={onKeyDown}
>
{items.map((item, index) => (
<div
key={item.id}
id={`menu-item-${index}`}
role="menuitem"
tabIndex={index === activeIndex ? 0 : -1}
onFocus={() => setActiveIndex(index)}
onClick={() => onSelect(item)}
>
{item.label}
</div>
))}
</div>
);
}
Two rules keep this consistent across a product. Escape always returns to the container rather than leaving the widget, and Home and End always jump to the extremes, because those expectations are shared by every native control a user already knows. Deviating from them in one widget is what makes an application feel inconsistent to someone navigating by keyboard.
ARIA that earns its place
Every attribute below has a specific job, and each one corresponds to information that is visible to a mouse user but invisible to everyone else.
| Attribute | What it communicates | Use when | Common mistake |
|---|---|---|---|
| aria-label | Accessible name with no visible text | Icon-only buttons and inputs | Repeating the visible label instead of leaving it out |
| aria-labelledby | Name computed from another element | Dialogs, sections, groups | Pointing at an id that changes on re-render |
| aria-expanded | Whether a control reveals more content | Disclosure, select, tree | Omitting it so the state is never announced |
| aria-live | That a region changed after load | Status messages, toasts, counters | Putting polite on every region so nothing is read |
| aria-busy | That content is being updated | Async tables, streaming regions | Leaving it true after the update finishes |
| aria-current | Position in a set or sequence | Breadcrumbs, pagination, nav | Using it for emphasis rather than location |
| aria-hidden | Remove from the accessibility tree | Purely decorative imagery | Hiding focusable content that is still reachable |
The rule of thumb is that aria-hidden on an element that can still receive focus creates a trap: a sighted keyboard user tabs to something a screen reader refuses to announce. If it can take focus, it must be reachable and announced, or it must not be focusable at all.
Announcing what changed
Static markup is only half the contract. When a result count updates, a form saves, or a filtered list changes, assistive technology needs to be told, because nothing moves focus and therefore nothing is read. The mechanism is an aria-live region, and its politeness setting decides whether the announcement interrupts.
Use polite for updates that can wait for a pause: a new number of search results, a saved draft confirmation. Use assertive only when the user must know now, such as a session about to expire. The trap is that the live region must exist in the DOM before the text changes, so rendering a fresh element together with its new content produces no announcement at all. Create the container once, then update its text.
Focus management is the other half. When a route change leaves focus on the body, a screen reader user hears nothing and has to hunt for position. Move focus to the new page heading after navigation, or set the main landmark as the target, and the reader hears the title of the page they just opened.
Testing without a screen reader on your team
Automated tools catch roughly a third of real issues, and the third they catch is the cheap one: missing labels, empty buttons, contrast below threshold. Run axe or Lighthouse in continuous integration so those never regress, then spend the remaining budget on two manual passes. Unplug the mouse and operate the primary flow with the keyboard alone, noting every place focus vanished or the order became illogical. Then, on one feature per sprint, run a screen reader through the same flow on a real device.
Both passes take minutes and find what no linter can: a modal that opens behind the page, a toast that is invisible to the reader, a grid where arrow keys fight the browser's own scrolling. Those are the failures that send a user to a competitor, and they are invisible in a screenshot review.
Common Mistakes
Rendering a clickable div and bolting a click handler plus a role onto it
Fix: Use a native button, link or input so focus, activation and role arrive without any ARIA at all.
Opening a dialog without moving focus and without restoring it on close
Fix: Store the opener, focus the first control on open, and return focus to that element in the cleanup function.
Letting every row of a data grid take Tab focus
Fix: Apply roving tabindex so the widget holds a single tab stop and arrow keys move within it.
Inserting an aria-live region at the same time as its message
Fix: Keep the live container mounted, then change its text so assistive technology actually announces the update.
Best Practices
- Reach for the native element first and add ARIA only for state or relationships the platform does not express
- Keep focus order aligned with the visual order, because a mismatch is the most disorienting keyboard defect there is
- Pair every animated or async change with an announcement appropriate to its urgency
- Add an automated accessibility check to continuous integration, then do one keyboard pass and one screen reader pass per feature
Key Takeaways
- Accessibility in React is decided by where focus is, where it goes next, and whether the semantic element carried that information
- Dialogs must move focus in, contain Tab, and restore focus to the opener when they close
- Composite widgets use roving tabindex so the whole control is one tab stop navigated by arrow keys
- ARIA attributes repair missing semantics; on native elements they are usually redundant, and aria-hidden on focusable content creates traps
- Announcements require a live region that already exists in the document, with politeness matched to urgency