Accordion

Expandable content sections. Accessible and keyboard navigable by default.


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 allowsMultipleExpanded prop on the AccordionGroup component.

<AccordionGroup allowsMultipleExpanded>...</AccordionGroup>

Controlled Accordion

  • You can control the expanded state of the accordion items by using the expandedItems and onExpandedItemsChange props on the AccordionGroup component.
  • By adding an id prop to each Accordion component, 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 Librariesversion
react-aria-components^1.18.0

The following folders need to be copied to your project:

Copy to projectReason
ui/components/accordion/Accordion components and hooks
ui/primatives/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
PropTypeDescription
allowsMultipleExpandedbooleanAllows multiple accordion items to be expanded at the same time
expandedKeysstring[]A set of accordion IDs that are currently expanded. This is used to control the expanded state of the accordion items.
onExpandedChangefunctionA 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 AccordionGroup component.
  • It is based on the <AriaDisclosure /> component from react-aria-components.
Props
PropTypeDescription
idstringA 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 Accordion component.
  • 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 corresponding AccordionPanel for accessibility purposes.
  • It also uses the <DisclosureTrigger /> component, which uses the <Button /> component from react-aria-components to toggle the expanded state of the accordion item.
  • Here you can change the icon of the accordion.
Props
PropTypeDescription
@extendsComponentProps<'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 Accordion component.
  • 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 corresponding AccordionHeader for accessibility purposes.
Props
PropTypeDescription
@extendsComponentProps<'section'>All props from the <section> element are accepted.
Data attributes
Data attributeTypeDescription
data-state'open' | 'closed'The state of the accordion panel.