Skip to main content

Accordion

The Accordion component in Pearl UI is a versatile and customizable element used to display collapsible content panels for presenting information in a limited amount of space.

Import#

Pearl UI provides four components related to the Accordion functionality:

  1. Accordion: This is the parent component that acts as a container for multiple AccordionItem components.
  2. AccordionItem: This component represents a single collapsible content panel within the Accordion.
  3. AccordionButton: This component controls the visibility of its corresponding AccordionPanel.
  4. AccordionPanel: This component contains the content that is shown or hidden when the AccordionButton is clicked.
import {  Accordion,  AccordionItem,  AccordionButton,  AccordionPanel,} from "pearl-ui";

Usage#

<Accordion>  <AccordionItem>    <AccordionButton>      <Text>Accordion Section 1</Text>    </AccordionButton>    <AccordionPanel>      <Text>        This is the content for the first section of the accordion. It can be        replaced with any content you like.      </Text>    </AccordionPanel>  </AccordionItem>
  <AccordionItem>    <AccordionButton>      <Text>Accordion Section 2</Text>    </AccordionButton>    <AccordionPanel>      <Text>        This is the content for the second section of the accordion. It can be        replaced with any content you like.      </Text>    </AccordionPanel>  </AccordionItem></Accordion>

Accordion with multiple items expanded#

The allowMultiple prop allows you to have multiple accordion items expanded at once.

<Accordion allowMultiple>  <AccordionItem>    <AccordionButton>      <Text>Accordion Section 1</Text>    </AccordionButton>    <AccordionPanel>      <Text>        This is the content for the first section of the accordion. It can be        replaced with any content you like.      </Text>    </AccordionPanel>  </AccordionItem>
  <AccordionItem>    <AccordionButton>      <Text>Accordion Section 2</Text>    </AccordionButton>    <AccordionPanel>      <Text>        This is the content for the second section of the accordion. It can be        replaced with any content you like.      </Text>    </AccordionPanel>  </AccordionItem></Accordion>

Accordion with toggle#

The allowToggle prop allows any expanded accordion item to be collapsed again.

<Accordion allowToggle>  <AccordionItem>    <AccordionButton>      <Text>Accordion Section 1</Text>    </AccordionButton>    <AccordionPanel>      <Text>        This is the content for the first section of the accordion. It can be        replaced with any content you like.      </Text>    </AccordionPanel>  </AccordionItem>
  <AccordionItem>    <AccordionButton>      <Text>Accordion Section 2</Text>    </AccordionButton>    <AccordionPanel>      <Text>        This is the content for the second section of the accordion. It can be        replaced with any content you like.      </Text>    </AccordionPanel>  </AccordionItem></Accordion>

Accordion with initial index#

The defaultIndices prop allows you to specify the initial index(es) of the expanded accordion item.

<Accordion defaultIndices={[1]}>  <AccordionItem>    <AccordionButton>      <Text>Accordion Section 1</Text>    </AccordionButton>    <AccordionPanel>      <Text>        This is the content for the first section of the accordion. It can be        replaced with any content you like.      </Text>    </AccordionPanel>  </AccordionItem>
  <AccordionItem>    <AccordionButton>      <Text>Accordion Section 2</Text>    </AccordionButton>    <AccordionPanel>      <Text>        This is the content for the second section of the accordion. It can be        replaced with any content you like.      </Text>    </AccordionPanel>  </AccordionItem></Accordion>

Accordion with reduced motion#

The reduceMotion prop allows you to disable height animation and transitions.

<Accordion reduceMotion>  <AccordionItem>    <AccordionButton>      <Text>Accordion Section 1</Text>    </AccordionButton>    <AccordionPanel>      <Text>        This is the content for the first section of the accordion. It can be        replaced with any content you like.      </Text>    </AccordionPanel>  </AccordionItem>
  <AccordionItem>    <AccordionButton>      <Text>Accordion Section 2</Text>    </AccordionButton>    <AccordionPanel>      <Text>        This is the content for the second section of the accordion. It can be        replaced with any content you like.      </Text>    </AccordionPanel>  </AccordionItem></Accordion>

Accessing the internal state#

If you need access to the internal state of each accordion item, you can use a render prop. It provides 2 internal state props: isExpanded and isDisabled.

Accordion Section 1
This is the content for the first section of the accordion. It can be replaced with any content you like.

Example#

Accessibility#

  • AccordionButton has the role of button.
  • When the AccordionButton is pressed, it controls the visibility of the AccordionPanel.

Accordion Component Properties#

Supported Style Properties#

The Accordion component is a composition of the Stack component. Therefore, all properties related to the Stack component can be passed to the Accordion component, in addition to the properties listed below.

Additional Properties#

Property NameRequiredData TypeDefaultDescription
sizeNoPearlTheme.components.Accordion["sizes"]Determines the size of the accordion.
variantNoPearlTheme.components.Accordion["variants"]Determines the variant of the accordion.
allowMultipleNobooleanfalseIf true, multiple accordion items can be expanded at once.
allowToggleNobooleanfalseIf true, any expanded accordion item can be collapsed again.
defaultIndicesNonumber[][]The initial index(es) of the expanded accordion item.
reduceMotionNobooleanfalseIf true, height animation and transitions will be disabled.

AccordionItem Component Properties#

Supported Style Properties#

The AccordionItem component is a composition of the Stack component. Therefore, all properties related to the Stack component can be passed to the AccordionItem component, in addition to the properties listed below.

Additional Properties#

Property NameRequiredData TypeDefaultDescription
indexNonumber0The index of the accordion item.
childrenNoReactNode or ((isExpanded: boolean) => ReactNode)nullThe children of the accordion item, which can be a function. If it is a function, it receives the expanded state of the accordion item as an argument.

AccordionButton Component Properties#

Supported Style Properties#

The AccordionButton component is a composition of the Pressable component. Therefore, all properties related to the Pressable component can be passed to the AccordionButton component, in addition to the properties listed below.

Additional Properties#

Property NameRequiredData TypeDefaultDescription
iconNoReact.ReactElementnullIcon to display on the right side of the accordion button.
onPressNo(index: number) => voidnullA function that is called when the accordion item is pressed. It receives the index of the accordion item as an argument.

AccordionPanel Component Properties#

Supported Style Properties#

The AccordionPanel component is a composition of the Collapse component. Therefore, all properties related to the Collapse component can be passed to the AccordionPanel component, in addition to the properties listed below.