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
Set disabled to stop the collapsible from toggling.
Partial Collapse
Set collapsedHeight or collapsedWidth for a "show more/less" pattern. The content keeps that size when collapsed
instead of shrinking to 0px. Use the --collapsed-height or --collapsed-width variables in your CSS animations.
Interactive elements in the collapsed area become
inert, so keyboard users can't reach hidden content.
Nested Collapsibles
Nest collapsibles to build hierarchical content.
Lazy Mount
Set lazyMount to mount the content on first open and unmountOnExit to remove it on collapse. With both, the content
is in the DOM only while expanded.
Root Provider
Use RootProvider with the useCollapsible hook to access the state and methods from outside the component.
Guides
Indicator Animation
Rotate the indicator on open with the data-state attribute:
[data-collapsible-indicator] {
transition: transform 200ms;
&[data-state='open'] {
transform: rotate(180deg);
}
}
Open vs Visible
useCollapsible and useCollapsibleContext expose both open and visible.
open: the intended state. It changes as soon as the collapsible is toggled.visible: whether the content is shown. It staystruewhile the exit animation plays, even thoughopenis alreadyfalse, and turnsfalsewhen the animation ends.
Content Animation
Animate the content size with the --height and --width CSS variables. With collapsedHeight or collapsedWidth,
use --collapsed-height or --collapsed-width as the start and end 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-collapsible-content] {
&[data-state='open'] {
animation: expand 250ms;
}
&[data-state='closed'] {
animation: collapse 250ms;
}
}
API Reference
Props
Root
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. 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. | |
hiddenUntilFound | booleanWhether the closed content stays searchable with the browser's find-in-page. Uses `hidden="until-found"`, and opens the collapsible when a match is found inside it. | |
hideMode | 'display-none' | HideModeHow to hide content when mounted but not present. - `'display-none'`: HTML `hidden` attribute. Effects stay alive. - `'activity'`: React 19 `<Activity mode="hidden">`. Effects pause. Requires React 19+. |
ids | ElementIdsThe 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. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. | |
state | {}The state of the part, forwarded to the `render` function. Set by the component, not the consumer. | |
unmountOnExit | false | booleanWhether to unmount on exit. |
| Attribute | Description |
|---|---|
[data-collapsible-root] | <uid> |
[data-state] | "open" | "closed" |
[data-disabled] | Present when disabled |
Content
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<CollapsibleContentState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-collapsible-content] | <uid> |
[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
Defines the <span> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-collapsible-indicator] | <uid> |
[data-state] | "open" | "closed" |
[data-disabled] | Present when disabled |
RootProvider
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
value | UseCollapsibleReturn | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
Trigger
Defines the <button> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<CollapsibleTriggerState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-collapsible-trigger] | <uid> |
[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, reason?: OpenChangeReason) => voidFunction to open or close the collapsible. |
measureSize | VoidFunctionFunction to measure the size of the content. |
getRootState | () => RootStateReturns the state of the root |
getContentState | () => ContentStateReturns the state of the content |
Accessibility
Keyboard Support
| Key | Description |
|---|---|
Space | Opens/closes the collapsible. |
Enter | Opens/closes the collapsible. |