Skip to content
Thrum

Accordion

Stacked sections whose bodies open and close: questions and answers, groups of settings, a long form in steps. Use it when readers need one or two sections out of many. For a single section use the collapsible; for views of equal weight use tabs.

Install

npx shadcn@latest add https://thrumui.dev/r/accordion.json

Adds components/ui/accordion.tsx and installs @base-ui/react@1.9.0. Nothing else in your project changes.

import { Accordion } from "@/components/ui/accordion";

Automatically installs icon chevron-down from the icon set.

Props

PropTypeDefaultDescription
Accordion: valuestring[]–The values of the open sections, when the parent holds them. Use it with onValueChange.
Accordion: defaultValuestring[]Default []The sections open at first, when the accordion holds its own state.
Accordion: onValueChange(value: string[]) => void–Called with the open sections after a trigger is pressed.
Accordion: multiplebooleanDefault falseLet several sections be open at once. Off, opening one closes the other.
Accordion: collapsiblebooleanDefault trueWith multiple off: whether pressing the open section closes it. Set false to always keep one open.
Accordion: variant"lines" | "boxed"Default "lines"lines puts a 1 pixel rule between sections. boxed draws each section as a bordered box in the card colour, 8 pixels apart.
Accordion: disabledbooleanDefault falseDim every section and ignore presses.
Accordion: hiddenUntilFoundbooleanDefault falseKeep closed bodies in the page so the browser's find in page can reach them and open their section.
Accordion: keepMountedbooleanDefault falseKeep closed bodies in the page, hidden. Ignored when hiddenUntilFound is set.
Accordion: soundbooleanDefault truePlay a soft rising tone when a section opens and a falling one when it closes, if the visitor has sound on.
AccordionItem: valuestring–The section's name in value. Without it the section is known by its position.
AccordionItem: disabledbooleanDefault falseDim this section's trigger and ignore presses on it. It stays in the tab order.
AccordionItem: onOpenChange(open: boolean) => void–Called when this section opens or closes.
AccordionTrigger: headingLevel2 | 3 | 4 | 5 | 6Default 3The level of the heading the trigger button sits in. Pick the one that fits the page's outline.
AccordionContentdiv props–The body. Its className goes to the inner block that holds the padding.
classNamestring–On every part: extra classes, merged last.

Motion and sound

Motion

  • A body opens by growing from 0 to its height over 200 ms, and closes the same way back. A second press during it reverses from where it is.
  • The chevron turns half a turn over 200 ms as its section opens.
  • Reduced motion: bodies open and close at once and the chevron flips without turning.

Sound

  • A soft rising tone when a section opens and a falling one when a section closes. Opening one section in single mode closes another and still makes one sound.

Sound is off until the visitor turns it on. Pass sound={false} to keep this component silent. How sound works

Accessibility

  • It follows the WAI-ARIA accordion pattern: each trigger is a button inside a heading, with aria-expanded and aria-controls pointing at its body, which is a region named by the trigger.
  • Arrow keys do not move between triggers. The pattern made that optional, and Tab already reaches every trigger in order.
  • Open and closed differ in the chevron's direction as well as in the body being shown.
  • A long title wraps beside the chevron. Nothing is cut off at 160 pixels wide.
  • Both variants adapt to glass materials: lines wraps the stack in a panel surface (data-surface="panel"), and boxed gives each section its own card panel surface.
KeysAction
Tab / Shift+TabMove between the section triggers and any control inside an open body.
Enter / SpaceOpen or close the focused section.

Source

components/ui/accordion.tsx
"use client";

import { createContext, useContext, useRef } from "react";
import { Accordion as AccordionPrimitive } from "@base-ui/react/accordion";

import { cn } from "@/lib/utils";
import { play } from "@/lib/thrum-sound";
import { ChevronDownIcon } from "@/components/icons/chevron-down";

export type AccordionVariant = "lines" | "boxed";

const VariantContext = createContext<AccordionVariant>("lines");

export type AccordionProps = AccordionPrimitive.Root.Props<string> & {
  /** `lines` separates the sections with a rule; `boxed` draws each section as its own bordered box. */
  variant?: AccordionVariant;
  /** With `multiple` off: whether the open section can be closed, leaving none open. */
  collapsible?: boolean;
  /** Play a soft sound when a section opens or closes, if the visitor has sound on. */
  sound?: boolean;
};

/**
 * Stacked sections whose bodies open and close: FAQs, settings groups, long forms in steps.
 * One section is open at a time unless `multiple` is set. Each section is an `AccordionItem` with a trigger and content.
 */
export function Accordion({ variant = "lines", collapsible = true, sound = true, multiple = false, value, defaultValue, onValueChange, className, ...props }: AccordionProps) {
  // The last value, so one press makes one sound: opening a section in single mode also closes another.
  const last = useRef<string[]>(defaultValue ?? []);
  return (
    <VariantContext value={variant}>
      <AccordionPrimitive.Root
        data-slot="accordion"
        data-variant={variant}
        data-surface={variant === "lines" ? "panel" : undefined}
        multiple={multiple}
        value={value}
        defaultValue={defaultValue}
        onValueChange={(next, details) => {
          if (!multiple && !collapsible && next.length === 0) {
            details.cancel();
            return;
          }
          const before = value ?? last.current;
          last.current = next;
          if (sound) play(next.some((item) => !before.includes(item)) ? "open" : "close");
          onValueChange?.(next, details);
        }}
        className={cn(
          "group/accordion flex w-full flex-col",
          variant === "lines" && "rounded-lg border bg-card px-4 text-card-foreground",
          variant === "boxed" && "gap-2",
          className,
        )}
        {...props}
      />
    </VariantContext>
  );
}

/** One section: an `AccordionTrigger` and its `AccordionContent`. Give it a `value` to open it by name. */
export function AccordionItem({ className, ...props }: AccordionPrimitive.Item.Props) {
  const variant = useContext(VariantContext);
  return (
    <AccordionPrimitive.Item
      data-slot="accordion-item"
      data-surface={variant === "boxed" ? "panel" : undefined}
      className={cn(
        "group-data-[variant=lines]/accordion:border-b group-data-[variant=lines]/accordion:border-foreground/15 group-data-[variant=lines]/accordion:last:border-b-0",
        "group-data-[variant=boxed]/accordion:rounded-lg group-data-[variant=boxed]/accordion:border group-data-[variant=boxed]/accordion:bg-card group-data-[variant=boxed]/accordion:text-card-foreground",
        className,
      )}
      {...props}
    />
  );
}

export type AccordionTriggerProps = AccordionPrimitive.Trigger.Props & {
  /** The level of the heading the trigger sits in. Pick the one that fits the page's outline. */
  headingLevel?: 2 | 3 | 4 | 5 | 6;
};

/** The section's title: a button inside a heading. The chevron turns when the section opens. */
export function AccordionTrigger({ headingLevel = 3, className, children, ...props }: AccordionTriggerProps) {
  const Heading = `h${headingLevel}` as "h3";
  return (
    <AccordionPrimitive.Header data-slot="accordion-header" render={<Heading />} className="flex">
      <AccordionPrimitive.Trigger
        data-slot="accordion-trigger"
        className={cn(
          "flex min-h-10 flex-1 cursor-pointer items-center justify-between gap-3 rounded-md py-3 text-start text-sm font-medium",
          "transition-[color] duration-150 ease-out hover:underline hover:underline-offset-4 motion-reduce:transition-none",
          "focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-ring",
          "data-disabled:cursor-not-allowed data-disabled:opacity-50 data-disabled:hover:no-underline",
          "group-data-[variant=boxed]/accordion:px-4",
          "[&[data-panel-open]>svg]:rotate-180",
          className,
        )}
        {...props}
      >
        <span className="min-w-0 break-words">{children}</span>
        <ChevronDownIcon size={16} className="flex-none text-muted-foreground transition-[rotate] duration-200 ease-out motion-reduce:transition-none" />
      </AccordionPrimitive.Trigger>
    </AccordionPrimitive.Header>
  );
}

/** The section's body. Its height opens from 0 and closes back; a second press reverses it halfway. */
export function AccordionContent({ className, children, ...props }: Omit<AccordionPrimitive.Panel.Props, "className"> & { className?: string }) {
  return (
    <AccordionPrimitive.Panel
      data-slot="accordion-content"
      className={cn(
        "h-(--accordion-panel-height) overflow-hidden text-sm",
        "transition-[height] duration-200 ease-out data-ending-style:h-0 data-starting-style:h-0 motion-reduce:transition-none",
      )}
      {...props}
    >
      <div className={cn("pb-4 group-data-[variant=boxed]/accordion:px-4", className)}>{children}</div>
    </AccordionPrimitive.Panel>
  );
}

Files and dependencies

Adds
components/ui/accordion.tsx
Packages
@base-ui/react@1.9.0
Brings along
Sound engine
Icons
chevron-down

Changelog

  1. 1.0.0 · 2026-12-03First release.

Loading index…

Keyboard shortcuts

/Ctrl KSearch components, docs and actions
?Show this list
AAppearance studio
SSound on or off
MFull or reduced motion
TTheme: auto, light, dark
GSurface: solid or frosted
BBackdrop photograph (under glass)
CCorner radius: square to round
RReplay the demo, on a component page
DDraw new data, on a component page
←→HomeEndMove the cursor in a focused chart
EscClose a dialog, or leave a chart cursor