Skip to content

shadcn.io is an independent project built on shadcn/ui

Free

Controlled Collapsible for Svelte

A controlled collapsible whose status line reads Open or Closed while Toggle content changes.

UI library: Ark UI

A status line above the trigger reads Open or Closed. Toggle content shows a short note, and that line follows the same open state.

Use it when another control must open the same region. Several sections that open one at a time belong in an Accordion. In Svelte, Ark UI does not keep this value after a reload.

Preview

Preview width

Installation

MCP · Pro
Make your AI a shadcn expert
Your agent installs components, builds pages and restyles your app from one prompt, in Claude Code, Cursor or VS Code.
Part of Pro

Anatomy

Collapsible anatomy: root, trigger, indicator, content

Usage

<div class="flex w-full max-w-lg flex-col gap-4"><p class="text-muted-foreground text-sm">Status: {#if isOpen}{"Open"}{:else}{"Closed"}{/if}</p><Collapsible onOpenChange={handleOpenChange} open={isOpen}><CollapsibleTrigger class="font-medium text-sm underline-offset-4 hover:underline">Toggle content</CollapsibleTrigger><CollapsibleContent class="mt-2 text-muted-foreground text-sm">This is a controlled collapsible component. The open state is managed externally.</CollapsibleContent></Collapsible></div>

FAQ

How does this file wire the open state?

isOpen is state in the demo, starting false. The status line prints Open or Closed from it, and Collapsible is controlled with that same value. Pass open and update it in onOpenChange, which receives { open }.

How do I make it start open?

Start the state at true. The status line follows that value on the first render.

Does Toggle content still open and close the panel?

Yes. The trigger asks the collapsible to change, the change handler writes the state, and the collapsible and the status line both read it.

How do I open it from a second button?

Set the state from that button. The collapsible opens because it reads the same value as the trigger.

What does the panel contain?

One sentence in CollapsibleContent, mt-2 text-muted-foreground text-sm, saying the open state is managed outside the collapsible.

Does the closed paragraph stay in the page?

Yes, by default the content stays mounted and hidden, so fields inside it still submit. Set lazyMount to render it on first open, or unmountOnExit to remove it when it closes.

API Reference

Collapsible

Sets onOpenChange={handleOpenChange} and open={isOpen}. The rest is on the Collapsible page.