Skip to the content

Controls and surfaces

collapsible

One section that a button opens and closes in place.

Primitives

When to use it

A single part of a page that is hidden until asked for: more options under a form, the rest of a long list, details under a summary. The trigger is yours to style (a Button through asChild). For several sections under headings use accordion; for content that floats over the page use popover.

See it working

The demonstration the browser checks drive, with the keyboard as well as the pointer. More of it is on the Primitives page.

Alert dialog

The project is kept.

Collapsible

The options are closed.

Hover card

The first published program was written by Ada Lovelace.

Menubar

Command: none yet. Ruler: hidden. Zoom: 100.

Navigation menu

Toolbar

Styles: none. Saved 0 times.

One-time code

Typed: nothing yet. Sent: nothing yet.

Password

At least twelve characters.

Words for screen readers

Add it

npx shadcn@latest add @rivier-ui/collapsible

The registry is named once in components.json: see Installation.

Example

Import it from @/components/ui/collapsible.

<Collapsible>
  <CollapsibleTrigger asChild><Button variant="ghost" size="sm">More options</Button></CollapsibleTrigger>
  <CollapsibleContent>
    <p>Shown only while open.</p>
  </CollapsibleContent>
</Collapsible>

What it needs

A client component: it uses state, effects or browser behaviour. Packages: radix-ui.

It imports

Source

The whole file. Once copied it is yours: the registry item holds the same text.

src/components/ui/collapsible.tsx
"use client";
/**
 * @name collapsible
 * @category ui
 * @description One section that a button opens and closes in place.
 * @when A single part of a page that is hidden until asked for: more options under a form, the rest of a long list, details under a summary. The trigger is yours to style (a Button through asChild). For several sections under headings use accordion; for content that floats over the page use popover.
 * @example
 * <Collapsible>
 *   <CollapsibleTrigger asChild><Button variant="ghost" size="sm">More options</Button></CollapsibleTrigger>
 *   <CollapsibleContent>
 *     <p>Shown only while open.</p>
 *   </CollapsibleContent>
 * </Collapsible>
 */
import { Collapsible as CollapsiblePrimitive } from "radix-ui";
import type { ComponentProps } from "react";
import { cn } from "@/lib/cn";

/** Holds the open state: `open`, `defaultOpen`, `onOpenChange`; `disabled` locks it as it is. */
export function Collapsible(props: ComponentProps<typeof CollapsiblePrimitive.Root>) {
  return <CollapsiblePrimitive.Root data-slot="collapsible" {...props} />;
}

/** The button. It carries `aria-expanded` and `data-state` ("open" or "closed"), so a chevron in it can turn with `group-data-[state=open]`. */
export function CollapsibleTrigger(props: ComponentProps<typeof CollapsiblePrimitive.Trigger>) {
  return <CollapsiblePrimitive.Trigger data-slot="collapsible-trigger" {...props} />;
}

/*
 * No height animation, for the reason given in accordion: only transform and opacity move. The section takes
 * its height at once and its content fades and settles in; closing is immediate.
 */
export function CollapsibleContent({ className, ...props }: ComponentProps<typeof CollapsiblePrimitive.Content>) {
  return (
    <CollapsiblePrimitive.Content
      data-slot="collapsible-content"
      className={cn("ease-out animation-duration-(--duration-base) fade-in-0 slide-in-from-top-1 data-[state=open]:animate-in motion-reduce:animate-none!", className)}
      {...props}
    />
  );
}