Toggle theme

React Accordion🔗

@volvo-cars/react-accordion

@volvo-cars/react-accordion
@volvo-cars/react-accordion
@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.

Installation🔗

💡 This package includes Typescript definitions

Simple Accordion🔗

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse malesuada lacus ex, sit amet blandit leo lobortis eget.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse malesuada lacus ex, sit amet blandit leo lobortis eget.

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.

Connected Accordions🔗

By wrapping a set of Accordions with an AccordionController we can control how accordions respond to other accordions expanded or collapse state.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse malesuada lacus ex, sit amet blandit leo lobortis eget.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse malesuada lacus ex, sit amet blandit leo lobortis eget.

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 multipleprop allows to override this behaviour.

Default Expanded🔗

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.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse malesuada lacus ex, sit amet blandit leo lobortis eget.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse malesuada lacus ex, sit amet blandit leo lobortis eget.

Controlled Accordion🔗

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.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse malesuada lacus ex, sit amet blandit leo lobortis eget.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse malesuada lacus ex, sit amet blandit leo lobortis eget.

Additional actions🔗

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.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse malesuada lacus ex, sit amet blandit leo lobortis eget.

onAnimationEnd🔗

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.

onChange: Expanded: false
onAnimationEnd: Expanded: false

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse malesuada lacus ex, sit amet blandit leo lobortis eget.

Performance🔗

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.

Accessibility🔗

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

API🔗

Props - Accordion🔗

Name
Description
Type
Default Value
defaultExpandedAccordion should be expanded by defaultbooleanundefined
onChangeFires when expanded state changes(isExpanded:boolean) => voidundefined
lazyRenderDetailsUnmounts AccordionDetails content when collapsedbooleanundefined
expandedManually control expanded statebooleanundefined
hideDividerForce hides divider that appears when more that one Accordion are renderedbooleanundefined
onAnimationEndFires when expanded state changes and animation has finished(isVisible?: boolean) => voidundefined
classNameClass name to be passed to Accordion wrapperstringundefined
idId to be passed to Accordion wrapperstringundefined
extendExtends Accordion wrapper styles{} ()=>{} []undefined

Props - AccordionController🔗

Name
Description
Type
Default Value
multipleWhen false, only one Accordion opens at a timebooleanfalse

Props - AccordionSummary🔗

Name
Description
Type
Default Value
onInteractionFires when the user interacts with the AccordionSummary, either Click, Enter or Space({event?: SyntheticEvent, expanded?:boolean}) => voidundefined
roleChanges the role of the AccordionSummary wrapperstringbutton
classNameClass name to be passed to AccordionSummary wrapperstringundefined
extendExtends AccordionSummary wrapper styles{} ()=>{} []undefined
iconAlignmentVertical alignment of the AccordionSummary icontop \| centertop

Props - AccordionDetails🔗

Name
Description
Type
Default Value
classNameClass name to be passed to AccordionDetails wrapperstringundefined
extendExtends AccordionDetails wrapper styles{} ()=>{} []undefined

Props - AccordionAction🔗

Name
Description
Type
Default Value
classNameClass name to be passed to AccordionAction wrapperstringundefined
extendExtends AccordionAction wrapper styles{} ()=>{} []undefined
idId to be passed to AccordionAction wrapperstringundefined
2022 © Volvo Car Corporation