Accordion
A collapsible component for displaying content in a vertical stack.
Features
- Full keyboard navigation
- Supports horizontal and vertical orientation
- Right-to-Left (RTL) support
- Single or multiple item expansion
- Controlled and uncontrolled modes
- Collapse each accordion item
Anatomy
To set up the accordion correctly, it's essential to understand its anatomy and the naming of its parts.
Each part includes a
data-partattribute to help identify them in the DOM.
Examples
Default Expanded State
Set the defaultValue prop to specify which item should be expanded by default.
Example not foundExample not foundExample not foundExample not foundCollapsible
Use the collapsible prop to allow the user to collapse all panels.
Example not foundExample not foundExample not foundExample not foundMultiple Panels
Use the multiple prop to allow multiple panels to be expanded simultaneously.
Example not foundExample not foundExample not foundExample not foundHorizontal Orientation
By default, the Accordion is oriented vertically. Use the orientation prop to switch to a horizontal layout.
Example not foundExample not foundExample not foundExample not foundUsing the Root Provider
The RootProvider component provides a context for the accordion. It accepts the value of the useAccordion hook. You
can leverage it to access the component state and methods from outside the accordion.
Example not foundExample not foundExample not foundExample not foundIf you're using the
RootProvidercomponent, you don't need to use theRootcomponent.
Accessing context
Use the Accordion.Context component or useAccordionContext hook to access the state of an accordion. It exposes the
following properties:
focusedValue: The value of the focused accordion item.value: The value of the selected accordion items.setValue: A method to set the selected accordion items.
Example not foundExample not foundExample not foundExample not foundAccessing the item context
Use the Accordion.ItemContext component or useAccordionItemContext hook to access the state of an accordion item. It
exposes the following properties:
expanded: Whether the accordion item is expanded.focused: Whether the accordion item is focused.disabled: Whether the accordion item is disabled.
Example not foundExample not foundExample not foundExample not foundGuides
Animate Content Size
Use the --height and/or --width CSS variables to animate the size of the content when it expands or closes:
@keyframes slideDown {
from {
opacity: 0.01;
height: 0;
}
to {
opacity: 1;
height: var(--height);
}
}
@keyframes slideUp {
from {
opacity: 1;
height: var(--height);
}
to {
opacity: 0.01;
height: 0;
}
}
[data-scope='accordion'][data-part='item-content'][data-state='open'] {
animation: slideDown 250ms ease-in-out;
}
[data-scope='accordion'][data-part='item-content'][data-state='closed'] {
animation: slideUp 200ms ease-in-out;
}
API Reference
Props
Root
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
collapsible | false | booleanWhether an accordion item can be closed after it has been expanded. |
defaultValue | string[]The initial value of the expanded accordion items. Use when you don't need to control the value of the accordion. | |
disabled | booleanWhether the accordion items are disabled | |
id | stringThe unique identifier of the machine. | |
ids | Partial<{
root: string
item: (value: string) => string
itemContent: (value: string) => string
itemTrigger: (value: string) => string
}>The ids of the elements in the accordion. Useful for composition. | |
lazyMount | false | booleanWhether to enable lazy mounting |
multiple | false | booleanWhether multiple accordion items can be expanded at the same time. |
onFocusChange | (details: FocusChangeDetails) => voidThe callback fired when the focused accordion item changes. | |
onValueChange | (details: ValueChangeDetails) => voidThe callback fired when the state of expanded/collapsed accordion items changes. | |
orientation | 'vertical' | 'horizontal' | 'vertical'The orientation of the accordion items. |
unmountOnExit | false | booleanWhether to unmount on exit. |
value | string[]The controlled value of the expanded accordion items. |
| Data Attribute | Value |
|---|---|
[data-scope] | accordion |
[data-part] | root |
[data-orientation] | The orientation of the accordion |
ItemContent
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
| Data Attribute | Value |
|---|---|
[data-scope] | accordion |
[data-part] | item-content |
[data-state] | "open" | "closed" |
[data-disabled] | Present when disabled |
[data-focus] | Present when focused |
[data-orientation] | The orientation of the item |
ItemIndicator
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
| Data Attribute | Value |
|---|---|
[data-scope] | accordion |
[data-part] | item-indicator |
[data-state] | "open" | "closed" |
[data-disabled] | Present when disabled |
[data-focus] | Present when focused |
[data-orientation] | The orientation of the item |
Item
| Prop | Default | Type |
|---|---|---|
value | stringThe value of the accordion item. | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
disabled | booleanWhether the accordion item is disabled. |
| Data Attribute | Value |
|---|---|
[data-scope] | accordion |
[data-part] | item |
[data-state] | "open" | "closed" |
[data-focus] | Present when focused |
[data-disabled] | Present when disabled |
[data-orientation] | The orientation of the item |
ItemTrigger
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
| Data Attribute | Value |
|---|---|
[data-scope] | accordion |
[data-part] | item-trigger |
[data-orientation] | The orientation of the item |
[data-state] | "open" | "closed" |
RootProvider
| Prop | Default | Type |
|---|---|---|
value | UseAccordionReturn | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
lazyMount | false | booleanWhether to enable lazy mounting |
unmountOnExit | false | booleanWhether to unmount on exit. |
Context
These are the properties available when using Accordion.Context, useAccordionContext hook or useAccordion hook.
API
| Property | Type |
|---|---|
focusedValue | stringThe value of the focused accordion item. |
value | string[]The value of the accordion |
setValue | (value: string[]) => voidSets the value of the accordion |
getItemState | (props: ItemProps) => ItemStateReturns the state of an accordion item. |
Accessibility
This component complies with the Accordion WAI-ARIA design pattern.
Keyboard Support
| Key | Description |
|---|---|
Space | When focus is on an trigger of a collapsed item, the item is expanded |
Enter | When focus is on an trigger of a collapsed section, expands the section. |
Tab | Moves focus to the next focusable element |
Shift + Tab | Moves focus to the previous focusable element |
ArrowDown | Moves focus to the next trigger |
ArrowUp | Moves focus to the previous trigger. |
Home | When focus is on an trigger, moves focus to the first trigger. |
End | When focus is on an trigger, moves focus to the last trigger. |