← Components

Utilities

Collapsible

One section that shows and hides. Its label rolls between Show more and Show less.

Examples and variants

Basic

Press Show more.

Three files changed.

Example
function CollapsibleBasic() {
  return (
    <Collapsible className="w-full max-w-xs text-sm">
      <p>Three files changed.</p>
      <CollapsibleContent className="text-muted-foreground">Brief.pdf, Moodboard.fig, Notes.md</CollapsibleContent>
      <CollapsibleTrigger render={<Button size="sm" variant="ghost" className="-ms-3 mt-1" />}>
        <CollapsibleLabel closed="Show more" open="Show less" />
      </CollapsibleTrigger>
    </Collapsible>
  )
}

Spec drawing

Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.

Install

With the CLI

The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.

Terminal
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/collapsible.json

By hand

No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.

  1. Install the packages it uses.

    Terminal
    npm install @base-ui/react cn
  2. Copy this file to the same paths in your project. Imports use the @/ alias for your project root.

    • components/ui/collapsible.tsxcollapsible
      components/ui/collapsible.tsx
      "use client"
      
      import * as React from "react"
      import { Collapsible as CollapsiblePrimitive } from "@base-ui/react/collapsible"
      import { cn } from "cn"
      
      /*
        Collapsible: one section that shows and hides. Height moves on
        --ease-sheet. CollapsibleLabel rolls between its closed and open text
        ("Show more" ↔ "Show less") with the same spring roll as other labels.
      */
      const OpenContext = React.createContext(false)
      
      function Collapsible({ open, defaultOpen, onOpenChange, ...props }: CollapsiblePrimitive.Root.Props) {
        const [inner, setInner] = React.useState(defaultOpen ?? false)
        const isOpen = open ?? inner
        return (
          <OpenContext.Provider value={isOpen}>
            <CollapsiblePrimitive.Root
              data-slot="collapsible"
              open={isOpen}
              onOpenChange={(next, details) => {
                setInner(next)
                onOpenChange?.(next, details)
              }}
              {...props}
            />
          </OpenContext.Provider>
        )
      }
      
      function CollapsibleTrigger(props: CollapsiblePrimitive.Trigger.Props) {
        return <CollapsiblePrimitive.Trigger data-slot="collapsible-trigger" {...props} />
      }
      
      function CollapsibleContent({ className, children, ...props }: CollapsiblePrimitive.Panel.Props) {
        return (
          <CollapsiblePrimitive.Panel data-slot="collapsible-content" className="collapsible-panel overflow-hidden" {...props}>
            <div className={cn("pt-stack-sm", className)}>{children}</div>
          </CollapsiblePrimitive.Panel>
        )
      }
      
      /** Text that rolls between two states with the collapsible. */
      function CollapsibleLabel({ closed, open }: { closed: React.ReactNode; open: React.ReactNode }) {
        const isOpen = React.useContext(OpenContext)
        return (
          <span className="relative inline-grid overflow-hidden">
            <span aria-hidden={isOpen} data-position={isOpen ? "above" : "at"} className="label-roll col-start-1 row-start-1">
              {closed}
            </span>
            <span aria-hidden={!isOpen} data-position={isOpen ? "at" : "below"} className="label-roll col-start-1 row-start-1">
              {open}
            </span>
          </span>
        )
      }
      
      export { Collapsible, CollapsibleTrigger, CollapsibleContent, CollapsibleLabel }
      

Imports

What the examples on this page import:

Imports
import { InfoIcon } from "lucide-react"
import { Alert, AlertTitle } from "@/components/ui/alert"
import { AspectRatio } from "@/components/ui/aspect-ratio"
import { Button } from "@/components/ui/button"
import { Collapsible, CollapsibleContent, CollapsibleLabel, CollapsibleTrigger } from "@/components/ui/collapsible"
import { DirectionProvider } from "@/components/ui/direction"
import { Kbd, KbdGroup } from "@/components/ui/kbd"
import { ScrollArea } from "@/components/ui/scroll-area"
import { Heading, Text } from "@/components/ui/typography"

Anatomy

  1. 1Trigger · A Button.
  2. 2Panel · Height animates.
  3. 3Label · CollapsibleLabel rolls.

Props

PropTypeDefaultDescription
open / defaultOpenboolean—State.

Tokens

Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.

This utility has no component tokens; it uses semantic tokens directly.

TokenPoints toLightDark

Motion

TriggerWhat happensTokens
ToggleHeight on the sheet curve; label rolls on a spring.--duration-medium, --spring-snappy

Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.

Accessibility

  • aria-expanded on the trigger.

Code

components/ui/collapsible.tsx
"use client"

import * as React from "react"
import { Collapsible as CollapsiblePrimitive } from "@base-ui/react/collapsible"
import { cn } from "cn"

/*
  Collapsible: one section that shows and hides. Height moves on
  --ease-sheet. CollapsibleLabel rolls between its closed and open text
  ("Show more" ↔ "Show less") with the same spring roll as other labels.
*/
const OpenContext = React.createContext(false)

function Collapsible({ open, defaultOpen, onOpenChange, ...props }: CollapsiblePrimitive.Root.Props) {
  const [inner, setInner] = React.useState(defaultOpen ?? false)
  const isOpen = open ?? inner
  return (
    <OpenContext.Provider value={isOpen}>
      <CollapsiblePrimitive.Root
        data-slot="collapsible"
        open={isOpen}
        onOpenChange={(next, details) => {
          setInner(next)
          onOpenChange?.(next, details)
        }}
        {...props}
      />
    </OpenContext.Provider>
  )
}

function CollapsibleTrigger(props: CollapsiblePrimitive.Trigger.Props) {
  return <CollapsiblePrimitive.Trigger data-slot="collapsible-trigger" {...props} />
}

function CollapsibleContent({ className, children, ...props }: CollapsiblePrimitive.Panel.Props) {
  return (
    <CollapsiblePrimitive.Panel data-slot="collapsible-content" className="collapsible-panel overflow-hidden" {...props}>
      <div className={cn("pt-stack-sm", className)}>{children}</div>
    </CollapsiblePrimitive.Panel>
  )
}

/** Text that rolls between two states with the collapsible. */
function CollapsibleLabel({ closed, open }: { closed: React.ReactNode; open: React.ReactNode }) {
  const isOpen = React.useContext(OpenContext)
  return (
    <span className="relative inline-grid overflow-hidden">
      <span aria-hidden={isOpen} data-position={isOpen ? "above" : "at"} className="label-roll col-start-1 row-start-1">
        {closed}
      </span>
      <span aria-hidden={!isOpen} data-position={isOpen ? "at" : "below"} className="label-roll col-start-1 row-start-1">
        {open}
      </span>
    </span>
  )
}

export { Collapsible, CollapsibleTrigger, CollapsibleContent, CollapsibleLabel }