React Accessible Accordion
react-accessible-accordion is a React component library providing a WAI-ARIA compliant accordion interface. The current stable version is 5.0.1. While it provides robust accessibility features for accordions, the project is officially no longer maintained, with the developers strongly recommending the use of native HTML `<details>` and `<summary>` elements for new implementations due to their superior performance, framework-agnostic nature, and built-in accessibility. Releases have been infrequent, primarily addressing bug fixes or adapting to new React versions, rather than active feature development. Key differentiators historically included its out-of-the-box WAI-ARIA compliance and a component-based API for building accordions within React applications, but these are now largely superseded by native browser capabilities. It ships with TypeScript types for improved developer experience.
Common errors
-
Accessibility features like `role="region"` are missing on accordion panels
cause The `role="region"` attribute on `AccordionItemPanel` was removed by default in v4.0.0.fixAdd the `region` boolean prop to your `AccordionItemPanel` component (e.g., `<AccordionItemPanel region>`) to re-enable the role. -
console.error: Provided with invalid HTML5 ids
cause The accordion components, particularly `AccordionItemHeading` or `AccordionItemButton`, were likely passed `id` props that are not valid HTML5 IDs, or duplicate IDs were generated/provided.fixEnsure that any `id` props provided manually are unique and adhere to HTML5 ID naming conventions. If not providing IDs, ensure the library's automatic ID generation functions correctly without conflicts, especially in complex or dynamic rendering scenarios. -
Module not found: Can't resolve 'react-accessible-accordion/dist/fancy-example.css' in ...
cause The path to the demo stylesheet is incorrect, or the file is not available in the expected location (e.g., due to build issues or incorrect `node_modules` structure).fixVerify the import statement `import 'react-accessible-accordion/dist/fancy-example.css';` is correct. If issues persist, manually locate the file in `node_modules` or download its contents and include it directly in your project.
Warnings
- gotcha The project is officially 'no longer maintained' by its creators. They strongly recommend using native HTML `<details>` and `<summary>` elements for new implementations due to their inherent performance, accessibility, and framework-agnostic benefits.
- breaking Starting with v4.0.0, the `AccordionItemPanel` component no longer includes `role="region"` by default. This change was based on WAI-ARIA guidelines, as `region` is often redundant and can clutter the accessibility tree.
- breaking Version 5.0.0 introduced React 18 support, leveraging the `useId` hook for improved out-of-order streaming. While this primarily adds compatibility, it might imply certain expectations or behaviors when integrating with older React versions or complex server-side rendering setups if not handled carefully.
- gotcha The library provides minimal to no default styling. The `fancy-example.css` stylesheet is intended for demonstration purposes only and should not be used in production directly without modification. Developers are expected to provide their own styles.
Install
-
npm install react-accessible-accordion -
yarn add react-accessible-accordion -
pnpm add react-accessible-accordion
Imports
- Accordion
const Accordion = require('react-accessible-accordion').Accordion;import { Accordion } from 'react-accessible-accordion'; - AccordionItem
import AccordionItem from 'react-accessible-accordion/AccordionItem';
import { AccordionItem } from 'react-accessible-accordion'; - styles
import './node_modules/react-accessible-accordion/dist/fancy-example.css';
import 'react-accessible-accordion/dist/fancy-example.css';
Quickstart
import React from 'react';
import {
Accordion,
AccordionItem,
AccordionItemHeading,
AccordionItemButton,
AccordionItemPanel,
} from 'react-accessible-accordion';
// Optional: Import demo styles, or create your own.
import 'react-accessible-accordion/dist/fancy-example.css';
export default function BasicAccordionExample() {
// Configure accordion behavior with props like allowMultipleExpanded and allowZeroExpanded
return (
<Accordion allowZeroExpanded>
<AccordionItem uuid="item-1">
<AccordionItemHeading>
<AccordionItemButton>
What is React?
</AccordionItemButton>
</AccordionItemHeading>
<AccordionItemPanel>
<p>
React is a free and open-source front-end JavaScript library for building user interfaces based on UI components.
</p>
</AccordionItemPanel>
</AccordionItem>
<AccordionItem uuid="item-2">
<AccordionItemHeading>
<AccordionItemButton>
Why use an accessible accordion?
</AccordionItemButton>
</AccordionItemHeading>
<AccordionItemPanel>
<p>
Accessible accordions ensure that all users, including those using assistive technologies, can easily navigate and interact with collapsed and expanded content.
</p>
</AccordionItemPanel>
</AccordionItem>
</Accordion>
);
}