Keyboard, Focus, Forms, Dialogs, and Dynamic UI
Accessibility becomes hardest when UI is interactive. Focus, keyboard behavior, announcements, loading, and client navigation are part of the user's application state even when they are not stored in React state.
Follow platform conventionsβ
Native buttons, links, inputs, checkboxes, radios, and form controls already implement mature keyboard/focus behavior.
Custom ARIA widgets such as tabs, menus, listboxes, trees, and comboboxes require you to implement the expected interaction model yourself.
Roving tabIndexβ
Composite widgets usually should not make every item a page-level Tab stop.
Example:
function TabList({ tabs }: { tabs: Tab[] }) {
const [activeIndex, setActiveIndex] = useState(0);
const refs = useRef<Array<HTMLButtonElement | null>>([]);
function move(nextIndex: number) {
setActiveIndex(nextIndex);
refs.current[nextIndex]?.focus();
}
return (
<div role="tablist" aria-label="Account sections">
{tabs.map((tab, index) => (
<button
key={tab.id}
ref={node => { refs.current[index] = node; }}
role="tab"
tabIndex={index === activeIndex ? 0 : -1}
aria-selected={index === activeIndex}
onKeyDown={event => {
if (event.key === 'ArrowRight') {
event.preventDefault();
move((index + 1) % tabs.length);
}
if (event.key === 'ArrowLeft') {
event.preventDefault();
move((index - 1 + tabs.length) % tabs.length);
}
}}
>
{tab.label}
</button>
))}
</div>
);
}
Production tabs also need tabpanel relationships, activation rules, orientation, disabled-state behavior when supported, and tests matching the chosen APG pattern.
aria-activedescendant is a different focus modelβ
Some widgets keep DOM focus on one container and expose the active child through aria-activedescendant.
<div
role="listbox"
tabIndex={0}
aria-activedescendant={activeOptionId}
>
...
</div>
These approaches are not interchangeable. Follow the accessibility pattern for the widget you are building.
Focus must be visibleβ
Do not globally remove outlines.
.button:focus-visible {
outline: 2px solid currentColor;
outline-offset: 3px;
}
WCAG 2.2 continues to require visible keyboard focus at Level AA and includes additional criteria around focus not being obscured.
Move focus only when the UI transition needs orientationβ
Good reasons include opening a modal, orienting users after client-side navigation, focusing a validation summary, or entering a newly created editor.
Do not move focus merely because a component re-rendered.
Refs are the React escape hatch for focusβ
const headingRef = useRef<HTMLHeadingElement>(null);
function focusHeading() {
headingRef.current?.focus();
}
For a normally non-focusable heading:
<h1 ref={headingRef} tabIndex={-1}>
Checkout
</h1>
tabIndex={-1} allows programmatic focus without placing the heading in normal sequential Tab navigation.
Dialog focus lifecycleβ
ARIA alone does not implement this behavior.
A mature modal primitive owns:
- dialog semantics and accessible name;
- initial focus placement;
- focus containment while modal;
- Escape/close behavior when appropriate;
- background interaction prevention;
- focus restoration.
Well-tested dialog primitives are often safer than repeatedly implementing focus trapping yourself.
Dialog namingβ
const titleId = useId();
return (
<div role="dialog" aria-modal="true" aria-labelledby={titleId}>
<h2 id={titleId}>Delete account?</h2>
...
</div>
);
If using the native <dialog> element or an accessibility library, follow its supported semantics rather than layering redundant roles automatically.
Focus restoration depends on commit timingβ
The element you focus must exist in the committed DOM.
Forms: group controls by meaningβ
<fieldset>
<legend>Shipping method</legend>
<label>
<input type="radio" name="shipping" value="standard" />
Standard
</label>
<label>
<input type="radio" name="shipping" value="express" />
Express
</label>
</fieldset>
fieldset + legend provide group context beyond individual labels.
Validation must communicate more than colorβ
<input
aria-invalid={Boolean(error)}
aria-describedby={error ? errorId : undefined}
/>
{error && <p id={errorId}>{error}</p>}
Visual styling can reinforce the error, but color alone should not carry its meaning.
Error summariesβ
For long forms, an error summary can orient users and provide direct links to invalid fields.
<div role="alert" aria-labelledby={summaryId}>
<h2 id={summaryId}>Fix the following errors</h2>
<ul>
<li><a href={`#${emailId}`}>Email is required</a></li>
</ul>
</div>
If the product focuses the summary after submission failure, test that focus behavior explicitly.
Dynamic updates: announcement and focus are different toolsβ
<p aria-live="polite">{statusMessage}</p>
Possible uses include background save completion, search-result counts, or an item added to cart without navigation.
Do not turn every status change into an assertive alert. Too many announcements create noise.
Suspense and pending UIβ
<Suspense fallback={<p role="status">Loading order historyβ¦</p>}>
<OrderHistory />
</Suspense>
A loading fallback should communicate enough context when users need it, but nested boundaries should not create competing live announcements.
Transitions and stale contentβ
When already-visible content remains during non-urgent work, preserve orientation.
A region may expose aria-busy={isPending} when that accurately represents a meaningful update.
Client-side navigation needs orientation reviewβ
A full-page browser navigation naturally changes document context. A client-side navigation may leave focus sitting on an old control while the page changes around it.
Verify:
- document title updates;
- focus/orientation strategy;
- landmarks and headings;
- pending navigation behavior;
- back/forward behavior.
Critical navigation flows deserve real-browser tests.
Menus, tabs, and comboboxes have specific interaction contractsβ
Do not use role="menu" just because a visual panel looks menu-like. Ordinary site navigation is often better represented by links in a semantic list.
Tabs require coordinated tablist, tab, tabpanel, selection state, relationships, arrow-key focus, and an activation model.
Comboboxes are even more complex: input semantics, popup relationship, expanded state, active option model, selection, Escape behavior, async results, and text-input/IME correctness all matter.
Use established APG patterns or accessible primitives rather than improvising from roles alone.
Keyboard testsβ
const user = userEvent.setup();
render(<Tabs />);
await user.tab();
expect(screen.getByRole('tab', { name: 'Profile' })).toHaveFocus();
await user.keyboard('{ArrowRight}');
expect(screen.getByRole('tab', { name: 'Security' })).toHaveFocus();
Then assert selected state and panel visibility according to your activation model.
Dialog testsβ
const trigger = screen.getByRole('button', { name: 'Delete account' });
await user.click(trigger);
const dialog = screen.getByRole('dialog', { name: 'Delete account?' });
expect(dialog).toBeVisible();
// Assert intended initial focus.
await user.keyboard('{Escape}');
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
expect(trigger).toHaveFocus();
This protects the complete user journey rather than only the dialog markup.
Review checklistβ
- Do native controls provide the behavior already?
- Is the page Tab sequence manageable?
- Is focus always visible and not intentionally obscured?
- Does a modal move focus in, contain the interaction, and restore focus?
- Do errors communicate through text/relationships rather than color alone?
- Are live regions used sparingly?
- Does pending UI preserve orientation?
- Do custom widgets follow an established keyboard model?
- Are important flows tested with keyboard and focus assertions?
- Are critical navigation/widget flows verified in a real browser?
Interview questionsβ
- What is roving
tabIndex? - When is
aria-activedescendantuseful? - Why is focus management tied to commit timing?
- What is the expected modal focus lifecycle?
- When should a live region be used instead of moving focus?
- Why does
role="tab"alone not make accessible tabs?