Accordion

Accordions display a list of high-level options that can expand/collapse to reveal more information.

Import#

Chakra UI exports 5 accordion related components.

  • Accordion: The wrapper that uses cloneElement to pass it's prop to the AccordionItem.
  • AccordionItem: A single accordion item.
  • AccordionHeader: The button that toggles the expand/collapse state of the accordion item.
  • AccordionPanel: The container for the details to be revealed.
  • AccordionIcon: A chevron-down icon that rotates based on the expanded/collapsed state

Usage#

By default, only one item may be expanded and it can only be collapsed again by expanding another.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Editable Example

Expand multiple items at once#

If you set allowMultiple to true then the accordion will permit multiple items to be expanded at once.

If you pass this prop, ensure that the index or defaultIndex prop is an array.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Editable Example

Toggle each accordion item#

If you set allowToggle to true, any expanded item may be collapsed again.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Editable Example

Styling the expanded state#

The AccordionHeader component has aria-expanded set to true or false depending on the state of the AccordionItem. Use the style prop _expanded to style this state.

Editable Example

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 isExpanded, and isDisabled.

Editable Example

Accessibility#

Pressing space or enter when focus is on the accordion panel header will toggle (expand or collapse) the accordion.

Props#

Accordion Props#

NameTypeDefaultDescription
allowMultiplebooleanIf true, multiple accordion items can be expanded at once.
allowTogglebooleanIf true, any expanded accordion item can be collapsed again.
indexNumberOrArrayOfNumberThe index(es) of the expanded accordion item
defaultIndexNumberOrArrayOfNumberThe initial index(es) of the expanded accordion item
onChange(NumberOrArrayOfNumber) => voidThe callback invoked when accordion items are expanded or collapsed.
childrenReactNodeThe content of the accordion. The children must be the AccordionHeader and AccordionPanel components.

AccordionItem Props#

NameTypeDefaultDescription
isOpenbooleanIf true, expands the accordion in the controlled mode.
defaultIsOpenbooleanIf true, expands the accordion by on initial mount.
idstringA unique id for the accordion item.
isDisabledbooleanIf true, the accordion header will be disabled.
onChange(NumberOrArrayOfNumber) => voidThe callback fired when the accordion is expanded/collapsed.
childrenReactNode, (RenderProps) => ReactNodeThe content of the accordion. The children must be the AccordionHeader and AccordionPanel components.

AccordionHeader Props#

AccordionHeader renders a button and composes PseudoBox, this means you can style it by passing the pseudo style props _expanded, _disabled, _hover, etc.

AccordionPanel Props#

AccordionPanel renders a div and composes Collapse to provide the height animation.

Proudly made in 🇳🇬

Released under the MIT License.

Copyright © 2020 Segun Adebayo