Accordion II: Single Open, Controlled
UI Coding
Medium
25 min
accessibility
design-system
state
controlled-components
Build an accordion where at most one section is open at a time, as in an FAQ page. The starter renders the sections with every panel visible and no interactivity.
Requirements
- Opening a section closes whichever section was open.
- A
collapsibleprop (defaulttrue) controls whether clicking the open section closes it. Withcollapsible={false}, one section always stays open. Its header then getsaria-disabled="true", because activating it does nothing. - Support uncontrolled use (
defaultValue) and controlled use (value+onValueChange). Withvalueset, the accordion never changes on its own. It callsonValueChange(next)and renders whatevervaluethe parent passes back. - Accessibility as in the WAI-ARIA Accordion pattern: header
<button>s inside headings witharia-expandedandaria-controls, panels withrole="region"andaria-labelledby, and collapsed panelshidden. - <kbd>↑</kbd>/<kbd>↓</kbd>/<kbd>Home</kbd>/<kbd>End</kbd> move focus between headers.
- The demo
Appshows a controlled accordion plus a line of text: "Open section: …" (or "none").
Follow-up
- How does the native
<details name="faq">exclusive accordion compare? - How do you warn when a component switches between controlled and uncontrolled?
- How would you animate one panel closing while another opens?
Spotted a mistake or a rights concern? Report an issue