...
// Better:
// Document structure roles
...
...
```
### States and Properties
States indicate current conditions; properties describe relationships.
```tsx
// States (can change)
aria-checked="true|false|mixed"
aria-disabled="true|false"
aria-expanded="true|false"
aria-hidden="true|false"
aria-pressed="true|false"
aria-selected="true|false"
// Properties (usually static)
aria-label="Accessible name"
aria-labelledby="id-of-label"
aria-describedby="id-of-description"
aria-controls="id-of-controlled-element"
aria-owns="id-of-owned-element"
aria-live="polite|assertive|off"
```
## Common ARIA Patterns
### Accordion
```tsx
function Accordion({ items }) {
const [openIndex, setOpenIndex] = useState(-1);
return (
{items.map((item, index) => {
const isOpen = openIndex === index;
const headingId = `accordion-heading-${index}`;
const panelId = `accordion-panel-${index}`;
return (
setOpenIndex(isOpen ? -1 : index)}
>
{item.title}
{isOpen ? '−' : '+'}
{item.content}
);
})}
);
}
```
### Tabs
```tsx
function Tabs({ tabs }) {
const [activeIndex, setActiveIndex] = useState(0);
const tabListRef = useRef(null);
const handleKeyDown = (e, index) => {
let newIndex = index;
switch (e.key) {
case 'ArrowRight':
newIndex = (index + 1) % tabs.length;
break;
case 'ArrowLeft':
newIndex = (index - 1 + tabs.length) % tabs.length;
break;
case 'Home':
newIndex = 0;
break;
case 'End':
newIndex = tabs.length - 1;
break;
default:
return;
}
e.preventDefault();
setActiveIndex(newIndex);
tabListRef.current?.children[newIndex]?.focus();
};
return (
{tabs.map((tab, index) => (
setActiveIndex(index)}
onKeyDown={(e) => handleKeyDown(e, index)}
>
{tab.label}
))}
{tabs.map((tab, index) => (
{tab.content}
))}
);
}
```
### Menu Button
```tsx
function MenuButton({ label, items }) {
const [isOpen, setIsOpen] = useState(false);
const [activeIndex, setActiveIndex] = useState(-1);
const buttonRef = useRef(null);
const menuRef = useRef(null);
const menuId = useId();
const handleKeyDown = (e) => {
switch (e.key) {
case 'ArrowDown':
e.preventDefault();
if (!isOpen) {
setIsOpen(true);
setActiveIndex(0);
} else {
setActiveIndex((prev) => Math.min(prev + 1, items.length - 1));
}
break;
case 'ArrowUp':
e.preventDefault();
setActiveIndex((prev) => Math.max(prev - 1, 0));
break;
case 'Escape':
setIsOpen(false);
buttonRef.current?.focus();
break;
case 'Enter':
case ' ':
if (isOpen && activeIndex >= 0) {
e.preventDefault();
items[activeIndex].onClick();
setIsOpen(false);
}
break;
}
};
// Focus management
useEffect(() => {
if (isOpen && activeIndex >= 0) {
menuRef.current?.children[activeIndex]?.focus();
}
}, [isOpen, activeIndex]);
return (
setIsOpen(!isOpen)}
onKeyDown={handleKeyDown}
>
{label}
{isOpen && (
)}
);
}
```
### Combobox (Autocomplete)
```tsx
function Combobox({ options, onSelect, placeholder }) {
const [inputValue, setInputValue] = useState('');
const [isOpen, setIsOpen] = useState(false);
const [activeIndex, setActiveIndex] = useState(-1);
const inputRef = useRef(null);
const listboxId = useId();
const filteredOptions = options.filter((opt) =>
opt.toLowerCase().includes(inputValue.toLowerCase())
);
const handleKeyDown = (e) => {
switch (e.key) {
case 'ArrowDown':
e.preventDefault();
setIsOpen(true);
setActiveIndex((prev) =>
Math.min(prev + 1, filteredOptions.length - 1)
);
break;
case 'ArrowUp':
e.preventDefault();
setActiveIndex((prev) => Math.max(prev - 1, 0));
break;
case 'Enter':
if (activeIndex >= 0) {
e.preventDefault();
selectOption(filteredOptions[activeIndex]);
}
break;
case 'Escape':
setIsOpen(false);
setActiveIndex(-1);
break;
}
};
const selectOption = (option) => {
setInputValue(option);
onSelect(option);
setIsOpen(false);
setActiveIndex(-1);
};
return (
);
}
```
### Alert Dialog
```tsx
function AlertDialog({ isOpen, onConfirm, onCancel, title, message }) {
const confirmRef = useRef(null);
const dialogId = useId();
const titleId = `${dialogId}-title`;
const descId = `${dialogId}-desc`;
useEffect(() => {
if (isOpen) {
confirmRef.current?.focus();
}
}, [isOpen]);
if (!isOpen) return null;
return (
{title}
{message}
Cancel
Confirm
);
}
```
### Toolbar
```tsx
function Toolbar({ items }) {
const [activeIndex, setActiveIndex] = useState(0);
const toolbarRef = useRef(null);
const handleKeyDown = (e) => {
let newIndex = activeIndex;
switch (e.key) {
case 'ArrowRight':
newIndex = (activeIndex + 1) % items.length;
break;
case 'ArrowLeft':
newIndex = (activeIndex - 1 + items.length) % items.length;
break;
case 'Home':
newIndex = 0;
break;
case 'End':
newIndex = items.length - 1;
break;
default:
return;
}
e.preventDefault();
setActiveIndex(newIndex);
toolbarRef.current?.querySelectorAll('button')[newIndex]?.focus();
};
return (
{items.map((item, index) => (
{item.icon}
))}
);
}
```
## Live Regions
### Polite Announcements
```tsx
// Status messages that don't interrupt
function SearchStatus({ count, query }) {
return (
{count} results found for "{query}"
);
}
// Progress indicator
function LoadingStatus({ progress }) {
return (
Loading: {progress}% complete
);
}
```
### Assertive Announcements
```tsx
// Important errors that should interrupt
function ErrorAlert({ message }) {
return (
Error: {message}
);
}
// Form validation summary
function ValidationSummary({ errors }) {
if (errors.length === 0) return null;
return (
Please fix the following errors:
{errors.map((error, index) => (
{error}
))}
);
}
```
### Log Region
```tsx
// Chat messages or activity log
function ChatLog({ messages }) {
return (
{messages.map((msg) => (
{msg.author}:
{msg.text}
))}
);
}
```
## Common Mistakes to Avoid
### 1. Redundant ARIA
```tsx
// Bad: role="button" on a button
Click me
// Good: just use button
Click me
// Bad: aria-label duplicating visible text
Submit form
// Good: just use visible text
Submit form
```
### 2. Invalid ARIA
```tsx
// Bad: aria-selected on non-selectable element
Item
// Good: use with proper role
Item
// Bad: aria-expanded without control relationship
Menu
Menu content
// Good: with aria-controls
Menu
```
### 3. Hidden Content Still Announced
```tsx
// Bad: visually hidden but still in accessibility tree
Hidden content
// Good: properly hidden
Hidden content
// Or just use display: none (implicitly hidden)
Hidden content
```
## Resources
- [WAI-ARIA Authoring Practices](https://www.w3.org/WAI/ARIA/apg/)
- [ARIA in HTML](https://www.w3.org/TR/html-aria/)
- [Using ARIA](https://www.w3.org/TR/using-aria/)