A vertically stacked set of interactive headings that each reveal an associated section of content.
Full keyboard navigation.
Supports horizontal/vertical orientation.
Supports Right to Left direction.
Can expand one or multiple items.
Can be controlled or uncontrolled.
Install the component from your command line.
npm install @radix-ui/react-accordion
Import all parts and piece them together.
import * as Accordion from '@radix-ui/react-accordion';
() => (
<Accordion.Root>
<Accordion.Item>
<Accordion.Header>
<Accordion.Trigger />
</Accordion.Header>
<Accordion.Content />
</Accordion.Item>
</Accordion.Root>
);
Contains all the parts of an accordion.
Contains all the parts of a collapsible section.
Wraps an Accordion.Trigger
. Use the asChild
prop to update it to the appropriate heading level for your page.
Toggles the collapsed state of its associated item. It should be nested inside of an Accordion.Header
.
Contains the collapsible content for an item.
Use the defaultValue
prop to define the open item by default.
<Accordion.Root type="single" defaultValue="item-2">
<Accordion.Item value="item-1">…</Accordion.Item>
<Accordion.Item value="item-2">…</Accordion.Item>
</Accordion.Root>
Use the collapsible
prop to allow all items to close.
<Accordion.Root type="single" collapsible>
<Accordion.Item value="item-1">…</Accordion.Item>
<Accordion.Item value="item-2">…</Accordion.Item>
</Accordion.Root>
Set the type
prop to multiple
to enable opening multiple items at once.
<Accordion.Root type="multiple">
<Accordion.Item value="item-1">…</Accordion.Item>
<Accordion.Item value="item-2">…</Accordion.Item>
</Accordion.Root>
You can add extra decorative elements, such as chevrons, and rotate it when the item is open.
// index.jsx
import * as Accordion from '@radix-ui/react-accordion';
import { ChevronDownIcon } from '@radix-ui/react-icons';
import './styles.css';
export default () => (
<Accordion.Root type="single">
<Accordion.Item value="item-1">
<Accordion.Header>
<Accordion.Trigger className="AccordionTrigger">
<span>Trigger text</span>
<ChevronDownIcon className="AccordionChevron" aria-hidden />
</Accordion.Trigger>
</Accordion.Header>
<Accordion.Content>…</Accordion.Content>
</Accordion.Item>
</Accordion.Root>
);
/* styles.css */
.AccordionChevron {
transition: transform 300ms;
}
.AccordionTrigger[data-state='open'] > .AccordionChevron {
transform: rotate(180deg);
}
Use the orientation
prop to create a horizontal accordion.
<Accordion.Root orientation="horizontal">
<Accordion.Item value="item-1">…</Accordion.Item>
<Accordion.Item value="item-2">…</Accordion.Item>
</Accordion.Root>
Use the --radix-accordion-content-width
and/or --radix-accordion-content-height
CSS variables to animate the size of the content when it opens/closes:
// index.jsx
import * as Accordion from '@radix-ui/react-accordion';
import './styles.css';
export default () => (
<Accordion.Root type="single">
<Accordion.Item value="item-1">
<Accordion.Header>…</Accordion.Header>
<Accordion.Content className="AccordionContent">…</Accordion.Content>
</Accordion.Item>
</Accordion.Root>
);
/* styles.css */
.AccordionContent {
overflow: hidden;
}
.AccordionContent[data-state='open'] {
animation: slideDown 300ms ease-out;
}
.AccordionContent[data-state='closed'] {
animation: slideUp 300ms ease-out;
}
@keyframes slideDown {
from {
height: 0;
}
to {
height: var(--radix-accordion-content-height);
}
}
@keyframes slideUp {
from {
height: var(--radix-accordion-content-height);
}
to {
height: 0;
}
}
Adheres to the Accordion WAI-ARIA design pattern.