Skip to content
Thrum

Collapsible

One region that a button shows and hides: the rest of a list, more filters, advanced settings. The first lines stay in view and the rest waits behind a button. For several such regions in a stack use the accordion; for content over the page use a popover.

Install

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

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

import { Collapsible } from "@/components/ui/collapsible";

Props

PropTypeDefaultDescription
Collapsible: openboolean–Whether the region is shown, when the parent holds the state. Use it with onOpenChange.
Collapsible: defaultOpenbooleanDefault falseWhether the region starts shown, when the collapsible holds its own state.
Collapsible: onOpenChange(open: boolean) => void–Called with the new state when the trigger is pressed.
Collapsible: disabledbooleanDefault falseThe trigger ignores presses and carries data-disabled.
Collapsible: soundbooleanDefault truePlay a soft rising tone when the region opens and a falling one when it closes, if the visitor has sound on.
Collapsible: classNamestring–Classes for the wrapping div, such as the width and the space between its children.
CollapsibleTriggerbutton props–The button. It is unstyled apart from the focus ring: pass buttonClass() or your own classes, and an aria-label when it holds only an icon. data-panel-open is set while the region is shown.
CollapsibleContent: hiddenUntilFoundbooleanDefault falseKeep the hidden region in the page so the browser's find in page can reach it and open it.
CollapsibleContent: keepMountedbooleanDefault falseKeep the hidden region in the page. Ignored when hiddenUntilFound is set.
CollapsibleContent: classNamestring–Classes for the inner block that holds the content, such as space-y-2.

Motion and sound

Motion

  • The region grows from 0 to its height over 200 ms when it opens, and shrinks back when it closes. A second press during it reverses from where it is.
  • Reduced motion: the region appears and leaves at once.

Sound

  • A soft rising tone when the region opens and a falling one when it closes.

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 disclosure pattern: the trigger is a button with aria-expanded, and aria-controls points at the region.
  • Name an icon-only trigger with aria-label. The name stays the same; aria-expanded says whether the region is open.
  • A hidden region is out of the page, so its contents are neither read nor reached by Tab.
  • The region clips its content while it opens and closes. Keep focusable content inside it 2 pixels from its edges so their focus rings show in full.
KeysAction
TabFocus the trigger.
Enter / SpaceShow or hide the region.

Source

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

import { Collapsible as CollapsiblePrimitive } from "@base-ui/react/collapsible";

import { cn } from "@/lib/utils";
import { play } from "@/lib/thrum-sound";

export type CollapsibleProps = CollapsiblePrimitive.Root.Props & {
  /** Play a soft sound when the region opens or closes, if the visitor has sound on. */
  sound?: boolean;
};

/**
 * One region that a button shows and hides: more filters, the rest of a list, advanced settings.
 * `Collapsible` holds the state; `CollapsibleTrigger` toggles it; `CollapsibleContent` is the region.
 */
export function Collapsible({ sound = true, onOpenChange, className, ...props }: CollapsibleProps) {
  return (
    <CollapsiblePrimitive.Root
      data-slot="collapsible"
      onOpenChange={(open, details) => {
        if (sound) play(open ? "open" : "close");
        onOpenChange?.(open, details);
      }}
      className={className}
      {...props}
    />
  );
}

/**
 * The button that shows and hides the region. It is unstyled: give it `buttonClass()` from the button, or your own classes.
 * It carries `data-panel-open` while the region is open, for a chevron that turns.
 */
export function CollapsibleTrigger({ className, ...props }: CollapsiblePrimitive.Trigger.Props) {
  return (
    <CollapsiblePrimitive.Trigger
      data-slot="collapsible-trigger"
      className={cn("cursor-pointer focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring data-disabled:cursor-not-allowed", className)}
      {...props}
    />
  );
}

/** The region. Its height opens from 0 and closes back; a second press reverses it halfway. */
export function CollapsibleContent({ className, children, ...props }: Omit<CollapsiblePrimitive.Panel.Props, "className"> & { className?: string }) {
  return (
    <CollapsiblePrimitive.Panel
      data-slot="collapsible-content"
      className={cn(
        "h-(--collapsible-panel-height) overflow-hidden",
        "transition-[height] duration-200 ease-out data-ending-style:h-0 data-starting-style:h-0 motion-reduce:transition-none",
      )}
      {...props}
    >
      <div className={className}>{children}</div>
    </CollapsiblePrimitive.Panel>
  );
}

Files and dependencies

Adds
components/ui/collapsible.tsx
Packages
@base-ui/react@1.9.0
Brings along
Sound engine

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