Collapsible
A button that shows and hides what follows it, the panel opening on its own height.
Installation
pnpm dlx @human-kit/design add collapsiblenpx @human-kit/design add collapsibleyarn dlx @human-kit/design add collapsiblebunx @human-kit/design add collapsibleThe 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
ghostvariant in the flow of a page or a form, where a box would weigh more than what it hides. - Use
outlinefor a section that stands on the page on its own, as a card that opens. - Use
filledwhere 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>witharia-expanded, andaria-controlspoints 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-expandedalready says whether it is open. disabledon the root turns the trigger off and takes it out of the tab order.
API reference
Collapsible.Root
| Prop | Type | Default |
|---|---|---|
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' |
open | boolean Whether the panel shows. Bindable. | — |
defaultOpen | boolean Whether it starts open, when no | false |
onOpenChange | (open: boolean) => void Runs when the trigger opens or closes the panel. | — |
controlledOpen | boolean Stops it writing back to | false |
disabled | boolean Turns the trigger off. | false |
element | HTMLDivElement | null The rendered element. Bindable. | — |
Plus every attribute of a <div>.
Collapsible.Trigger
| Prop | Type | Default |
|---|---|---|
element | HTMLButtonElement | 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
| Prop | Type | Default |
|---|---|---|
forceMount | boolean Keeps the closed panel in the page, hidden. With | true |
element | HTMLDivElement | null The rendered element. Bindable. | — |
Plus every attribute of a <div>. class styles the inner box that holds the text.