Skip to content

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

Components

Free

Collapsible for Svelte UI SDK

A button that shows or hides one section under it, like the details of an order or a form's advanced fields. A collapsible for Svelte on UI SDK.

On this page9 sections

A collapsible is one button and the section it shows or hides: the shipping details under an order status, the advanced fields at the end of a form, the rest of a long description. It opens and closes on its own, and nothing else on the page changes when it does.

For several sections that read as one list, such as an FAQ, use an Accordion, which can close one section when another opens. A nested outline that people browse and select from belongs in a Tree View.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Collapsible anatomy: root, trigger, indicator, content

Usage

<script lang="ts">
import {
  Collapsible, CollapsibleContent, CollapsibleIndicator,
  CollapsibleTrigger,
} from "@/components/ui/collapsible";
</script>

CollapsibleTrigger is the button that opens and closes the section, and CollapsibleIndicator draws a chevron that flips while it is open.

<Collapsible class="w-full max-w-sm">
  <CollapsibleTrigger class="flex w-full items-center justify-between py-2 font-medium text-sm">
    Order #4189
    <CollapsibleIndicator />
  </CollapsibleTrigger>
  <CollapsibleContent>
    <p class="pb-2 text-muted-foreground text-sm">
      Shipped to 100 Market St, San Francisco: 2x Studio Headphones.
    </p>
  </CollapsibleContent>
</Collapsible>

Accessibility

CollapsibleTrigger renders a <button> with aria-expanded, so a screen reader says whether the section is open, and Enter or Space toggles it. Its aria-controls points at the content, which carries the hidden attribute while the section is closed.

The trigger's name is yours to give. When it holds only an icon, as in the demo at the top of the page, add visually hidden text or an aria-label that says what it opens, and keep links and other buttons out of it.

Space
Opens/closes the collapsible.
Enter
Opens/closes the collapsible.

Demos

Basic

A card holds one full-width trigger, Product details, and while its section is open the chevron flips and the trigger and section share a muted background. Inside are a line of text and a Learn More button. Nothing outside reads the state, so the collapsible keeps it. Start here for a block of detail on a product or help page that most people skip.

Preview width

Settings

A Radius card shows two corner fields, and an outline icon button beside them reveals the two for the bottom corners. The demo controls open because the button's icon and its aria-label switch with it, from show to hide. Use this for values most people leave alone, and keep any field the form requires where it is always visible.

Preview width

File tree

A card lists a small project: the components and lib folders, then app.tsx and package.json. Each folder is a collapsible that opens onto the level under it, indented, and each file is a plain button. It suits a short, fixed outline. Arrow keys don't move between the rows and nothing can be selected, so a file browser people work in belongs in a Tree View.

Preview width

RTL

The order summary from the top of the page, in Arabic, with the collapsible inside a Direction provider set to right to left, which sets dir on an element around it. The title, the status row and the opened address and items mirror, with the toggle on the left. Use this for a block in Arabic or Hebrew, even on a page that is otherwise left to right.

Preview width

FAQ

When should I use a Collapsible instead of an Accordion?

Use a Collapsible for one section with its own button, where opening it changes nothing else: a Show more under a description, the advanced options of a form, the details of one order. It knows nothing about any other collapsible on the page.

When several sections sit together as one list, like an FAQ or a filter sidebar, use an Accordion. It groups the items and can close the open one when another opens, which a stack of separate Collapsibles can't do without state of your own.

How do I open a Collapsible from another button in Svelte?

Hold the open state yourself, pass it as open and update it in onOpenChange, which receives the new boolean as its first argument. Your other button sets the same state, and the trigger keeps working because it calls onOpenChange too.

The Settings demo holds its own open state so the button's icon and label can follow it.

How do I make a Collapsible start open?

Set defaultOpen on Collapsible. The section renders open, and from then on it opens and closes on its own with no state of yours.

To bring it back the way someone left it, after a reload or on their next visit, save the state yourself, in local storage or the URL, and pass it in as the controlled value instead.

Do form fields inside a closed Collapsible still submit?

Yes. CollapsibleContent in components/ui/collapsible sets UI SDK's keepMounted, so a closed panel stays in the page under the hidden attribute, and its fields keep their values and submit with the form. Set keepMounted to false on it when you would rather drop the closed content.

A hidden field can't show the browser's own validation message, though. Keep a required field outside the collapsible, or open the section yourself when validation fails.

How do I use my own button as the Collapsible trigger in UI SDK?

Give CollapsibleTrigger a render snippet that spreads the props it receives on your button, such as a Button with variant set to ghost, as the demos here do, and put the label and icon inside it.

The trigger's props, aria-expanded and the press handler among them, land on your button instead of on a second button wrapped around it.

An icon-only trigger also needs a name, from visually hidden text, as in the demo at the top of the page, or from an aria-label.

How do I change or turn off the Collapsible animation?

Set disableAnimation on CollapsibleContent, and the section opens and closes at once. People whose system asks for reduced motion get no animation either way.

The panel transitions its height between 0 and the --collapsible-panel-height that UI SDK measures. Its duration-200 and ease-out classes set the timing, in CollapsibleContent in components/ui/collapsible.

Can I nest Collapsibles to build a file tree?

Yes. Each Collapsible keeps its own state, so one placed inside another's content opens and closes on its own, as the folders in the File tree demo do. Indent the inner level and it reads as a hierarchy.

It stays a stack of separate buttons, though: Tab visits every visible row, the arrow keys do nothing, and nothing can be selected. For a file browser or a navigation tree that people move through with the keyboard, use a Tree View.

How do I show the first lines of a Collapsible before it opens?

UI SDK has no preview height: a closed section is hidden whole. Put the first lines above CollapsibleContent, where they always show, and the rest inside it, with the trigger under the text.

A Show more button reads better when its label follows the state. Hold the open state yourself, as the Settings demo does, and switch the label to Show less while the section is open.

API Reference

Collapsible

<div> data-slot="collapsible"

PropType
open?boolean

Whether the panel is open. Pass it to control the collapsible. Two-way with bind:open.

defaultOpen?boolean

Whether the panel is open at first, when uncontrolled.

Default
false
onOpenChange?(open: boolean, details: ChangeDetails<CollapsibleOpenChangeReason>) → void

Runs when the panel wants to open or close. details.reason says why, and details.cancel() stops it.

Type
(open: boolean, details: ChangeDetails<CollapsibleOpenChangeReason>) → void
Parameters

open – boolean

details – ChangeDetails<CollapsibleOpenChangeReason>

Returns
void
controller?CollapsibleController

Opens and closes the panel from anywhere. Create one with createCollapsibleController().

debug?boolean

Logs every state change with its reason to the console, in development builds.

disabled?boolean

Stops the trigger from opening or closing the panel. The trigger stays focusable.

Default
false
onOpenChangeComplete?(open: boolean) → void

Runs once the panel has finished opening or closing, after any animation.

Parameters

open – boolean

Returns
void
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

AttributeType
data-closed?string

Present while the panel is closed, including while it animates out.

data-disabled?string

Present when the collapsible can't open or close.

data-open?string

Present while the panel is open.

CollapsibleTrigger

<button> data-slot="collapsible-trigger"

PropType
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

AttributeType
data-closed?string

Present while the panel is closed, including while it animates out.

data-disabled?string

Present when the collapsible can't open or close.

data-open?string

Present while the panel is open.

CollapsibleContent

<div> data-slot="collapsible-panel"

PropType
disableAnimation?boolean

Opens and closes it at once, without the height animation.

Default
false
hiddenUntilFound?boolean

Keeps the closed panel in the page as hidden="until-found", so the browser's find in page can match its text and open it.

Default
false
keepMounted?boolean

Whether the panel stays in the page, hidden, while closed.

Default
false
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

AttributeType
data-closed?string

Present while the panel is closed, including while it animates out.

data-disabled?string

Present when the collapsible can't open or close.

data-ending-style?string

Present while the panel animates closed. Style the state you animate to.

data-open?string

Present while the panel is open.

data-starting-style?string

Present for the panel's first frame. Style the state you animate from.

CSS variableType
--collapsible-panel-heightstring

The panel's content height in pixels while it animates, auto once open and 0px while closed.

--collapsible-panel-widthstring

The panel's content width, the same way. For panels that open sideways.

CollapsibleIndicator

<span> data-slot="collapsible-indicator"

PropType
class?string

Merged with the part's classes.