Skip to main content

Collapsible

Disclose one content region from a caller-placed trigger.

@solidjs starred 3 repositories
@solidjs/router
Props
Slots

Anatomy#

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 when several disclosures share selection rules.

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.

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

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

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

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.

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.

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.

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.

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
data-closed
Slot: collapsiblecollapsible-triggercollapsible-content
Description: Present when disclosure or transition content is closed.
data-expanded
Slot: collapsiblecollapsible-triggercollapsible-content
Description: Present when the panel, accordion, or menu is expanded.
data-disabled
Slot: collapsible-trigger
Description: Present when the component, slot, or item is disabled.

Props#

Collapsible#

Props for the Collapsible component. Renders a <div> element by default.

Prop

Trigger#

Renders a <button> element by default.

Prop

Content#

Renders a <div> element by default.

Prop