A react component that renders an accessible accordion.
$ npm install @moxy/react-accordion
This library is written in modern JavaScript and is published in both CommonJS and ES module transpiled variants. If you target older browsers please make sure to transpile accordingly.
An accordion allows users to toggle the display of content sections. This package was created to make it easier to implement an accordion with accessible navigation.
import React from 'react';
import { Accordion, AccordionItem } from '@moxy/react-accordion';
const MyComponent = () => {
<div>
<Accordion defaultActiveItems={ [0, 1] } multiple scrollIntoView>
<AccordionItem title="Foo">
Bar
</AccordionItem>
<AccordionItem title="Foo1">
Bar1
</AccordionItem>
</Accordion>
</div>
}
To import a stylesheet, one can import it on the project's entry CSS file:
/* src/index.css */
@import "@moxy/react-accordion/dist/index.css";
Or in the project's entry JavaScript file:
/* src/index.js */
import "@moxy/react-accordion/dist/index.css";
These are the props available in @moxy/react-accordion
.
Type: node
| Required: true
One or more AccordionItem
elements that will be rendered inside the Accordion
.
Type: array
| Required: false
| Default: []
The indexes of the items that should be open by default.
It is necessary to have the prop multiple
enabled to have more than one item open.
Type: boolean
| Required: false
| Default: false
Whether or not multiple items can be open at the same time.
Type: string
| Required: false
A className to apply to the accordion container.
Type: string
| Required: false
A className to apply to the accordion container when all the items are closed.
Type: function
| Required: false
| Arguments: index: Number, options: { active: Boolean }
Callback to be invoked before an item is opened or closed.
Type: any
| Required: true
The content of the title.
Type: node
or function
| Required: false
The content of the item.
Example with function:
<AccordionItem { ...props }>
{ ({ isActive }) => isActive ? 'Open' : 'Closed' }
</AccordionItem>
⚠️ All the following props can be defined at theAccordion
level whenever you want a specific prop to be the same for all accordion items.Keep in mind that if you define a prop in
Accordion
and in anAccordionItem
, the one that will be used is theAccordionItem
one.
Type: number
| Required: false
| Default: 300
Duration of the animation for both opening and closing states.
Type: string
| Required: false
| Default: ease-in-out
Type of animation easing for both opening and closing states.
Type: string
| Required: false
A className to apply to the item container.
Type: string
| Required: false
A className to apply to the heading container.
Type: string
| Required: false
A className to apply to the title.
Type: string
| Required: false
A className to apply to the content container.
Type: string
| Required: false
A className to apply to the content.
Type: string
| Required: false
A className to apply to the icon container.
Type: any
| Required: false
The content of the icon.
$ npm test
$ npm test -- --watch # during development
A demo Next.js project is available in the /demo
folder so you can try out this component.
First, build the react-accordion
project with:
$ npm run build
To run the demo, do the following inside the demo's folder:
$ npm i
$ npm run dev
Note: Everytime a change is made to the package a rebuild is required to reflect those changes on the demo.
There is an ongoing next.js issue about the loading order of modules and global CSS in development mode. This has been fixed in v9.3.6-canary.0, so you can either update next.js
to a version higher than v9.3.5
, or simply increase the CSS specificity when overriding component's classes, as we did in the demo
, e.g. having the page or section CSS wrap the component's one.
Released under the MIT License.