Accordion
Stacks headings that each show or hide a section of content.
Installation
pnpm dlx shadcn@latest add @silkware/accordionTo copy the component by hand, install its dependencies:
pnpm add react-aria-components cn lucide-reactThen copy the source into components/ui/accordion.tsx:
"use client"
import * as React from "react"import { cn } from "cn"import { DisclosurePanel as AccordionContentPrimitive, Heading as AccordionHeaderPrimitive, Disclosure as AccordionItemPrimitive, DisclosureGroup as AccordionPrimitive, Button as AccordionTriggerPrimitive, type ButtonProps, type DisclosureGroupProps, type DisclosurePanelProps, type DisclosureProps,} from "@animate-ui/react/primitives/react-aria"import { ChevronDownIcon, ChevronUpIcon } from "lucide-react"import { AnimatePresence } from "motion/react"
function Accordion({ className, ...props }: DisclosureGroupProps) { return ( <AccordionPrimitive data-slot="accordion" className={cn("flex w-full flex-col", className)} {...props} /> )}
function AccordionItem({ className, ...props }: DisclosureProps) { return ( <AnimatePresence initial={false}> <AccordionItemPrimitive data-slot="accordion-item" className={cn("not-last:border-b", className)} {...props} /> </AnimatePresence> )}
function AccordionTrigger({ className, children, ...props}: Omit<ButtonProps, "children"> & { children: React.ReactNode }) { return ( <AccordionHeaderPrimitive className="flex"> <AccordionTriggerPrimitive slot="trigger" data-slot="accordion-trigger" className={cn( "group/accordion-trigger relative flex flex-1 items-start justify-between rounded-md border border-transparent py-4 text-left text-sm font-medium transition-all outline-none hover:underline focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 focus-visible:after:border-ring disabled:pointer-events-none disabled:opacity-50 **:data-[slot=accordion-trigger-icon]:ml-auto **:data-[slot=accordion-trigger-icon]:size-4 **:data-[slot=accordion-trigger-icon]:text-muted-foreground", className )} {...props} > {children} <ChevronDownIcon data-slot="accordion-trigger-icon" className="pointer-events-none shrink-0 group-aria-expanded/accordion-trigger:hidden" /> <ChevronUpIcon data-slot="accordion-trigger-icon" className="pointer-events-none hidden shrink-0 group-aria-expanded/accordion-trigger:inline" /> </AccordionTriggerPrimitive> </AccordionHeaderPrimitive> )}
function AccordionContent({ className, children, ...props}: DisclosurePanelProps) { return ( <AccordionContentPrimitive data-slot="accordion-content" className="overflow-clip text-sm" {...props} > <div className={cn( "pt-0 pb-4 [&_a]:underline [&_a]:underline-offset-3 [&_a]:hover:text-foreground [&_p:not(:last-child)]:mb-4", className )} > {children} </div> </AccordionContentPrimitive> )}
export { Accordion, AccordionItem, AccordionTrigger, AccordionContent }Usage
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger,} from "@/components/ui/accordion"<Accordion defaultExpandedKeys={["approval"]}> <AccordionItem id="approval"> <AccordionTrigger>Who approves a purchase order?</AccordionTrigger> <AccordionContent>Orders above 5,000 euros go to Giulia.</AccordionContent> </AccordionItem></Accordion>Give every AccordionItem an id. The expanded state is a set of those ids, in defaultExpandedKeys, expandedKeys and onExpandedChange. By default only one item is open at a time.
Examples
Single
One item open at a time. Opening another closes the first. Use it for questions and answers, where the user reads one answer and moves on.
Multiple
allowsMultipleExpanded lets several items stay open. Use it when the user compares sections.
Disabled
isDisabled on an AccordionItem ignores presses and removes the trigger from the tab order. Use it for a section that has nothing to show yet.
Controlled
Use expandedKeys and onExpandedChange to open items from outside, for example from a link elsewhere on the page. The value is a Set of ids.
Accessibility
| Key | Action |
|---|---|
| Space | Expands or collapses the focused item. |
| Enter | Expands or collapses the focused item. |
| Tab | Moves focus to the next trigger or focusable element in an open panel. |
| ShiftTab | Moves focus to the previous trigger. |
React Aria provides:
- A heading that contains a button for each trigger, with
aria-expandedandaria-controlslinking it to its panel. - A panel with the
grouprole, labelled by its trigger, and hidden from the accessibility tree while collapsed. - Single or multiple expansion in the group, and a disabled state that removes the trigger from the tab order.
You provide:
- A trigger label that names the content of the panel.
- The right heading level.
AccordionTriggerrenders anh3; make sure it fits the outline of the page. - Content that does not depend on being opened: do not hide information the user must see.
Guidelines
When to use
- For several sections of secondary content where most users need only one or two: questions and answers, grouped settings, long reference text.
- To shorten a long page that has a clear list of topics.
When not to use
- For a single block that opens and closes. Use a Collapsible.
- For views of the same object that are switched, not stacked. Use Tabs.
- For content every user must read. Show it in the page.
Content
- Write the trigger as the question or topic: "When does a supplier deliver?" or "Quality".
- Use sentence case and no full stop on the trigger.
- Keep panels short. A panel longer than a screen belongs on its own page.
Group several related questions, each with a short answer.
Don’t hide what the user must act on. A single item is a Collapsible at most, and this deadline belongs in the page.
API reference
Accordion
The group. Renders a div through the React Aria DisclosureGroup. It accepts every DisclosureGroup prop and the props below.
| Prop | Type | Default | Description |
|---|---|---|---|
allowsMultipleExpanded? | boolean | false | Lets more than one item stay open. |
expandedKeys? | Iterable<Key> | None | The ids of the open items, for a controlled accordion. |
defaultExpandedKeys? | Iterable<Key> | None | The ids of the items open at first. |
onExpandedChange? | (keys: Set<Key>) => void | None | Called when an item opens or closes. |
isDisabled? | boolean | false | Disables every item. |
className? | string | None | Classes merged after the base classes. |
AccordionItem
One section, with a bottom border except for the last. Renders a div through the React Aria Disclosure.
| Prop | Type | Default | Description |
|---|---|---|---|
id? | Key | None | Identifies the item in the expanded set. Required for control by the group. |
isDisabled? | boolean | false | Ignores presses on the trigger. |
className? | string | None | Classes merged after the base classes. |
AccordionTrigger
The heading and its button, with a chevron that follows the state. Renders an h3 with a React Aria Button, so it accepts every Button prop.
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | None | The label of the section. |
className? | string | None | Classes merged after the base classes. |
AccordionContent
The panel. Renders a DisclosurePanel with an inner wrapper that holds the padding. It accepts every DisclosurePanel prop.
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | None | The content of the section. |
className? | string | None | Classes applied to the inner wrapper, not to the panel. |