Accordion
React Accordion components let users show and hide sections of related content on a page. They are often used for FAQs, menus and submenus, settings panels, and other layouts where content needs to stay organized.
This Accordion component is styled using Tailwind CSS, giving developers control over structure, layout, and styling.
Installation
Install the component using the Tailgrids CLI.
npx @tailgrids/cli@latest add accordionAnatomy
Import the component parts and combine them.
import {
Accordion,
AccordionItem,
AccordionTrigger,
AccordionContent
} from "@/components/tailgrids/accordion";
export const AccordionExample = () => (
<Accordion>
<AccordionItem>
<AccordionTrigger />
<AccordionContent />
</AccordionItem>
</Accordion>
);Examples
Style One (Default)
This is the default style for the accordion. No variant prop is required.
Style Two
Set variant="style_two" on Accordion for this visual style.
Style Three
Set variant="style_three" on Accordion for this variation.
Style Four
Set variant="style_four" on Accordion for this layout.
Style Five
Set variant="style_five" on Accordion for this design.
Custom
Use className to give a custom look to the component.
API Reference
Accordion
Wraps all accordion items and controls the overall layout and style. Extends React Aria's DisclosureGroup props.
| Prop | Type | Default | Description |
|---|---|---|---|
variant | 'style_one' | 'style_two' | 'style_three' | 'style_four' | 'style_five' | 'style_one' | Tailgrids-only visual style for the accordion |
allowsMultipleExpanded | boolean | false | Allow more than one item to be expanded at once |
defaultExpandedKeys | Iterable<Key> | - | Item ids expanded initially (uncontrolled) |
expandedKeys | Iterable<Key> | - | Controlled set of expanded item ids |
onExpandedChange | (keys: Set<Key>) => void | - | Called when the expanded items change |
className | string | - | Additional CSS classes |
children | React.ReactNode | - | Accordion items |
AccordionItem
A single accordion item that groups a trigger and its content. Extends React Aria's Disclosure props; give each item an id when using defaultExpandedKeys or expandedKeys.
| Prop | Type | Description |
|---|---|---|
id | Key | Unique item identifier |
className | string | Additional CSS classes |
children | React.ReactNode | Trigger and content components |
| Data Attribute | Values | Description |
|---|---|---|
data-expanded | boolean | Indicates whether the item is expanded or collapsed. |
AccordionTrigger
Toggles the visibility of the accordion content. Renders a semantic heading wrapping a React Aria button with slot="trigger". Extends React Aria's Button props.
| Prop | Type | Default | Description |
|---|---|---|---|
level | 1 | 2 | 3 | 4 | 5 | 6 | 3 | Heading level rendered around the trigger |
className | string | - | Additional CSS classes |
children | React.ReactNode | - | Trigger content (typically text) |
| Data Attribute | Values | Description |
|---|---|---|
data-expanded | boolean | Indicates whether the item is expanded or collapsed. |
AccordionContent
Holds the collapsible content for an accordion item. Maps to React Aria's DisclosurePanel.
| Prop | Type | Description |
|---|---|---|
className | string | Additional CSS classes |
children | React.ReactNode | Content to display when expanded |
| Data Attribute | Values | Description |
|---|---|---|
data-expanded | boolean | Indicates whether the item is expanded or collapsed. |
Accessibility
- Built on React Aria's
DisclosureGroup,DisclosureandDisclosurePanel, which managearia-expanded,aria-controlsandaria-labelledby - Uses a semantic heading structure (
h3by default) around each trigger - Supports keyboard interaction with visible focus rings
- Associates each trigger with its content using React Aria's disclosure relationships
data-expandedon items, triggers and panels can be used for styling
