Skip to the content
human-kit design
44 — Layout

Collapsible

A button that shows and hides what follows it, the panel opening on its own height.

Installation

pnpm dlx @human-kit/design add collapsible
npx @human-kit/design add collapsible
yarn dlx @human-kit/design add collapsible
bunx @human-kit/design add collapsible

The command copies the files to src/lib/components/ui/collapsible. It also installs the packages that the files import.

Usage

<script lang="ts">
	import * as Collapsible from '$lib/components/ui/collapsible';
</script>

<Collapsible.Root>
	<Collapsible.Trigger>Previous cycles</Collapsible.Trigger>
	<Collapsible.Panel>Two stimulation cycles in 2025, one transfer.</Collapsible.Panel>
</Collapsible.Root>

Guidelines

  • Use a collapsible for one section that most readers skip: older records, advanced settings, the detail behind a total.
  • Use the ghost variant in the flow of a page or a form, where a box would weigh more than what it hides.
  • Use outline for a section that stands on the page on its own, as a card that opens.
  • Use filled where a white card would weigh too much, such as a dense panel or a form: one soft grey panel with no edge, as a filled field.
  • For several sections in a stack that open one at a time, use the accordion instead: it shares the three looks and keeps the group in step.
  • Do not hide what the reader needs to finish the task. A required field inside a closed panel is a field nobody fills.

Accessibility

  • The trigger is a native <button> with aria-expanded, and aria-controls points at the panel: the disclosure pattern of WAI-ARIA.
  • Enter and Space open and close it, as on any button.
  • The closed panel stays in the page, hidden, so a screen reader skips it and the first opening animates too.
  • The chevron is hidden from assistive technology: aria-expanded already says whether it is open.
  • disabled on the root turns the trigger off and takes it out of the tab order.

API reference

Collapsible.Root

PropTypeDefault
variant'ghost' | 'outline' | 'filled'

A quiet row of text, a card of its own, or a soft grey panel with no edge. The trigger and the panel follow it.

'ghost'
openboolean

Whether the panel shows. Bindable.

—
defaultOpenboolean

Whether it starts open, when no open is given.

false
onOpenChange(open: boolean) => void

Runs when the trigger opens or closes the panel.

—
controlledOpenboolean

Stops it writing back to open: it only reports through onOpenChange, so your code can refuse a change.

false
disabledboolean

Turns the trigger off.

false
elementHTMLDivElement | null

The rendered element. Bindable.

—

Plus every attribute of a <div>.

Collapsible.Trigger

PropTypeDefault
elementHTMLButtonElement | null

The rendered button. Bindable.

—

Plus every attribute of a native <button> but type, disabled, aria-expanded and aria-controls, which the root sets.

Collapsible.Panel

PropTypeDefault
forceMountboolean

Keeps the closed panel in the page, hidden. With false, it leaves the page once it has closed.

true
elementHTMLDivElement | null

The rendered element. Bindable.

—

Plus every attribute of a <div>. class styles the inner box that holds the text.