Collapsible
An interactive component that can be expanded or collapsed.
Anatomy
<Collapsible.Root>
<Collapsible.Trigger>
<Collapsible.Indicator />
</Collapsible.Trigger>
<Collapsible.Content />
</Collapsible.Root>
Examples
Disabled
Use the disabled prop to disable the collapsible and prevent it from being toggled.
Partial Collapse
Use the collapsedHeight or collapsedWidth props to create a "show more/less" pattern. When set, the content
maintains the specified dimensions when collapsed instead of collapsing to 0px.
We expose the --collapsed-height or --collapsed-width variables to use in your CSS animations.
Interactive elements (links, buttons, inputs) within the collapsed area automatically become
inertto prevent keyboard navigation to hidden content.
Nested Collapsibles
You can nest collapsibles within collapsibles to create hierarchical content structures.
Lazy Mount
Use lazyMount to delay mounting the content until first opened, and unmountOnExit to remove it from the DOM when
collapsed. Combining both ensures the component is only in the DOM while expanded.
Root Provider
An alternative way to control the collapsible is to use the RootProvider component and the useCollapsible hook. This
way you can access the state and methods from outside the component.
Guides
Indicator Animation
To rotate the indicator icon (such as a chevron) when the collapsible opens and closes, use CSS transforms with the
data-state attribute:
[data-scope='collapsible'][data-part='indicator'] {
transition: transform 200ms;
&[data-state='open'] {
transform: rotate(180deg);
}
}
Open vs Visible
When using useCollapsible or useCollapsibleContext, you can access the open and visible state properties. They
seem similar but serve different purposes:
-
open: Indicates the intended state of the collapsible. This istruewhen the collapsible should be expanded andfalsewhen it should be collapsed. This changes immediately when triggered. -
visible: Indicates whether the content is currently visible in the DOM. This accounts for exit animations - the content remainsvisiblewhile the closing animation plays, even thoughopenis alreadyfalse. Once the animation completes,visiblebecomesfalse.
Content Animation
Use the --height and/or --width CSS variables to animate the size of the content when it expands or closes.
If you use collapsedHeight or collapsedWidth, update your CSS animations to use the --collapsed-height or
--collapsed-width variables as the starting/ending point:
@keyframes expand {
from {
height: var(--collapsed-height, 0);
}
to {
height: var(--height);
}
}
@keyframes collapse {
from {
height: var(--height);
}
to {
height: var(--collapsed-height, 0);
}
}
[data-scope='collapsible'][data-part='content'] {
&[data-state='open'] {
animation: expand 250ms;
}
&[data-state='closed'] {
animation: collapse 250ms;
}
}
API Reference
Props
Root
Renders a <div> element.
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
collapsedHeight | string | numberThe height of the content when collapsed. | |
collapsedWidth | string | numberThe width of the content when collapsed. | |
defaultOpen | booleanThe initial open state of the collapsible when rendered. Use when you don't need to control the open state of the collapsible. | |
disabled | booleanWhether the collapsible is disabled. | |
ids | Partial<{ root: string; content: string; trigger: string }>The ids of the elements in the collapsible. Useful for composition. | |
lazyMount | false | booleanWhether to enable lazy mounting |
onExitComplete | VoidFunctionThe callback invoked when the exit animation completes. | |
onOpenChange | (details: OpenChangeDetails) => voidThe callback invoked when the open state changes. | |
open | booleanThe controlled open state of the collapsible. | |
unmountOnExit | false | booleanWhether to unmount on exit. |
| Attribute | Description |
|---|---|
[data-scope] | collapsible |
[data-part] | root |
[data-state] | "open" | "closed" |
Content
Renders a <div> element.
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
| Attribute | Description |
|---|---|
[data-scope] | collapsible |
[data-part] | content |
[data-collapsible] | |
[data-state] | "open" | "closed" |
[data-disabled] | Present when disabled |
[data-has-collapsed-size] | Present when the content has collapsed width or height |
| CSS Variable | Description |
|---|---|
--height | The height of the element |
--width | The width of the element |
--collapsed-height | The height of the Content |
--collapsed-width | The width of the Content |
Indicator
Renders a <div> element.
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
| Attribute | Description |
|---|---|
[data-scope] | collapsible |
[data-part] | indicator |
[data-state] | "open" | "closed" |
[data-disabled] | Present when disabled |
RootProvider
Renders a <div> element.
| Prop | Default | Type |
|---|---|---|
value | UseCollapsibleReturn | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Trigger
Renders a <button> element.
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
| Attribute | Description |
|---|---|
[data-scope] | collapsible |
[data-part] | trigger |
[data-state] | "open" | "closed" |
[data-disabled] | Present when disabled |
Context
API
| Property | Type |
|---|---|
open | booleanWhether the collapsible is open. |
visible | booleanWhether the collapsible is visible (open or closing) |
disabled | booleanWhether the collapsible is disabled |
setOpen | (open: boolean) => voidFunction to open or close the collapsible. |
measureSize | VoidFunctionFunction to measure the size of the content. |
Accessibility
Keyboard Support
| Key | Description |
|---|---|
Space | Opens/closes the collapsible. |
Enter | Opens/closes the collapsible. |