Accordion
A vertically stacked set of interactive headings that each reveal a section of content.
Installation
npx shadcn@latest add accordion
Usage
import {
Accordion,
AccordionContent,
AccordionItem,
AccordionTrigger,
} from "@/components/ui/accordion"
export function AccordionDemo() {
return (
<Accordion type="single" collapsible className="w-full">
<AccordionItem value="item-1">
<AccordionTrigger>Is it accessible?</AccordionTrigger>
<AccordionContent>
Yes. It adheres to the WAI-ARIA design pattern.
</AccordionContent>
</AccordionItem>
<AccordionItem value="item-2">
<AccordionTrigger>Is it styled?</AccordionTrigger>
<AccordionContent>
Yes. It comes with default styles that matches the other components' aesthetic.
</AccordionContent>
</AccordionItem>
<AccordionItem value="item-3">
<AccordionTrigger>Is it animated?</AccordionTrigger>
<AccordionContent>
Yes. It's animated by default, but you can disable it if you prefer.
</AccordionContent>
</AccordionItem>
</Accordion>
)
}
API Reference
Accordion
The root component that wraps all accordion items.
Prop | Type | Default | Description |
---|---|---|---|
type | "single" | "multiple" | - | Determines whether one or multiple items can be opened at the same time. |
value | string | string[] | - | The controlled value of the item(s) to expand. |
defaultValue | string | string[] | - | The value of the item(s) to expand when initially rendered (uncontrolled). |
collapsible | boolean | false | When type is "single" , allows closing content when clicking the trigger of an open item. |
disabled | boolean | false | When true , prevents the user from interacting with the accordion. |
dir | "ltr" | "rtl" | "ltr" | The reading direction of the accordion. |
orientation | "horizontal" | "vertical" | "vertical" | The orientation of the accordion. |
asChild | boolean | false | Merge props and behavior onto a child element. |
onValueChange | (value: string | string[]) => void | - | Event handler called when the expanded state changes. |
AccordionItem
A single collapsible item within the accordion.
Prop | Type | Default | Description |
---|---|---|---|
value | string | - | A unique value for the item. |
disabled | boolean | false | When true , prevents the user from interacting with the item. |
asChild | boolean | false | Merge props and behavior onto a child element. |
AccordionTrigger
The button that toggles the collapsed state of an item.
Prop | Type | Default | Description |
---|---|---|---|
asChild | boolean | false | Merge props and behavior onto a child element. |
AccordionContent
The collapsible content area.
Prop | Type | Default | Description |
---|---|---|---|
asChild | boolean | false | Merge props and behavior onto a child element. |
forceMount | boolean | - | Used to force mount when more control is needed. |
Accessibility
The accordion component follows the WAI-ARIA Accordion Pattern, including:
Keyboard Interactions
Key | Description |
---|---|
Space | When focus is on an accordion trigger, activates the trigger. |
Enter | When focus is on an accordion trigger, activates the trigger. |
Tab | Moves focus to the next focusable element. |
Shift + Tab | Moves focus to the previous focusable element. |
Arrow Down | When focus is on a trigger, moves focus to the next trigger. |
Arrow Up | When focus is on a trigger, moves focus to the previous trigger. |
Home | When focus is on a trigger, moves focus to the first trigger. |
End | When focus is on a trigger, moves focus to the last trigger. |
ARIA Attributes
The component automatically handles the following ARIA attributes:
role="region"
on the content areaaria-controls
linking triggers to their contentaria-expanded
indicating the expanded statearia-disabled
when items are disabled
Customization
Styling
The accordion component uses CSS variables for theming and can be customized using Tailwind classes:
<Accordion className="w-full">
<AccordionItem className="border-b">
<AccordionTrigger className="text-left">
Custom styled trigger
</AccordionTrigger>
<AccordionContent className="text-muted-foreground">
Custom styled content
</AccordionContent>
</AccordionItem>
</Accordion>
Animation
The expand/collapse animation can be customized through CSS:
[data-state="open"] {
animation: accordion-down 0.2s ease-out;
}
[data-state="closed"] {
animation: accordion-up 0.2s ease-out;
}
Examples
Single Mode (Non-collapsible)
Multiple Mode
Controlled State
Custom Styling
SEO Considerations
For better SEO, consider:
- Semantic HTML: The accordion uses proper heading hierarchy and semantic markup.
- Content Visibility: All accordion content is in the DOM (even when collapsed), making it accessible to search engines.
- Structured Data: Consider adding FAQ schema markup when using accordions for FAQs:
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [{
"@type": "Question",
"name": "Question text",
"acceptedAnswer": {
"@type": "Answer",
"text": "Answer text"
}
}]
}
</script>
- Progressive Enhancement: The accordion content remains accessible even without JavaScript.