Accordion
UI Coding
Easy
25 min
accessibility
design-system
state
Build an Accordion component that shows a list of sections. Each section has a header button that expands or collapses its panel.
The starter file has the section data and a static layout. Make it interactive and accessible.
Requirements
- All sections start collapsed.
- Clicking a header toggles its section. Several sections can be open at once.
- Each header shows an indicator (for example a chevron) that reflects whether it's open.
- Accessibility, following the WAI-ARIA Accordion pattern:
- Each header is a real
<button>witharia-expandedandaria-controlspointing to its panel. - Each panel has
role="region"andaria-labelledbypointing back to its header. - Collapsed panels are hidden from everyone, including screen readers (
hidden). - IDs are unique even if the page renders two accordions (use
useId).
- Each header is a real
- Keyboard support: <kbd>Enter</kbd>/<kbd>Space</kbd> toggle (native buttons do this). <kbd>↓</kbd>/<kbd>↑</kbd> move focus to the next/previous header and wrap around. <kbd>Home</kbd>/<kbd>End</kbd> jump to the first/last header.
Follow-up
- Add a
multiple={false}mode where opening one section closes the others. - Make it controlled (
value/onChange) as well as uncontrolled. - How would you animate the height without layout thrash?
Spotted a mistake or a rights concern? Report an issue