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.
#
ImportPearl UI provides four components related to the Accordion functionality:
Accordion
: This is the parent component that acts as a container for multipleAccordionItem
components.AccordionItem
: This component represents a single collapsible content panel within theAccordion
.AccordionButton
: This component controls the visibility of its correspondingAccordionPanel
.AccordionPanel
: This component contains the content that is shown or hidden when theAccordionButton
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 expandedThe 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 toggleThe 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 indexThe 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 motionThe 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 stateIf 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.
#
Example#
AccessibilityAccordionButton
has therole
ofbutton
.- When the
AccordionButton
is pressed, it controls the visibility of theAccordionPanel
.
#
Accordion Component Properties#
Supported Style PropertiesThe 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 PropertiesProperty Name | Required | Data Type | Default | Description |
---|---|---|---|---|
size | No | Determines the size of the accordion. | ||
variant | No | Determines the variant of the accordion. | ||
allowMultiple | No | false | If true, multiple accordion items can be expanded at once. | |
allowToggle | No | false | If true, any expanded accordion item can be collapsed again. | |
defaultIndices | No | [] | The initial index(es) of the expanded accordion item. | |
reduceMotion | No | false | If true, height animation and transitions will be disabled. |
#
AccordionItem Component Properties#
Supported Style PropertiesThe 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 PropertiesProperty Name | Required | Data Type | Default | Description |
---|---|---|---|---|
index | No | 0 | The index of the accordion item. | |
children | No | null | The 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 PropertiesThe 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 PropertiesProperty Name | Required | Data Type | Default | Description |
---|---|---|---|---|
icon | No | null | Icon to display on the right side of the accordion button. | |
onPress | No | null | A 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 PropertiesThe 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.