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.jsonAdds 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
| Prop | Type | Default | Description |
|---|---|---|---|
Collapsible: open | boolean | – | Whether the region is shown, when the parent holds the state. Use it with onOpenChange. |
Collapsible: defaultOpen | boolean | Default false | Whether 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: disabled | boolean | Default false | The trigger ignores presses and carries data-disabled. |
Collapsible: sound | boolean | Default true | Play a soft rising tone when the region opens and a falling one when it closes, if the visitor has sound on. |
Collapsible: className | string | – | Classes for the wrapping div, such as the width and the space between its children. |
CollapsibleTrigger | button 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: hiddenUntilFound | boolean | Default false | Keep the hidden region in the page so the browser's find in page can reach it and open it. |
CollapsibleContent: keepMounted | boolean | Default false | Keep the hidden region in the page. Ignored when hiddenUntilFound is set. |
CollapsibleContent: className | string | – | 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.
| Keys | Action |
|---|---|
| Tab | Focus the trigger. |
| Enter / Space | Show or hide the region. |
Source
"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.0.0 · 2026-12-03First release.