Collapsible

An interactive component that can be expanded or collapsed.

Loading...

Anatomy

Shipping detailsroottriggerindicatorcontent
<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 stays true while the exit animation plays, even though open is already false, and turns false when 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.

AttributeDefaultType
asChild
boolean

Use 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 | number

The height of the content when collapsed.

collapsedWidth
string | number

The width of the content when collapsed.

defaultOpen
boolean

The initial open state of the collapsible when rendered. Use when you don't need to control the open state of the collapsible.

disabled
boolean

Whether the collapsible is disabled.

hiddenUntilFound
boolean

Whether 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'
HideMode

How 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
ElementIds

The ids of the elements in the collapsible. Useful for composition.

lazyMountfalse
boolean

Whether to enable lazy mounting

onExitComplete
VoidFunction

The callback invoked when the exit animation completes.

onOpenChange
(details: OpenChangeDetails) => void

The callback invoked when the open state changes.

open
boolean

The 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.

unmountOnExitfalse
boolean

Whether to unmount on exit.

AttributeDescription
[data-collapsible-root]<uid>
[data-state]"open" | "closed"
[data-disabled]Present when disabled

Content

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use 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.

AttributeDescription
[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 VariableDescription
--heightThe height of the element
--widthThe width of the element
--collapsed-heightThe height of the Content
--collapsed-widthThe width of the Content

Indicator

Defines the <span> element.

AttributeDefaultType
asChild
boolean

Use 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.

AttributeDescription
[data-collapsible-indicator]<uid>
[data-state]"open" | "closed"
[data-disabled]Present when disabled

RootProvider

Defines the <div> element.

AttributeDefaultType
value
UseCollapsibleReturn

asChild
boolean

Use 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.

AttributeDefaultType
asChild
boolean

Use 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.

AttributeDescription
[data-collapsible-trigger]<uid>
[data-state]"open" | "closed"
[data-disabled]Present when disabled

Context

API

PropertyType
open
boolean

Whether the collapsible is open.

visible
boolean

Whether the collapsible is visible (open or closing)

disabled
boolean

Whether the collapsible is disabled

setOpen
(open: boolean, reason?: OpenChangeReason) => void

Function to open or close the collapsible.

measureSize
VoidFunction

Function to measure the size of the content.

getRootState
() => RootState

Returns the state of the root

getContentState
() => ContentState

Returns the state of the content

Accessibility

Keyboard Support

KeyDescription
Space
Opens/closes the collapsible.
Enter
Opens/closes the collapsible.