Set up the token layer and the StyleX compiler, if you have not already. See
Installation, and paste state-styles.ts from the same
page. The trigger reads its open state and the panel reads its transition
state through it. The examples render the
trigger as a Button.
Paste this source into the file named in its title.
Change the @/ imports if your project uses different aliases.
Usage
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@/components/ui/collapsible"
<Collapsible> <CollapsibleTrigger>What changed in 2.4?</CollapsibleTrigger> <CollapsibleContent>Offline editing, and a fix for sync on slow networks.</CollapsibleContent></Collapsible>
CollapsibleTrigger is unstyled. Pass render={<Button />} to make it a
button. CollapsibleContent renders the Base UI Panel around an inner
element that holds the content box. The panel animates its height over
300ms when it opens and closes.
Put the space above or below the panel inside it, as padding on
CollapsibleContent, not as a gap or margin around it. A gap outside stays at
full size while the panel shrinks to nothing, then disappears when the panel
unmounts, so the close ends with a jump.
Controlled state
Pass open and onOpenChange to keep the open state in your own component.
Pass render to replace a part's element or compose it with another component.
Every part wraps the Base UI Collapsible part of the same name and accepts
its props. See the Base UI reference
for the full list. Each part also takes xstyle and className.
CollapsibleTrigger and CollapsibleContent take style as real inline CSS,
merged over the compiled styles.
Collapsible
Prop
Type
Default
open
boolean
—
defaultOpen
boolean
false
onOpenChange
(open: boolean, details) => void
—
disabled
boolean
false
CollapsibleContent
Prop
Type
Default
keepMounted
boolean
false
hiddenUntilFound
boolean
false
hiddenUntilFound hides the closed panel with hidden="until-found" instead
of unmounting it, which lets the browser's find-in-page reach the text and open
the panel. It overrides keepMounted.
CollapsibleContent applies xstyle and className to the inner element and
style to the panel. The panel
animates its height between 0 and the measured content height, and a padded
box of height 0 still draws its padding.
collapsibleStyles
Styles for an icon inside the trigger. Each reads the open state from the
trigger and has its own transition.
Key
What it is
chevron
Turns a down-pointing chevron up when the panel opens
chevronInline
Turns a right-pointing chevron down when the panel opens
Data attributes
Attribute
On
Effect
data-slot="collapsible-content"
the panel
Targets the animated panel
data-starting-style, data-ending-style
the panel
Base UI writes them while the panel animates
data-open, data-disabled
root, trigger, panel
Base UI writes them
data-panel-open
the trigger
Base UI writes it
CSS variables
Variable
On
Effect
--collapsible-panel-height
the panel
Base UI writes the measured height; the panel transitions to it
--arene-collapsible-open
the trigger
0 closed, 1 open. Inherited, so anything inside the trigger can read it