Accordion
A collapsible component for displaying content in a vertical stack.
Anatomy
<Accordion.Root>
<Accordion.Item>
<Accordion.ItemTrigger>
<Accordion.ItemIndicator />
</Accordion.ItemTrigger>
<Accordion.ItemContent />
</Accordion.Item>
</Accordion.Root>
Examples
Default Value
Set the defaultValue prop to specify which item should be expanded by default.
Controlled
Use the value and onValueChange props to control the expanded items.
Root Provider
An alternative way to control the accordion is to use the RootProvider component and the useAccordion hook. This way
you can access the state and methods from outside the component.
Collapsible
Use the collapsible prop to allow the user to collapse all panels.
Multiple
Use the multiple prop to allow multiple panels to be expanded simultaneously.
Horizontal
By default, the Accordion is oriented vertically. Use the orientation prop to switch to a horizontal layout.
Lazy Mount
Use the lazyMount prop to defer rendering of accordion content until the item is expanded. Combine with
unmountOnExit to unmount content when collapsed, freeing up resources.
Context
Use Accordion.Context or useAccordionContext to access the accordion state.
Item State
Use Accordion.ItemContext or useAccordionItemContext to access the state of an accordion item.
Guides
Content Animation
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
Renders a <div> element.
| 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. |
| Attribute | Description |
|---|---|
[data-scope] | accordion |
[data-part] | root |
[data-orientation] | The orientation of the accordion |
ItemContent
Renders a <div> element.
| 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. |
| Attribute | Description |
|---|---|
[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
Renders a <div> element.
| 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. |
| Attribute | Description |
|---|---|
[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
Renders a <div> element.
| 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. |
| Attribute | Description |
|---|---|
[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
Renders a <button> element.
| 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. |
| Attribute | Description |
|---|---|
[data-scope] | accordion |
[data-part] | item-trigger |
[data-orientation] | The orientation of the item |
[data-state] | "open" | "closed" |
RootProvider
Renders a <div> element.
| 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
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. |