Accordion
A vertically stacked set of interactive headings that reveal or hide associated content sections.
Installation
npx vayu-ui-cli@latest add accordionUsage
Accordion Example
Multiple Items Can Be Open
Anatomy
import { Accordion } from 'vayu-ui';
<Accordion aria-labelledby="your-label">
<Accordion.Item itemId="unique-id">
<Accordion.Header itemId="unique-id">Section Title</Accordion.Header>
<Accordion.Body itemId="unique-id">Section content goes here.</Accordion.Body>
</Accordion.Item>
</Accordion>;- Accordion — Root container. Manages open/close state and provides context to children.
- Accordion.Item — Wrapper for a single accordion section. Registers the item for keyboard navigation.
- Accordion.Header — Clickable button trigger. Displays the section title with a chevron indicator.
- Accordion.Body — Collapsible content panel with smooth height animation.
Accessibility
- Keyboard Support:
Enter/Space— Toggle the focused section open or closed.ArrowDown— Move focus to the next header.ArrowUp— Move focus to the previous header.Home— Move focus to the first header.End— Move focus to the last header.Escape— Close the currently open section.
- ARIA Attributes:
aria-expandedonAccordion.Headerreflects the open/closed state.aria-controlsonAccordion.Headerlinks to the associated panel.aria-labelledbyonAccordion.Bodyreferences the controlling header.aria-hiddenonAccordion.Bodyprevents hidden content from being read.- Chevron icon is marked
aria-hidden="true".
- Focus Behavior:
- Focus moves to the first focusable element inside the panel when a section expands.
- When a section collapses, focus returns to its header button if the panel had focus.
Screen reader behavior
When a user navigates to an accordion header, the screen reader announces the button text, its expanded or collapsed state (via aria-expanded), and the number of items in the accordion group. When a section is expanded, the panel content is announced (the panel has role="region" with aria-labelledby linking back to the header). When collapsed, the panel content is hidden from the assistive technology tree (aria-hidden="true"). Users can navigate between headers using arrow keys, and the screen reader will announce each header as it receives focus.
Component Folder Structure
Accordion/
├── Accordion.tsx # Root component with context provider and state management
├── AccordionItem.tsx # Item container with registration for keyboard navigation
├── AccordionHeader.tsx # Clickable header trigger with keyboard navigation
├── AccordionBody.tsx # Collapsible panel with animated height transitions
├── types.ts # TypeScript type definitions
├── index.ts # Re-exports all components and types
└── README.md # Component usage referenceProps
Accordion (Root)
| Prop | Type | Default | Description |
|---|---|---|---|
allowMultiple | boolean | false | Allow multiple items to be open simultaneously. |
children | ReactNode | — | Accordion.Item elements. |
className | string | — | Additional CSS classes. |
Accordion.Item
| Prop | Type | Default | Description |
|---|---|---|---|
itemId | string | — | Unique identifier for the item. Required. |
children | ReactNode | — | Accordion.Header and Accordion.Body. |
className | string | — | Additional CSS classes. |
Accordion.Header
| Prop | Type | Default | Description |
|---|---|---|---|
itemId | string | — | Unique identifier matching the parent Accordion.Item. Required. |
children | ReactNode | — | Header label content. |
className | string | — | Additional CSS classes. |
Accordion.Body
| Prop | Type | Default | Description |
|---|---|---|---|
itemId | string | — | Unique identifier matching the parent Accordion.Item. Required. |
children | ReactNode | — | Collapsible content. |
className | string | — | Additional CSS classes. |