Skip to main content

Accordion: React

An accordion component is a list of items, allowing each item’s content to be expanded and collapsed by clicking its header.

Installation

Before you can start using the @mediahuis/chameleon-react component, you’ll need to install it, for more information check: Getting started for developers.

With that done, you’re now ready to implement the component in your application.

import { Accordion, AccordionItem } from '@mediahuis/chameleon-react';

export default function Example() {
return (
<Accordion>
<AccordionItem title="Item 1">Content of item 1</AccordionItem>
<AccordionItem title="Item 2">Content of item 2</AccordionItem>
</Accordion>
);
}

Accordion Properties

PropertyTypeDefaultRequiredDescription
collapsibleBoolean-NoWhether or not clicking an `AccordionItem` can collapse it again.
classNameString-NoAdditional class names to apply to the Accordion
idString-YesThe Accordion's id (Needed for a11y implementation)
initialExpandedIndexNumber-1NoTo specify a particular item to be in an open state on initial render, provide the index of the item to this prop. Use -1 to indicate that no items should be expanded initially.
onChangeFunction-NoThis callback function is triggered when an `AccordionItem` is expanded or collapsed. The function is passed the index of the item that has been interacted with.

AccordionItem Properties

PropertyTypeDefaultRequiredDescription
childrenReactNode-NoRepresents the body content of the `AccordionItem`. The content placed here will be visible when the item is expanded.
classNameString-NoAdditional class names to apply to the AccordionItem.
expandedBoolean-NoIndicates whether the panel body is visible or hidden.
focusedBoolean-NoWhen this is set to `true`, the component will automatically set focus on itself.
idString-NoA unique id for a11y reasons.
indexNumber-NoRepresents the index of the item within the Accordion component.
onClickFunction-NoThis event is fired when the header of the AccordionItem is clicked.
onFocusFunction-NoThis event is fired when the header of the AccordionItem receives focus.
onKeyDownFunction-NoThis event is fired when a key is pressed, typically used for arrow navigation within the AccordionItem.
titleString-NoRepresents the title within the header of the AccordionItem.