Collapsible
A disclosure: a trigger that shows and hides a region of content. Render
Collapsible.Trigger and
Collapsible.Content inside it.
Built on bits-ui Collapsible.
<Collapsible bind:open> <Collapsible.Trigger> <span class="font-semibold">Advanced</span> </Collapsible.Trigger> <Collapsible.Content> <p>More settings</p> </Collapsible.Content></Collapsible>- Composed from a trigger and a content part, so the layout inside each is yours.
openis bindable, two-way or as a getter and setter. State & binding explains both modes.Collapsible.Contentis only in the document while open.- The trigger carries
data-state="open"orclosedandaria-expanded. - The trigger’s children are the title and an optional subtitle. The default chevron turns when open, and
indicatorreplaces it.
Anatomy
Section titled “Anatomy”Every part is a property on Collapsible. The root renders a <div>, the trigger a <button>.
<Collapsible> <Collapsible.Trigger>title</Collapsible.Trigger> <Collapsible.Content>...</Collapsible.Content></Collapsible>Two-way binding
Section titled “Two-way binding”bind:open on the root.
open: false
Code
<script lang="ts"> import { Collapsible } from "@viamrobotics/prime-ui";
let open = $state(false);</script>
<div class="not-content"> <Collapsible bind:open> <Collapsible.Trigger class="font-semibold">Advanced</Collapsible.Trigger> <Collapsible.Content class="pt-2 text-sm">Extra settings live here.</Collapsible.Content> </Collapsible>
<p class="mt-3 text-sm text-subtle-2">open: <code>{open}</code></p></div>Fully controlled
Section titled “Fully controlled”Pass bind:open a getter and a setter. The setter can decline a change.
Code
<script lang="ts"> import { Collapsible } from "@viamrobotics/prime-ui";
let open = $state(false); let isLocked = $state(true);</script>
<div class="not-content flex flex-col items-start gap-3"> <label class="flex items-center gap-2 text-sm"> <input type="checkbox" bind:checked={isLocked} /> Keep closed </label>
<Collapsible class="w-full" bind:open={ () => open, (next) => { if (!isLocked) open = next; } } > <Collapsible.Trigger class="font-semibold">Advanced</Collapsible.Trigger> <Collapsible.Content class="pt-2 text-sm"> The setter declines every change while the box is checked. </Collapsible.Content> </Collapsible></div>Title and subtitle
Section titled “Title and subtitle”The trigger’s children are laid out by you, so a title and a subtitle are two elements inside it.
Code
<script lang="ts"> import { Collapsible } from "@viamrobotics/prime-ui";</script>
<div class="not-content"> <Collapsible> <Collapsible.Trigger> <span class="block font-semibold">Network</span> <span class="block text-sm text-subtle-2">Ports, proxies, and timeouts</span> </Collapsible.Trigger> <Collapsible.Content class="pt-2 text-sm">Port 8080, no proxy, 30 second timeout.</Collapsible.Content> </Collapsible></div>Custom indicator
Section titled “Custom indicator”Pass an indicator snippet to the trigger to replace the chevron. It receives open.
Code
<script lang="ts"> import { Collapsible } from "@viamrobotics/prime-ui";</script>
<div class="not-content"> <Collapsible> <Collapsible.Trigger class="font-semibold"> Advanced {#snippet indicator({ open })} <span class="text-sm text-subtle-2">{open ? "Hide" : "Show"}</span> {/snippet} </Collapsible.Trigger> <Collapsible.Content class="pt-2 text-sm">Extra settings live here.</Collapsible.Content> </Collapsible></div>States
Section titled “States”Closed
Open
Disabled
Styling
Section titled “Styling”Every part takes class on its element. Collapsible.Trigger is a full-width <button> with the focus ring and no other look, so add the type and spacing you want. bits-ui sets the data attributes below.
| Part | Attribute | Value |
|---|---|---|
Root |
data-state |
open or closed |
Root |
data-disabled |
present when disabled |
Trigger |
data-state |
open or closed |
Trigger |
aria-expanded |
true or false |
Trigger |
data-disabled |
present when disabled |
Content |
data-state |
open |
Keyboard
Section titled “Keyboard”| Key | Action |
|---|---|
| Tab | Focuses the trigger. |
| Enter / Space | On the trigger, opens or closes the content. |
| Prop | Type | Default | Description |
|---|---|---|---|
id | string | Element id. null is not accepted. | |
bind:open | boolean | false | Whether the content is shown. Two-way bindable. |
disabled | boolean | false | Disable the trigger. |
Plus everything in HTMLAttributes<HTMLDivElement>, spread onto the element.
Collapsible.Content
The region a Collapsible shows and hides. Rendered only while open.
Takes extraClasses, children, ...restProps }: Props as its props.
Collapsible.Trigger
The button that opens and closes a Collapsible. Its children are the
title, and an optional subtitle, as the consumer lays them out. The indicator snippet replaces
the default chevron, which turns when the content is open.
Takes extraClasses, indicator, children, ...restProps }: Props as its props.