Skip to content

Accordion

Stacks headings that each show or hide a section of content.

Orders above 5,000 euros go to Giulia. Smaller orders are approved by the department lead.

Installation

pnpm dlx shadcn@latest add @silkware/accordion

To copy the component by hand, install its dependencies:

pnpm add react-aria-components cn lucide-react

Then copy the source into components/ui/accordion.tsx:

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.

Orders above 5,000 euros go to Giulia. Smaller orders are approved by the department lead.

Multiple

allowsMultipleExpanded lets several items stay open. Use it when the user compares sections.

Work orders, line schedules and output for every site.

Inspections, non-conformities and corrective actions.

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.

Three lines, two shifts a day.

Accessibility

KeyAction
SpaceExpands or collapses the focused item.
EnterExpands or collapses the focused item.
TabMoves focus to the next trigger or focusable element in an open panel.
ShiftTabMoves focus to the previous trigger.

React Aria provides:

  • A heading that contains a button for each trigger, with aria-expanded and aria-controls linking it to its panel.
  • A panel with the group role, 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. AccordionTrigger renders an h3; 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.

Do

Group several related questions, each with a short answer.

Don’t

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.

PropTypeDefaultDescription
allowsMultipleExpanded?booleanfalseLets more than one item stay open.
expandedKeys?Iterable<Key>NoneThe ids of the open items, for a controlled accordion.
defaultExpandedKeys?Iterable<Key>NoneThe ids of the items open at first.
onExpandedChange?(keys: Set<Key>) => voidNoneCalled when an item opens or closes.
isDisabled?booleanfalseDisables every item.
className?stringNoneClasses merged after the base classes.

AccordionItem

One section, with a bottom border except for the last. Renders a div through the React Aria Disclosure.

PropTypeDefaultDescription
id?KeyNoneIdentifies the item in the expanded set. Required for control by the group.
isDisabled?booleanfalseIgnores presses on the trigger.
className?stringNoneClasses 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.

PropTypeDefaultDescription
childrenReactNodeNoneThe label of the section.
className?stringNoneClasses merged after the base classes.

AccordionContent

The panel. Renders a DisclosurePanel with an inner wrapper that holds the padding. It accepts every DisclosurePanel prop.

PropTypeDefaultDescription
childrenReactNodeNoneThe content of the section.
className?stringNoneClasses applied to the inner wrapper, not to the panel.