These accordion components are a very thin wrapper on top of the <DisclosureGroup /> and <Disclosure /> components from
react-aria-components.
For more usage details, checkout their docs:
Examples
Single Expanded Item
<AccordionGroup>
<Accordion>
<AccordionHeader>What is the return policy</AccordionHeader>
<AccordionPanel>
<p>...</p>
</AccordionPanel>
</Accordion>
<Accordion>
<AccordionHeader>Do you offer customer support?</AccordionHeader>
<AccordionPanel>
<p>...</p>
</AccordionPanel>
</Accordion>
</AccordionGroup>Multiple Expanded Items At A Time
- You can allow multiple accordion items to be expanded at the same time by setting the
allowsMultipleExpandedprop on theAccordionGroupcomponent.
<AccordionGroup allowsMultipleExpanded>...</AccordionGroup>Controlled Accordion
- You can control the expanded state of the accordion items by using the
expandedItemsandonExpandedItemsChangeprops on theAccordionGroupcomponent. - By adding an
idprop to eachAccordioncomponent, you can specify which items are expanded by default and control the expanded state programmatically.
We offer a 30-day return policy for all products.
const [expandedKeys, setExpandedKeys] = useState(['accordion-1']);
return (
<AccordionGroup
expandedKeys={expandedKeys}
onExpandedChange={setExpandedKeys}
>
<Accordion id="accordion-1">
<AccordionHeader>What is the return policy</AccordionHeader>
<AccordionPanel>
<p>...</p>
</AccordionPanel>
</Accordion>
<Accordion id="accordion-2">
<AccordionHeader>Do you offer customer support?</AccordionHeader>
<AccordionPanel>
<p>...</p>
</AccordionPanel>
</Accordion>
<Accordion id="accordion-3">
<AccordionHeader>How long does shipping take</AccordionHeader>
<AccordionPanel>
<p>...</p>
</AccordionPanel>
</Accordion>
</AccordionGroup>
);Requirements
The following libraries need to be installed:
| 3rd Party Libraries | version |
|---|---|
react-aria-components | ^1.18.0 |
The following folders need to be copied to your project:
| Copy to project | Reason |
|---|---|
/components/accordion/ | Accordion components and hooks |
/components/disclosure/ | The accordion uses the disclosure primitive |
Components
<AccordionGroup />
The <AccordionGroup /> component is the parent component that wraps around all
Accordion components. It is based on the <AriaDisclosureGroup /> component
from react-aria-components.
Props
| Prop | Type | Description |
|---|---|---|
allowsMultipleExpanded | boolean | Allows multiple accordion items to be expanded at the same time |
expandedKeys | string[] | A set of accordion IDs that are currently expanded. This is used to control the expanded state of the accordion items. |
onExpandedChange | function | A callback function that is called when the expanded state of the accordion items changes. It receives the new array of expanded accordion IDs as an argument. |
<Accordion />
The <Accordion /> component represents a single accordion item.
- It should be wrapped inside an
AccordionGroupcomponent. - It is based on the
<AriaDisclosure />component fromreact-aria-components.
Props
| Prop | Type | Description |
|---|---|---|
id | string | A unique identifier for the accordion item. This is used to control the expanded state of the accordion item when using a controlled accordion. |
<AccordionHeader />
The <AccordionHeader /> component represents the header of the accordion item.
- It should be wrapped inside an
Accordioncomponent. - It is the element that the user clicks on to expand or collapse the accordion item.
- It makes use of a custom
<DisclosureHeader />component, which gives it an id to link it to the correspondingAccordionPanelfor accessibility purposes. - It also uses the
<DisclosureTrigger />component, which uses the<Button />component fromreact-aria-componentsto toggle the expanded state of the accordion item. - Here you can change the icon of the accordion.
Props
| Prop | Type | Description |
|---|---|---|
| @extends | ComponentProps<'h3'> | All props from the <h3> element are accepted. |
<AccordionPanel />
The <AccordionPanel /> component represents the content panel of the accordion item.
- It should be wrapped inside an
Accordioncomponent. - It is shown or hidden based on the expanded state of the accordion item.
- It makes use of a custom
<DisclosurePanel />component, which gives it an aria-labelledby attribute to link it to the correspondingAccordionHeaderfor accessibility purposes.
Props
| Prop | Type | Description |
|---|---|---|
| @extends | ComponentProps<'section'> | All props from the <section> element are accepted. |
Data attributes
| Data attribute | Type | Description |
|---|---|---|
data-state | 'open' | 'closed' | The state of the accordion panel. |