---
title: Collapsible
description: Disclose one content region from a caller-placed trigger.
package: moraine
version: 0.11.0
repository: https://github.com/subframe7536/moraine
---

# Collapsible

> Disclose one content region from a caller-placed trigger.

## Anatomy

```text
Collapsible [component; slot=root]
├── Collapsible.Trigger [part; slot=trigger]
└── contentWrapper [internal]
    └── Collapsible.Content [part; slot=content]
```

## Usage

Use Collapsible for one disclosure whose trigger and content belong to the surrounding layout. Use
[Accordion](https://moraine.subf.dev/components/accordion.md) when several disclosures share selection rules.

```tsx
import { Collapsible } from 'moraine'

export function Example() {
  return (
    <Collapsible>
      <Collapsible.Trigger>Details</Collapsible.Trigger>
      <Collapsible.Content>More information</Collapsible.Content>
    </Collapsible>
  )
}
```

### State ownership

Use `open` with `onOpenChange` for controlled state, or `defaultOpen` when the component owns its
initial state. `onOpenChange` receives the next state when the user toggles a trigger, and does not
fire when you change `open` yourself. With a controlled `open`, the state changes only when you
update it. The trigger and content parts can sit anywhere inside the root, so you can compose them
into a custom layout.

`disabled` on `Collapsible` blocks every trigger and keeps the current open state. `disabled` on
`Collapsible.Trigger` blocks only that trigger. Set `transition` to animate the content height. The
content then stays mounted until the closing animation finishes, and the measured height is exposed
as `--mo-collapsible-content-height` on the root. Animation is skipped for `prefers-reduced-motion`.

```tsx
<Collapsible open={open()} onOpenChange={setOpen}>
  <Collapsible.Trigger>Advanced settings</Collapsible.Trigger>
  <Collapsible.Content>Telemetry endpoints</Collapsible.Content>
</Collapsible>
```

### Content lifetime

The content shell stays mounted so the trigger's `aria-controls` points to a real element even while
closed. `forceMount` controls the content inside it. By default, content mounts on first open and
unmounts after it closes. Set `forceMount` when content holds state that should survive closing; it
mounts the content even before the first open. Mounted closed content stays hidden and `inert`, and
browser find-in-page can reveal it, which opens the Collapsible and calls `onOpenChange`.

```tsx
<Collapsible forceMount defaultOpen>
  <Collapsible.Trigger>Filters</Collapsible.Trigger>
  <Collapsible.Content>
    <Input defaultValue="tag:moraine" />
  </Collapsible.Content>
</Collapsible>
```

### Styling

Configure `root`, `trigger`, and `content` through `collapsible.base` in your Theme. The root and
Content expose `data-expanded` and `data-closed` for state styling, and the Trigger also exposes
`data-disabled`. Collapsible also renders an internal wrapper with `data-expanded`, `data-closed`,
and `data-transition` for measurement, height animation, presence, and accessibility. You cannot
configure that wrapper through Theme, `classes`, `styles`, or Content props.

Root `classes` and `styles` set defaults for this Collapsible. Use `class` and `style` directly on
the root, Trigger, or Content to style those elements. Content's `ref` and `as` apply to the inner
Content element. There are no visual variants.

```tsx
<Collapsible classes={{ trigger: 'font-medium', content: 'text-sm text-muted-foreground' }}>
  <Collapsible.Trigger>Details</Collapsible.Trigger>
  <Collapsible.Content>More information</Collapsible.Content>
</Collapsible>
```

### Keyboard interaction

The trigger renders a `button` by default, or an `a` when you pass `href`. Pass `as` to render a
different element or component, which then follows its own keyboard semantics.

## Examples

### Answer a password FAQ

Leave the disclosure uncontrolled when the page does not need to read open state.

```tsx
import { Card, Collapsible, Icon, Switch } from 'moraine'
import { createSignal } from 'solid-js'

export function Uncontrolled() {
  const [transition, setTransition] = createSignal(true)

  return (
    <div class="max-w-xs w-full space-y-3">
      <Switch label="Transition" checked={transition()} onCheckedChange={setTransition} />

      <div class="h-40">
        <Card class="rounded-lg">
          <Card.Body class="px-3 py-3">
            <Collapsible transition={transition()}>
              <Collapsible.Trigger class="group flex w-full items-center justify-between">
                <span>How do I reset my password?</span>
                <Icon
                  name="i-lucide-chevron-down"
                  aria-hidden="true"
                  class="group-data-expanded:rotate-180 transition-transform"
                />
              </Collapsible.Trigger>
              <Collapsible.Content>
                <div class="text-sm text-muted-foreground pt-3">
                  You can reset your password from Account settings. We send a verification link to
                  the primary email on the workspace.
                </div>
              </Collapsible.Content>
            </Collapsible>
          </Card.Body>
        </Card>
      </div>
    </div>
  )
}
```

### Inspect a locked invoice

Own `open` when an external control or a lock switch must drive the same panel.

```tsx
import { Button, Collapsible, Icon, Switch } from 'moraine'
import { createSignal } from 'solid-js'

export function Controlled() {
  const [open, setOpen] = createSignal(true)
  const [locked, setLocked] = createSignal(false)

  return (
    <div class="max-w-md w-full space-y-3">
      <div class="flex flex-wrap gap-2 items-center">
        <Button size="sm" onClick={() => !locked() && setOpen((value) => !value)}>
          {open() ? 'Hide invoice details' : 'Show invoice details'}
        </Button>
        <Switch
          label="Lock edits"
          checked={locked()}
          onCheckedChange={setLocked}
          checkedIcon="i-lucide-lock"
          uncheckedIcon="i-lucide-lock-open"
        />
      </div>

      <Collapsible
        open={open()}
        disabled={locked()}
        onOpenChange={setOpen}
        class="b-1 b-border rounded-lg w-full"
      >
        <Collapsible.Trigger class="group text-sm font-medium px-4 py-3 text-left flex w-full items-center justify-between data-disabled:opacity-60">
          <span>June invoice #INV-2048</span>
          <Icon
            name="i-lucide-chevron-down"
            aria-hidden="true"
            class="group-data-expanded:rotate-180 text-muted-foreground transition-transform"
          />
        </Collapsible.Trigger>
        <Collapsible.Content class="text-sm text-foreground px-4 pb-4">
          <div class="space-y-2">
            <div class="flex items-center justify-between">
              <span class="text-muted-foreground">Plan</span>
              <span>Team Pro</span>
            </div>
            <div class="flex items-center justify-between">
              <span class="text-muted-foreground">Seats</span>
              <span>18 active</span>
            </div>
            <div class="text-muted-foreground flex gap-2 items-center">
              <Icon name="i-lucide-info" />
              <span>Locked invoices cannot be expanded from the row trigger.</span>
            </div>
          </div>
        </Collapsible.Content>
      </Collapsible>
    </div>
  )
}
```

### Reveal extra starred repositories

Place the trigger beside a heading when only some rows should expand. Rows that stay visible can sit
outside `Collapsible.Content`.

```tsx
import { Button, Card, Collapsible, Icon } from 'moraine'

export function Composable() {
  return (
    <div class="max-w-md w-full">
      <Card class="rounded-xl">
        <Card.Body class="p-4">
          <Collapsible transition>
            <div class="flex items-center justify-between">
              <span class="text-sm text-foreground font-semibold">
                @solidjs starred 3 repositories
              </span>
              <Collapsible.Trigger
                as={Button}
                variant="ghost"
                size="icon-sm"
                aria-label="Toggle repositories"
              >
                <Icon name="i-lucide-chevrons-up-down" class="size-4" />
              </Collapsible.Trigger>
            </div>

            <div class="text-sm font-mono mt-2 px-4 py-2 border border-border rounded-md">
              @solidjs/router
            </div>

            <Collapsible.Content class="pt-2 space-y-2">
              <div class="text-sm font-mono px-4 py-2 border border-border rounded-md">
                @solidjs/testing-library
              </div>
              <div class="text-sm font-mono px-4 py-2 border border-border rounded-md">
                subframe7536/moraine
              </div>
            </Collapsible.Content>
          </Collapsible>
        </Card.Body>
      </Card>
    </div>
  )
}
```

### Toggle advanced settings from outside the panel

Pass `open` and `onOpenChange` so a second control can show or hide the same region.

```tsx
import { Button, Collapsible } from 'moraine'
import { createSignal } from 'solid-js'

export function StateOwnership() {
  const [open, setOpen] = createSignal(false)

  return (
    <div class="max-w-md w-full space-y-3">
      <Collapsible open={open()} onOpenChange={setOpen}>
        <Collapsible.Trigger as={Button} size="xs" variant="outline">
          {open() ? 'Hide advanced settings' : 'Show advanced settings'}
        </Collapsible.Trigger>
        <Collapsible.Content class="text-sm text-muted-foreground mt-2 p-3 b-1 b-border rounded-lg">
          Advanced configuration parameters and telemetry endpoints.
        </Collapsible.Content>
      </Collapsible>
      <div class="flex gap-2 items-center">
        <Button size="xs" variant="outline" onClick={() => setOpen((v) => !v)}>
          External toggle ({open() ? 'open' : 'closed'})
        </Button>
      </div>
    </div>
  )
}
```

### Keep filter text while the panel is closed

Set `forceMount` so fields inside Content keep their values.

```tsx
import { Button, Collapsible, Input } from 'moraine'

export function ContentLifetime() {
  return (
    <div class="max-w-md w-full">
      <Collapsible forceMount defaultOpen>
        <Collapsible.Trigger as={Button} size="xs" variant="outline">
          Persistent filter fields
        </Collapsible.Trigger>
        <Collapsible.Content class="mt-2 p-3 b-1 b-border rounded-lg space-y-2">
          <p class="text-xs text-muted-foreground">
            Input state persists while closed when forceMount is set.
          </p>
          <Input defaultValue="tag:moraine" placeholder="Search filter" />
        </Collapsible.Content>
      </Collapsible>
    </div>
  )
}
```

## API Reference

### Attributes

| Attributes | Slot | Description |
| --- | --- | --- |
| `data-closed` | `collapsible`, `collapsible-trigger`, `collapsible-content` | Present when disclosure or transition content is closed. |
| `data-expanded` | `collapsible`, `collapsible-trigger`, `collapsible-content` | Present when the panel, accordion, or menu is expanded. |
| `data-disabled` | `collapsible-trigger` | Present when the component, slot, or item is disabled. |

### Props

#### Collapsible

Props for the Collapsible component.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| defaultOpen | boolean \| undefined | false | Whether the collapsible is open initially when uncontrolled. |
| disabled | boolean \| undefined | false | Whether the collapsible is disabled: its triggers cannot toggle it and `onOpenChange` is not<br>called. The current open state is kept. |
| forceMount | boolean \| undefined | false | Whether closed content stays mounted. When `false`, content mounts on first open and<br>unmounts once it closes; when `true`, content mounts even before the first open and stays<br>hidden (and `inert`) while closed. Mounted closed content can be found by browser<br>in-page search, which opens the collapsible. |
| id | string \| undefined | — | Unique identifier for the collapsible root element. The trigger and content derive their<br>IDs from it (`{id}-trigger`, `{id}-content`) for `aria-controls` and `aria-labelledby`. A<br>unique ID is generated when omitted. |
| onOpenChange | ((open: boolean) => void) \| undefined | — | Called with the next open state when the user toggles the collapsible with the trigger, or<br>when in-page search reveals `forceMount`ed content. It is not called while `disabled`, nor<br>when `open` changes from outside. When `open` is controlled, the state only changes if you<br>update `open`. |
| open | boolean \| undefined | — | Whether the collapsible is open (controlled). Leave it `undefined` for uncontrolled state<br>and update it from `onOpenChange`. |
| transition | boolean \| undefined | false | Whether to animate the content height when opening and closing. The content is measured in<br>pixels and exposed as the `--mo-collapsible-content-height` CSS variable on the root, and<br>on close it stays mounted until the animation ends. Animations are skipped with<br>`prefers-reduced-motion`. |
| children | JSX.Element \| undefined | — | Content to render inside the collapsible. |
| class | SlotClassValue \| undefined | — | Class applied to the component root or trigger element. |
| classes | Classes \| undefined | — | Family slot class defaults for this instance. |
| style | SlotStyleValue \| undefined | — | Style applied to the component root or trigger element. |
| styles | Styles \| undefined | — | Family slot style defaults for this instance. |

#### Collapsible.Trigger

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| disabled | boolean \| undefined | — | Whether this trigger is disabled. |
| as | T \| undefined | 'button' | Element or component to render as. A tag name (`'a'`) renders that element and narrows the<br>other props to its attributes; a Solid component receives the props the trigger would put on<br>its element, so it should spread them onto its root and forward `ref`. Own styles and state<br>attributes are kept. |
| children | JSX.Element \| undefined | — | Trigger label and visual content. |
| class | SlotClassValue \| undefined | — | Class applied to the component root or trigger element. |
| style | SlotStyleValue \| undefined | — | Style applied to the component root or trigger element. |

**`as` example**

```tsx
import { Button } from 'moraine'

<Button as="a" href="/docs" target="_blank">Docs</Button>
```

#### Collapsible.Content

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| as | T \| undefined | 'div' | Element or component to render inner content as. |
| children | JSX.Element \| undefined | — | Content to render. |
| class | SlotClassValue \| undefined | — | Class applied to the component root or trigger element. |
| style | SlotStyleValue \| undefined | — | Style applied to the component root or trigger element. |
