Collapsible
An interactive component that expands and collapses a panel.
Order #4189
Installation
Install the following dependencies:
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.
Copy and paste the following code into your project.
Update the import paths to match your project setup.
Usage
Composition
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
Use the open and onOpenChange props to control the state.
Basic
Spread collapsibleStyles.chevron on an icon inside the trigger to turn it
with the panel.
Settings panel
Use a trigger button to reveal additional settings.
File tree
Use nested collapsibles to build a file tree. collapsibleStyles.chevronInline
turns a right-pointing chevron down.
API reference
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 |
--carbon-collapsible-open | the trigger | 0 closed, 1 open. Inherited, so anything inside the trigger can read it |