Collapsible
A section of content that the user can show or hide with a trigger.
Use the components as they are, or restyle them with your own CSS. You can start with one component and add more of Lever UI as your app grows.
Usage
import { Collapsible } from '@leverui/components';
<Collapsible.Root>
<Collapsible.Trigger>
<Collapsible.Indicator />
</Collapsible.Trigger>
<Collapsible.Content />
</Collapsible.Root>
Examples
Initial Open
Use the defaultOpen prop to render the collapsible in an open state by default.
A collapsible hides content until someone asks for it. Use one when a page has details that only some readers need, such as delivery terms, a long answer to a common question, or the full text of a review. The trigger says what is inside, and the content opens in place so nobody loses their spot on the page. When the content is long, you can keep the first few lines visible as a preview and let people open the rest.
A collapsible hides content until someone asks for it. Use one when a page has details that only some readers need, such as delivery terms, a long answer to a common question, or the full text of a review. The trigger says what is inside, and the content opens in place so nobody loses their spot on the page. When the content is long, you can keep the first few lines visible as a preview and let people open the rest.
Partial Height
Use the collapsedHeight prop to show a preview of the content when collapsed.
A collapsible hides content until someone asks for it. Use one when a page has details that only some readers need, such as delivery terms, a long answer to a common question, or the full text of a review. The trigger says what is inside, and the content opens in place so nobody loses their spot on the page. When the content is long, you can keep the first few lines visible as a preview and let people open the rest.
A collapsible hides content until someone asks for it. Use one when a page has details that only some readers need, such as delivery terms, a long answer to a common question, or the full text of a review. The trigger says what is inside, and the content opens in place so nobody loses their spot on the page. When the content is long, you can keep the first few lines visible as a preview and let people open the rest.
Disabled
Use the disabled prop to prevent the collapsible from being toggled.
disabled prop.Controlled
Use the open and onOpenChange props to control the collapsible state.
Store
An alternative way to control the collapsible is to use the RootProvider component and the useCollapsible store hook.
useCollapsible hook with RootProvider lets you read the collapsible's state and open or close it from anywhere in your component.Lazy Mounted
Use the unmountOnExit prop to make the content unmount when collapsed.
Hide Mode
Solid has no Activity boundary, so Lever UI has no hideMode prop. Collapsed content stays mounted but hidden, and its effects keep running. To pause work while collapsed, read the open state with Collapsible.Context, as this counter does.
Collapse this for a few seconds, then reopen it. The counter pauses while collapsed and resumes from where it stopped.
Effect ticks: 0
Use unmountOnExit to remove the subtree instead of hiding it.
Guides
Accessing collapsible context
Call useCollapsibleContext in any component inside the collapsible. It
returns an accessor.
import { Collapsible, useCollapsibleContext } from '@leverui/components';
function CollapsibleStatus() {
const collapsible = useCollapsibleContext();
return <div>Collapsible is {collapsible().open ? 'open' : 'closed'}</div>;
}
function MyCollapsible() {
return (
<Collapsible.Root>
<Collapsible.Trigger>Toggle</Collapsible.Trigger>
<Collapsible.Content>
<CollapsibleStatus />
</Collapsible.Content>
</Collapsible.Root>
);
}
Closing programmatically
Call setOpen(false) on the context.
import { Button, Collapsible, useCollapsibleContext } from '@leverui/components';
function CloseButton() {
const collapsible = useCollapsibleContext();
return <Button onClick={() => collapsible().setOpen(false)}>Close Collapsible</Button>;
}
function MyCollapsible() {
return (
<Collapsible.Root>
<Collapsible.Trigger>Toggle</Collapsible.Trigger>
<Collapsible.Content>
<CloseButton />
</Collapsible.Content>
</Collapsible.Root>
);
}
Content Padding
Put padding on an element inside Collapsible.Content, not on
Collapsible.Content itself. Padding on it makes the animation stutter.
Do this:
<Collapsible.Root>
<Collapsible.Trigger>Toggle</Collapsible.Trigger>
<Collapsible.Content>
<Box style={{ padding: '1rem', border: '1px solid' }}>{/* Content here */}</Box>
</Collapsible.Content>
</Collapsible.Root>
Not this:
<Collapsible.Root>
<Collapsible.Trigger>Toggle</Collapsible.Trigger>
{/* Avoid applying padding directly to Content */}
<Collapsible.Content style={{ padding: '1rem', border: '1px solid' }}>
{/* Content here */}
</Collapsible.Content>
</Collapsible.Root>
Props
Root
| Prop | Default | Type | Description |
|---|---|---|---|
as | div | Native tag | HTML tag to render. |
asChild | — | Solid render function | Render your own element with the collapsible's props. |
unstyled | — | boolean | Turn off the default styles. Behavior stays. |
collapsedHeight | — | string | number | Content height when collapsed. |
collapsedWidth | — | string | number | Content width when collapsed. |
defaultOpen | — | boolean | Start open. |
disabled | — | boolean | Disable the collapsible. |
ids | — | Partial<{ root: string; content: string; trigger: string }> | Your own ids for the elements. |
lazyMount | false | boolean | Render the content on first open. |
onExitComplete | — | VoidFunction | Called when the closing animation finishes. |
onOpenChange | — | (details: OpenChangeDetails) => void | Called when the collapsible opens or closes. |
open | — | boolean | Open state, when you control it. |
unmountOnExit | false | boolean | Remove the content when the collapsible closes. |
Give the trigger a visible name that says what it opens. With
collapsedHeight, check that controls in the cut-off preview still work.
Collapsible.RootProvider, Collapsible.PropsProvider, and the context hooks
are for building your own compositions.
Customize with CSS
Classes: leverui-collapsible__root, leverui-collapsible__trigger,
leverui-collapsible__content, leverui-collapsible__indicator. State is on
the data-state attributes. unstyled keeps the behavior and the
accessibility features.
Explorer
Click a part name to highlight it in the preview.
Component Anatomy
Hover to highlight, click to select parts