Collapsible

A section of content that the user can show or hide with a trigger.

Usage

Code
import { Collapsible } from '@leverui/components';
Code
<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.

Controlled

Use the open and onOpenChange props to control the collapsible state.

Status: Closed

Store

An alternative way to control the collapsible is to use the RootProvider component and the useCollapsible store hook.

State: Collapsed

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.

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

Code
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:

Code
<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:

Code
<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

PropDefaultTypeDescription
asdivNative tagHTML tag to render.
asChild—Solid render functionRender your own element with the collapsible's props.
unstyled—booleanTurn off the default styles. Behavior stays.
collapsedHeight—string | numberContent height when collapsed.
collapsedWidth—string | numberContent width when collapsed.
defaultOpen—booleanStart open.
disabled—booleanDisable the collapsible.
ids—Partial<{ root: string; content: string; trigger: string }>Your own ids for the elements.
lazyMountfalsebooleanRender the content on first open.
onExitComplete—VoidFunctionCalled when the closing animation finishes.
onOpenChange—(details: OpenChangeDetails) => voidCalled when the collapsible opens or closes.
open—booleanOpen state, when you control it.
unmountOnExitfalsebooleanRemove 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