Skip to content

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

Components

Free

Collapsible for Svelte

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 Ark UI.

On this page9 sections
UI library: Ark UI

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, where Ark UI reads the direction. 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 an object, so read the new state from details.open. 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. A closed collapsible keeps its content in the page under the hidden attribute, so the fields inside keep their values and submit with the form. That changes if you set unmountOnExit on Collapsible, which removes the content once it closes, or lazyMount, which leaves it out until the first open.

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 Ark UI?

Give CollapsibleTrigger an asChild snippet and spread the props it receives on your button, calling props() with any class you want merged in. The trigger's attributes and click handler land on your button instead of on a second one.

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 height animates through the animate-expand and animate-collapse classes on CollapsibleContent, between 0 and the --height that Ark UI measures. Change the timing, 0.2 seconds as installed, in the --animate-expand and --animate-collapse theme variables.

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?

Set collapsedHeight on Collapsible to a height, such as 80 for 80 pixels. The closed content stays that tall and clipped instead of hidden, and opening it grows it to full height. While it is closed, Ark UI marks every focusable element inside it inert, the ones in the visible lines too, so Tab skips the whole preview until it opens.

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

The controlled open state of the collapsible.

defaultOpen?boolean

The initial open state of the collapsible when rendered. Use when you don't need to control the open state of the collapsible.

onOpenChange?(details: OpenChangeDetails) → void

The callback invoked when the open state changes.

Type
(details: OpenChangeDetails) → void
Parameters

details – { open: boolean }

Returns
void
collapsedHeight?number | string

The height of the content when collapsed.

Type
number | string
collapsedWidth?number | string

The width of the content when collapsed.

Type
number | string
dir?"ltr" | "rtl"

The document's text/writing direction.

Type
"ltr" | "rtl"
Default
"ltr"
disabled?boolean

Whether the collapsible is disabled.

hideMode?"display-none" | "activity"

How to hide content when mounted but not present. - display-none: HTML hidden attribute. Effects stay alive. - activity: React 19 <Activity mode="hidden">. Effects pause. Requires React 19+.

Type
"display-none" | "activity"
Default
"display-none"
id?string

The unique identifier of the machine.

ids?object

The ids of the elements in the collapsible. Useful for composition.

Type
{ root?: string; content?: string; trigger?: string }
lazyMount?boolean

Whether to enable lazy mounting.

Default
false
onExitComplete?VoidFunction

The callback invoked when the exit animation completes.

unmountOnExit?boolean

Whether to unmount on exit.

Default
false
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-partial-collapse?string

Present when collapsedHeight is set.

data-state"open" | "closed"

"open" or "closed".

Type
"open" | "closed"

CollapsibleTrigger

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

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Forwarded to the part.

AttributeType
data-disabled?string

Present when disabled.

data-state"open" | "closed"

"open" or "closed".

Type
"open" | "closed"

CollapsibleContent

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

PropType
disableAnimation?boolean

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

Default
false
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-collapsible""
Default
""
data-disabled?string

Present when disabled.

data-has-collapsed-size?string

Present when the content has collapsed width or height.

data-state"open" | "closed"

"open" or "closed".

Type
"open" | "closed"
CSS variableType
--collapsed-heightstring

The height of the Content.

--collapsed-widthstring

The width of the Content.

--heightstring

The height of the element.

--widthstring

The width of the element.

CollapsibleIndicator

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

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled.

data-state"open" | "closed"

"open" or "closed".

Type
"open" | "closed"

CollapsibleRootProvider

<div> data-slot="collapsible"

PropType
valueobject

What useCollapsible() returns: the state and API, made outside the component to control it.

Type
{ isUnmounted?: boolean; hideMode: HideMode }
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-state"open" | "closed"

"open" or "closed".

Type
"open" | "closed"

useCollapsible

PropType
open?boolean

The controlled open state of the collapsible.

defaultOpen?boolean

The initial open state of the collapsible when rendered. Use when you don't need to control the open state of the collapsible.

onOpenChange?(details: OpenChangeDetails) → void

The callback invoked when the open state changes.

Type
(details: OpenChangeDetails) → void
Parameters

details – { open: boolean }

Returns
void
collapsedHeight?number | string

The height of the content when collapsed.

Type
number | string
collapsedWidth?number | string

The width of the content when collapsed.

Type
number | string
disabled?boolean

Whether the collapsible is disabled.

hideMode?"display-none" | "activity"

How to hide content when mounted but not present. - display-none: HTML hidden attribute. Effects stay alive. - activity: React 19 <Activity mode="hidden">. Effects pause. Requires React 19+.

Type
"display-none" | "activity"
Default
"display-none"
id?string

The unique identifier of the machine.

ids?object

The ids of the elements in the collapsible. Useful for composition.

Type
{ root?: string; content?: string; trigger?: string }
lazyMount?boolean

Whether to enable lazy mounting.

Default
false
onExitComplete?VoidFunction

The callback invoked when the exit animation completes.

unmountOnExit?boolean

Whether to unmount on exit.

Default
false

CollapsibleContext

PropType
disabledboolean

Whether the collapsible is disabled.

hideMode"display-none" | "activity"

Resolved hide strategy for mounted content.

Type
"display-none" | "activity"
measureSizeVoidFunction

Function to measure the size of the content.

openboolean

Whether the collapsible is open.

setOpen(open: boolean) → void

Function to open or close the collapsible.

Parameters

open – boolean

Returns
void
visibleboolean

Whether the collapsible is visible (open or closing).

isUnmounted?boolean

Whether the content is unmounted.