Cursor rule for accessible React UI covering semantic elements, labels, keyboard support, dialog focus management, alt text, WCAG 2.2 AA contrast and reduced motion.
.cursor/rules/react-accessibility-guidelines.mdc. Downloaded files need to be placed in that repository path.---
description: "Cursor rule for accessible React UI covering semantic elements, labels, keyboard support, dialog focus management, alt text, WCAG 2.2 AA contrast and reduced motion."
globs: "**/*"
alwaysApply: false
---
# React Accessibility Guidelines
Applies to: React components (`.tsx`/`.jsx`) that render UI, including forms, dialogs, menus, media and animated content.
Version baseline: checked against WCAG 2.2 (W3C Recommendation), the WAI-ARIA Authoring Practices Guide (APG) and the React 19 docs. If the project has an accessibility statement or a stricter target, follow that.
## 1. Semantic HTML first, ARIA second
- Use the native element for the job: `<button>` for actions, `<a href>` for navigation, `<label>` for inputs, `<ul>/<li>` for lists, `<table>` for tabular data, `<nav>`, `<main>`, `<header>` and `<footer>` for landmarks, and headings in order (`h1` → `h2` → `h3`).
- Never put `onClick` on a `<div>` or `<span>` to make it a button. Native elements give you focus, keyboard activation and roles for free.
- Add ARIA only when no native element expresses the state or role (for example `aria-expanded` on a disclosure button, or `aria-current="page"` on the active nav link). A wrong role is worse than no role. Do not add `role="listbox"` or `role="option"` unless you implement the full APG keyboard pattern.
## 2. Names, labels and descriptions
- Every form control has a visible `<label htmlFor>` (or a wrapping `<label>`). Placeholder text is not a label.
- Icon-only buttons need an accessible name: `<button aria-label="Close dialog">` or visually hidden text.
- Associate help and error text with `aria-describedby`, and mark invalid fields with `aria-invalid="true"`. Use `useId()` to generate matching ids.
## 3. Keyboard support
- Everything that works with a mouse works with the keyboard: Tab/Shift+Tab to move, Enter/Space to activate, Escape to close overlays.
- Do not use positive `tabIndex` values. Use `tabIndex={0}` only on custom widgets that follow an APG pattern, and `tabIndex={-1}` for elements that receive focus programmatically.
- Keep a visible focus indicator. Never remove outlines without providing a replacement `:focus-visible` style.
## 4. Focus management
- When a dialog opens, move focus into it. When it closes, return focus to the element that opened it. The native `<dialog>` element with `showModal()` traps focus, closes on Escape and makes the rest of the page inert.
- After client-side navigation or a major content change, move focus to the new page heading or announce the change.
- Announce async results (saved, error, search result count) in a polite live region (`role="status"` or `aria-live="polite"`) that exists in the DOM before the message is inserted.
```tsx
import { useId, useRef, useState } from "react";
export function ConfirmDelete({ onConfirm }: { onConfirm: () => void }) {
const dialogRef = useRef<HTMLDialogElement>(null);
const titleId = useId();
const [status, setStatus] = useState("");
return (
<>
<button type="button" onClick={() => dialogRef.current?.showModal()}>
Delete project
</button>
<dialog ref={dialogRef} aria-labelledby={titleId} onClose={() => setStatus("")}>
<h2 id={titleId}>Delete this project?</h2>
<p>This cannot be undone.</p>
<form method="dialog">
<button value="cancel">Cancel</button>
<button value="confirm" onClick={() => { onConfirm(); setStatus("Project deleted"); }}>
Delete
</button>
</form>
</dialog>
<p role="status">{status}</p>
</>
);
}
```
A native `<dialog>` closed with `method="dialog"` returns focus to the previously focused element in current browsers. Still test it with the keyboard.
## 5. Images and media
- Informative images get `alt` text that conveys their purpose. Decorative images get `alt=""`. Do not repeat adjacent caption text or write "image of".
- Video needs captions, and audio needs a transcript. Do not autoplay audio.
## 6. Colour and contrast (WCAG 2.2 AA)
- Text contrast is at least 4.5:1 (3:1 for large text: 24px regular, or 18.66px bold). UI component boundaries, focus indicators and meaningful icons are at least 3:1 against adjacent colours.
- Never use colour alone to convey state (errors, required fields, chart series). Add text or an icon.
- Interactive targets are at least 24×24 CSS pixels, or have enough spacing (WCAG 2.2 SC 2.5.8).
## 7. Motion
- Respect `prefers-reduced-motion`. Disable or reduce non-essential animation and parallax:
```css
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; scroll-behavior: auto !important; }
}
```
- Nothing flashes more than three times per second.
## Verify before finishing
- Lint with `eslint-plugin-jsx-a11y` if the project has it, and run automated checks with axe (for example `@axe-core/playwright` or `vitest-axe`/`jest-axe`). Automated tools find only part of the issues.
- Do a keyboard-only pass of the changed UI (Tab order, Escape, focus return), and a quick screen reader pass (VoiceOver on macOS/iOS, NVDA on Windows, TalkBack on Android) for new widgets.
- Check contrast of any new colours with a contrast checker.
Cursor rules for Next.js 14+ App Router projects updated for Next.js 16 with async params, validated Route Handlers, generateMetadata and proxy.ts in place of middleware.
Cursor rules for strict TypeScript: compiler checks beyond strict, interfaces vs type aliases, real type guards, discriminated unions, utility types and branded IDs.
Cursor rules for Tailwind CSS v4: automatic class sorting, mobile-first breakpoints, CVA variants with merging, class-based dark mode via @custom-variant, and CSS-first @theme configuration.
Cursor
frontend
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
A developer asks Cursor to build or fix an interactive React component (dialog, menu, form) and wants it to be usable with a keyboard and screen reader.
Fix DeleteButton.tsx: it renders <div onClick={open}><TrashIcon/></div>, opens a custom modal built from divs without focus handling, and shows a green toast 'Deleted' that screen readers never announce.Illustrative; not a recorded model run. The div becomes a <button type="button"> with an accessible name (aria-label or visible text). The modal becomes a native <dialog> opened with showModal() and labelled by its heading via useId, with Escape closing it and focus returning to the trigger. The toast text is rendered into a role="status" region that already exists in the DOM. The answer recommends an axe check and a keyboard-only pass.
Cursor applies project rules stored as .mdc files in .cursor/rules. Each rule's frontmatter (description, globs, alwaysApply) decides whether it is always included, attached to matching files, chosen by the agent from its description, or only used when you @-mention it. Cursor also reads AGENTS.md in the project root and subdirectories for plain-Markdown instructions.
.cursor/rules/react-accessibility-guidelines.mdcStarting point, not a tested configuration for your repository. Attached when files matching the globs are in context (alwaysApply: false with globs). Review the instructions, adapt the file paths and run your project's existing checks.
---
description: "React Accessibility Guidelines: Semantic HTML, Keyboard, Focus and WCAG 2.2 AA project conventions"
globs: "**/*"
alwaysApply: false
---
# React Accessibility Guidelines
Applies to: React components (`.tsx`/`.jsx`) that render UI, including forms, dialogs, menus, media and animated content.
Version baseline: checked against WCAG 2.2 (W3C Recommendation), the WAI-ARIA Authoring Practices Guide (APG) and the React 19 docs. If the project has an accessibility statement or a stricter target, follow that.
## 1. Semantic HTML first, ARIA second
- Use the native element for the job: `<button>` for actions, `<a href>` for navigation, `<label>` for inputs, `<ul>/<li>` for lists, `<table>` for tabular data, `<nav>`, `<main>`, `<header>` and `<footer>` for landmarks, and headings in order (`h1` → `h2` → `h3`).
- Never put `onClick` on a `<div>` or `<span>` to make it a button. Native elements give you focus, keyboard activation and roles for free.
- Add ARIA only when no native element expresses the state or role (for example `aria-expanded` on a disclosure button, or `aria-current="page"` on the active nav link). A wrong role is worse than no role. Do not add `role="listbox"` or `role="option"` unless you implement the full APG keyboard pattern.
## 2. Names, labels and descriptions
- Every form control has a visible `<label htmlFor>` (or a wrapping `<label>`). Placeholder text is not a label.
- Icon-only buttons need an accessible name: `<button aria-label="Close dialog">` or visually hidden text.
- Associate help and error text with `aria-describedby`, and mark invalid fields with `aria-invalid="true"`. Use `useId()` to generate matching ids.
## 3. Keyboard support
- Everything that works with a mouse works with the keyboard: Tab/Shift+Tab to move, Enter/Space to activate, Escape to close overlays.
- Do not use positive `tabIndex` values. Use `tabIndex={0}` only on custom widgets that follow an APG pattern, and `tabIndex={-1}` for elements that receive focus programmatically.
- Keep a visible focus indicator. Never remove outlines without providing a replacement `:focus-visible` style.
## 4. Focus management
- When a dialog opens, move focus into it. When it closes, return focus to the element that opened it. The native `<dialog>` element with `showModal()` traps focus, closes on Escape and makes the rest of the page inert.
- After client-side navigation or a major content change, move focus to the new page heading or announce the change.
- Announce async results (saved, error, search result count) in a polite live region (`role="status"` or `aria-live="polite"`) that exists in the DOM before the message is inserted.
```tsx
import { useId, useRef, useState } from "react";
export function ConfirmDelete({ onConfirm }: { onConfirm: () => void }) {
const dialogRef = useRef<HTMLDialogElement>(null);
const titleId = useId();
const [status, setStatus] = useState("");
return (
<>
<button type="button" onClick={() => dialogRef.current?.showModal()}>
Delete project
</button>
<dialog ref={dialogRef} aria-labelledby={titleId} onClose={() => setStatus("")}>
<h2 id={titleId}>Delete this project?</h2>
<p>This cannot be undone.</p>
<form method="dialog">
<button value="cancel">Cancel</button>
<button value="confirm" onClick={() => { onConfirm(); setStatus("Project deleted"); }}>
Delete
</button>
</form>
</dialog>
<p role="status">{status}</p>
</>
);
}
```
A native `<dialog>` closed with `method="dialog"` returns focus to the previously focused element in current browsers. Still test it with the keyboard.
## 5. Images and media
- Informative images get `alt` text that conveys their purpose. Decorative images get `alt=""`. Do not repeat adjacent caption text or write "image of".
- Video needs captions, and audio needs a transcript. Do not autoplay audio.
## 6. Colour and contrast (WCAG 2.2 AA)
- Text contrast is at least 4.5:1 (3:1 for large text: 24px regular, or 18.66px bold). UI component boundaries, focus indicators and meaningful icons are at least 3:1 against adjacent colours.
- Never use colour alone to convey state (errors, required fields, chart series). Add text or an icon.
- Interactive targets are at least 24×24 CSS pixels, or have enough spacing (WCAG 2.2 SC 2.5.8).
## 7. Motion
- Respect `prefers-reduced-motion`. Disable or reduce non-essential animation and parallax:
```css
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; scroll-behavior: auto !important; }
}
```
- Nothing flashes more than three times per second.
## Verify before finishing
- Lint with `eslint-plugin-jsx-a11y` if the project has it, and run automated checks with axe (for example `@axe-core/playwright` or `vitest-axe`/`jest-axe`). Automated tools find only part of the issues.
- Do a keyboard-only pass of the changed UI (Tab order, Escape, focus return), and a quick screen reader pass (VoiceOver on macOS/iOS, NVDA on Windows, TalkBack on Android) for new widgets.
- Check contrast of any new colours with a contrast checker.
Ask for a small change to one matching file. Check the tool's loaded context or instruction view, inspect the diff for the requested convention, and run the relevant build or test. A well-formatted file does not guarantee the model follows every instruction.
Cursor's docs recommend keeping individual rules under 500 lines and splitting large rules into composable ones. Cursor's documentation states that it supports AGENTS.md in the project root and in subdirectories. Use AGENTS.md for plain-Markdown instructions you also want other tools to read, and .mdc rules when you need Cursor's scoping options (globs, description-based selection, always-apply).
Documentation checked 2026-11-05: Cursor docs: Rules
Cursor rules for Next.js 14+ App Router projects updated for Next.js 16 with async params, validated Route Handlers, generateMetadata and proxy.ts in place of middleware.
Cursor rules for strict TypeScript: compiler checks beyond strict, interfaces vs type aliases, real type guards, discriminated unions, utility types and branded IDs.
Cursor rules for Tailwind CSS v4: automatic class sorting, mobile-first breakpoints, CVA variants with merging, class-based dark mode via @custom-variant, and CSS-first @theme configuration.