Accessibility patterns for React and Next.js — semantic HTML, ARIA attributes, form labeling, keyboard navigation, focus management, and screen reader support. Use when building any interactive UI component or form.
Accessibility patterns for React and Next.js — semantic HTML, ARIA attributes, form labeling, keyboard navigation, focus management, and screen reader support. Use when building any interactive UI component or form.
metadata
{"origin":"community"}
Frontend Accessibility Patterns
Practical accessibility patterns for React and Next.js. Covers the issues most commonly flagged in code review: missing form labels, incorrect ARIA usage, non-semantic interactive elements, and broken keyboard navigation.
When to Activate
Building or reviewing form components (<input>, <select>, <textarea>)
Creating interactive elements (modals, dropdowns, tooltips, tabs)
Using <div> or <span> with onClick
Adding aria-* attributes to any element
Implementing keyboard navigation or focus management
Receiving accessibility feedback from code review tools (CodeRabbit, ESLint a11y)
Building components that must support screen readers
Form Accessibility
Missing htmlFor / id pairing and disconnected error messages are the most common issues flagged in code review.
Label Connection
// BAD: label has no connection to input — screen readers cannot associate them
<label>Email</label>
< = />
input
type
"email"
// GOOD: htmlFor matches input id
<labelhtmlFor="email">Email</label>
<inputid="email"type="email" />
Required Fields
// BAD: visual-only asterisk conveys nothing to screen readers
<label htmlFor="email">Email *</label>
<inputid="email"type="email" />// GOOD: required enables native browser validation; aria-required signals it to screen readers<labelhtmlFor="email">
Email <spanaria-hidden="true">*</span></label><inputid="email"type="email"requiredaria-required="true" />
Error Messages
// BAD: error text exists visually but is not linked to the input
<input id="email"type="email" />
<spanclassName="error">Invalid email address</span>// GOOD: aria-describedby connects input to its error message// aria-invalid signals the invalid state to screen readers<inputid="email"type="email"aria-describedby="email-error"aria-invalid={!!error}
/>
{error && (
<spanid="email-error"role="alert">
{error}
</span>
)}
Use the element that matches the intent. Screen readers and keyboard users depend on native semantics.
// BAD: div has no role, no keyboard support, no accessible name
<div onClick={handleClick}>Submit</div>
// GOOD: button is focusable, activates on Enter/Space, announces as "button"<buttontype="button"onClick={handleClick}>Submit</button>
Use ARIA only when native HTML semantics are insufficient. Wrong ARIA is worse than no ARIA.
aria-label vs aria-labelledby
// aria-label: inline string label — use when no visible label text exists
<button aria-label="Close modal">
<XIcon />
</button>
// aria-labelledby: references another element's text — use when a visible label exists<sectionaria-labelledby="section-title"><h2id="section-title">Recent Orders</h2>
{/* content */}
</section>
aria-describedby
// Provides supplementary description beyond the label
<button
aria-describedby="delete-warning"
onClick={handleDelete}
> Delete account
</button>
<pid="delete-warning">This action cannot be undone.</p>
aria-live for Dynamic Content
// Use aria-live to announce content that updates without a page reload// polite: waits for user to finish current action before announcing// assertive: interrupts immediately — use only for urgent errorsexportfunctionStatusMessage({ message, isError }: { message: string; isError?: boolean }) {
return (
<divrole="status"aria-live={isError ? 'assertive' : 'polite'} aria-atomic="true">
{message}
</div>
);
}
Focus must move logically when UI state changes — especially for modals and route transitions.
Modal Focus Restoration
This example covers initial focus and restoration. For a full focus trap (Tab/Shift+Tab cycling within the modal), use a library like focus-trap-react which handles edge cases like dynamic content and nested portals.
exportfunctionModal({ isOpen, onClose, title, children }: { isOpen: boolean; onClose: () => void; title: string; children: React.ReactNode }) {
const modalRef = useRef<HTMLDivElement>(null);
const previousFocusRef = useRef<HTMLElement | null>(null);
useEffect(() => {
if (isOpen) {
// Save currently focused element and move focus into modal
previousFocusRef.current = document.activeElementasHTMLElement;
modalRef.current?.focus();
} else {
// Restore focus to the element that opened the modal
previousFocusRef.current?.focus();
}
}, [isOpen]);
if (!isOpen) returnnull;
return (
<divref={modalRef}role="dialog"aria-modal="true"aria-labelledby="modal-title"tabIndex={-1}onKeyDown={e => e.key === 'Escape' && onClose()}>
<h2id="modal-title">{title}</h2>
{children}
<buttononClick={onClose}>Close</button></div>
);
}
Images and Icons
// BAD: decorative icon announced as unlabeled image
<img src="/icon.svg" />
// GOOD: decorative image hidden from screen readers<imgsrc="/decoration.png"alt=""aria-hidden="true" />// GOOD: meaningful image with descriptive alt text<imgsrc="/chart.png"alt="Monthly revenue increased 23% from January to March" />// GOOD: icon button with accessible label<buttonaria-label="Delete item"><TrashIconaria-hidden="true" /></button>
Reduced Motion
Respect users who have requested reduced motion in their OS settings.
// BAD: onClick on non-interactive element with no keyboard support
<div onClick={handleClick}>Click me</div>
// BAD: aria-label on a div that has no role<divaria-label="Navigation">...</div>// BAD: placeholder used as a substitute for label<inputplaceholder="Enter your email" />// BAD: positive tabIndex creates unpredictable tab order<buttontabIndex={3}>Submit</button>// BAD: aria-hidden on a focusable element — keyboard users get trapped<buttonaria-hidden="true">Open</button>// BAD: role="button" on div without keyboard handler<divrole="button"onClick={handleClick}>Submit</div>// Missing: tabIndex={0}, onKeyDown for Enter/Space
Checklist
Before submitting any interactive component for review:
Every <input>, <select>, and <textarea> has a connected <label> via htmlFor/id
Error messages are linked with aria-describedby and marked role="alert"
No onClick on <div> or <span> without role, tabIndex, and onKeyDown
Icon-only buttons have aria-label
Decorative images use alt="" and aria-hidden="true"
Modals restore focus on close (for full focus trapping with Tab/Shift+Tab cycling, use a library like focus-trap-react)
Dynamic content updates use aria-live
prefers-reduced-motion is respected for animations
Related Skills
frontend-patterns — general React component and state patterns
design-system — design token and component consistency
motion-ui — animation patterns with accessibility considerations