An accordion is a container for multiple accordion items.
density
Expanding
Use the defaultExpandedKeys or expandedKeys prop to set the expanded items, and onExpandedChange to handle user interactions. The expanded keys correspond to the id prop of each <AccordionItem>.
import {Accordion, AccordionItem, AccordionItemTitle, AccordionItemPanel, type Key} from '@react-spectrum/s2';
import {useState} from 'react';
function Example() {
let [expandedKeys, setExpandedKeys] = useState(new Set<Key>(['settings']));
return (
<Accordion
expandedKeys={expandedKeys}
onExpandedChange={setExpandedKeys}>
<AccordionItem id="settings">
<AccordionItemTitle>Settings</AccordionItemTitle>
<AccordionItemPanel>Application settings content</AccordionItemPanel>
</AccordionItem>
<AccordionItem id="preferences">
<AccordionItemTitle>Preferences</AccordionItemTitle>
<AccordionItemPanel>User preferences content</AccordionItemPanel>
</AccordionItem>
<AccordionItem id="advanced">
<AccordionItemTitle>Advanced</AccordionItemTitle>
<AccordionItemPanel>Advanced configuration options</AccordionItemPanel>
</AccordionItem>
</Accordion>
);
}
Content
Use AccordionItemHeader to add additional elements alongside the title, such as action buttons or icons.
import {Accordion, AccordionItem, AccordionItemTitle, AccordionItemPanel, AccordionItemHeader, ActionButton} from '@react-spectrum/s2';
import {style} from '@react-spectrum/s2/style' with {type: 'macro'};
<Accordion>
<AccordionItem styles={style({width: 250})}>
<AccordionItemHeader>
<AccordionItemTitle>Project Settings</AccordionItemTitle>
<ActionButton>Edit</ActionButton>
</AccordionItemHeader>
<AccordionItemPanel>
Configure your project settings including name, description, and permissions.
</AccordionItemPanel>
</AccordionItem>
<AccordionItem id="preferences">
<AccordionItemTitle>Preferences</AccordionItemTitle>
<AccordionItemPanel>User preferences content</AccordionItemPanel>
</AccordionItem>
</Accordion>
API
<Accordion>
<AccordionItem>
<AccordionItemHeader>
<AccordionItemTitle />
</AccordionItemHeader>
<AccordionItemPanel />
</AccordionItem>
</Accordion>
Accordion
| Name | Type | Default |
|---|---|---|
children | React.ReactNode | Default: — |
| The accordion item elements in the accordion. | ||
styles | StylesPropWithHeight | Default: — |
Spectrum-defined styles, returned by the style() macro. | ||
size | 'S'
| 'M'
| 'L'
| 'XL' | Default: 'M'
|
| The size of the accordion. | ||
density | 'compact'
| 'regular'
| 'spacious' | Default: 'regular'
|
| The amount of space between the accordion items. | ||
isQuiet | boolean | Default: — |
| Whether the accordion should be displayed with a quiet style. | ||
allowsMultipleExpanded | boolean | Default: — |
| Whether multiple accordion items can be expanded at the same time. | ||
isDisabled | boolean | Default: — |
| Whether all accordion items are disabled. | ||
expandedKeys | Iterable | Default: — |
| The currently expanded keys in the accordion (controlled). | ||
defaultExpandedKeys | Iterable | Default: — |
| The initial expanded keys in the accordion (uncontrolled). | ||
AccordionItem
A accordion item is a collapsible section of content. It is composed of a header with a heading and trigger button, and a panel that contains the content.
| Name | Type | Default |
|---|---|---|
size | 'S'
| 'M'
| 'L'
| 'XL' | Default: 'M'
|
| The size of the accordion item. | ||
density | 'compact'
| 'regular'
| 'spacious' | Default: 'regular'
|
| The amount of space between the accordion item. | ||
isQuiet | boolean | Default: — |
| Whether the accordion item should be displayed with a quiet style. | ||
children | ReactNode | Default: — |
| The contents of the accordion item, consisting of a accordion item title and accordion item panel. | ||
id | Key | Default: — |
An id for the accordion item, matching the id used in expandedKeys. | ||
isDisabled | boolean | Default: — |
| Whether the accordion item is disabled. | ||
isExpanded | boolean | Default: — |
| Whether the accordion item is expanded (controlled). | ||
defaultExpanded | boolean | Default: — |
| Whether the accordion item is expanded by default (uncontrolled). | ||
styles | StylesProp | Default: — |
Spectrum-defined styles, returned by the style() macro. | ||
AccordionItemHeader
A wrapper element for the accordion item title that can contain other elements not part of the trigger.
| Name | Type | |
|---|---|---|
children | React.ReactNode | |
| The contents of the accordion item header. | ||
AccordionItemTitle
An accordion item title consisting of a heading and a trigger button to expand/collapse the panel.
| Name | Type | Default |
|---|---|---|
level | number | Default: 3
|
| The heading level of the accordion item title. | ||
children | React.ReactNode | Default: — |
| The contents of the accordion item title. | ||
AccordionItemPanel
An accordion item panel is a collapsible section of content that is hidden until the accordion item is expanded.
| Name | Type | |
|---|---|---|
children | React.ReactNode | |
| The contents of the accordion item panel. | ||