Toggle theme
@volvo-cars/react-accordion
An Accordion is a content area which can be collapsed and expanded. It can be used to group or hide complex regions to keep the page clean. This Accordion component is a lightweight container that may either stand alone or be connected to other Accordions.
💡 This package includes Typescript definitions
Accordion 1
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse malesuada lacus ex, sit amet blandit leo lobortis eget.
Accordion 2
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse malesuada lacus ex, sit amet blandit leo lobortis eget.
Accordion 3
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse malesuada lacus ex, sit amet blandit leo lobortis eget.
The above example shows independent accordions using AccordionSummary
which controls the expanded state and AccordionDetails
that houses the content of the Accordion
.
By wrapping a set of Accordions with an AccordionController
we can control how accordions respond to other accordions expanded or collapse state.
Accordion 1
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse malesuada lacus ex, sit amet blandit leo lobortis eget.
Accordion 2
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse malesuada lacus ex, sit amet blandit leo lobortis eget.
Accordion 3
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse malesuada lacus ex, sit amet blandit leo lobortis eget.
Notice how only one Accordion is open at a time in the above example using AccordionController
, the multiple
prop allows to override this behaviour.
Specify which Accordions to be expanded by default.
Accordion 1
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse malesuada lacus ex, sit amet blandit leo lobortis eget.
Accordion 2
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse malesuada lacus ex, sit amet blandit leo lobortis eget.
Accordion 3
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse malesuada lacus ex, sit amet blandit leo lobortis eget.
Extend the default behaviour and control expanded and collapsed state yourself.
Accordion 1
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse malesuada lacus ex, sit amet blandit leo lobortis eget.
Accordion 2
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse malesuada lacus ex, sit amet blandit leo lobortis eget.
Accordion 3
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse malesuada lacus ex, sit amet blandit leo lobortis eget.
In order to put an action such as a Checkbox or a button inside of the AccordionSummary
, you can use the AccordionAction
component that stops propagation of click and Enter/Spacebar events to the parent AccordionSummary
.
⚠️ Note that when using the AccordionAction
, some accessibility issues arise mainly due to nested interactive elements, as those are ignored by screen readers. You are free to change the role
prop on the AccordionSummary
to decided which elements are favored by screen readers.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse malesuada lacus ex, sit amet blandit leo lobortis eget.
Accordion 2
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse malesuada lacus ex, sit amet blandit leo lobortis eget.
Apart from onChange
which is called when the Accordion is about to change state, there is a onAnimationEnd
callback function similar to onChange
but will only be called upon animation finish.
Accordion 1
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse malesuada lacus ex, sit amet blandit leo lobortis eget.
The content of the Accordion is rendered by default even when collapsed, which is useful if it's required for SEO purposes. But if the content of the Accordion is expensive to render, it's possible to unmount it when collapsed using the lazyRenderDetails
prop on the Accordion component.
If you have child elements that are expensive to render, and want to decrease the number of unnecessary re-renders in the Accordion component, it's recommended to wrap your callback function with a useCallback
instead of passing a new function on every render.
The Accordion components are accessible by default, they add necessary roles, aria labels, keyboard interaction with Enter and Space as well as tab focus following WAI:ARIA
Accordion
🔗Name | Description | Type | Default Value |
---|---|---|---|
defaultExpanded | Accordion should be expanded by default | boolean | undefined |
onChange | Fires when expanded state changes | (isExpanded:boolean) => void | undefined |
lazyRenderDetails | Unmounts AccordionDetails content when collapsed | boolean | undefined |
expanded | Manually control expanded state | boolean | undefined |
hideDivider | Force hides divider that appears when more that one Accordion are rendered | boolean | undefined |
onAnimationEnd | Fires when expanded state changes and animation has finished | (isVisible?: boolean) => void | undefined |
className | Class name to be passed to Accordion wrapper | string | undefined |
id | Id to be passed to Accordion wrapper | string | undefined |
extend | Extends Accordion wrapper styles | {} ()=>{} [] | undefined |
AccordionController
🔗Name | Description | Type | Default Value |
---|---|---|---|
multiple | When false , only one Accordion opens at a time | boolean | false |
AccordionSummary
🔗Name | Description | Type | Default Value |
---|---|---|---|
onInteraction | Fires when the user interacts with the AccordionSummary , either Click, Enter or Space | ({event?: SyntheticEvent, expanded?:boolean}) => void | undefined |
role | Changes the role of the AccordionSummary wrapper | string | button |
className | Class name to be passed to AccordionSummary wrapper | string | undefined |
extend | Extends AccordionSummary wrapper styles | {} ()=>{} [] | undefined |
iconAlignment | Vertical alignment of the AccordionSummary icon | top \| center | top |
AccordionDetails
🔗Name | Description | Type | Default Value |
---|---|---|---|
className | Class name to be passed to AccordionDetails wrapper | string | undefined |
extend | Extends AccordionDetails wrapper styles | {} ()=>{} [] | undefined |
AccordionAction
🔗Name | Description | Type | Default Value |
---|---|---|---|
className | Class name to be passed to AccordionAction wrapper | string | undefined |
extend | Extends AccordionAction wrapper styles | {} ()=>{} [] | undefined |
id | Id to be passed to AccordionAction wrapper | string | undefined |