Accordion
Details about our Accordion component and how to use it.
- Makes use of
react-aria-componentsunder the hood, see here. It specifically uses the<DisclosureGroup />,<DisclosureStateContext />and<Disclosure />components. - Meets accessibility standards.
- Supports expanding one item at a time by default.
- Supports allowing multiple expanded items at the same time.
- Supports being fully controlled by the parent component.
One expanded item at a time
That's a really great question! We offer a 30-day return policy for all products.
Yes, we provide comprehensive customer support through multiple channels including email, live chat, and phone support during business hours (9 AM - 6 PM EST, Monday through Friday). Our support team is trained to help with product questions, technical issues, and order inquiries.
<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.
We offer a 30-day return policy for all products.
Yes, we provide comprehensive customer support through multiple channels including email, live chat, and phone support during business hours (9 AM - 6 PM EST, Monday through Friday). Our support team is trained to help with product questions, technical issues, and order inquiries.
<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.
Yes, we provide comprehensive customer support through multiple channels including email, live chat, and phone support during business hours (9 AM - 6 PM EST, Monday through Friday). Our support team is trained to help with product questions, technical issues, and order inquiries.
Standard shipping typically takes 3-5 business days within the continental United States. Express shipping options are available for 1-2 day delivery. International shipping times vary by destination but generally take 7-14 business days. You will receive a tracking number via email once your order has been processed and shipped.
const [expandedKeys, setExpandedKeys] = useState(
() => new Set<AccordionKey>(['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.14.0 |
The following folder need to be copied to your project:
| Copy to project | Reason |
|---|---|
ui/components/accordion/ | Accordion components and hooks |
ui/primatives/disclosure/ | The accordion uses the disclosure primitive |
ui/hooks/use-resize-observer.ts | Uses to track the height of the accordion panel for animation |
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 | Set<AccordionKey> | A set of accordion item keys 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 set of expanded item keys 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. |