Skip to main content

Starwind UI v3.7 is here with stable Vue and Svelte, the new Chart component, and full RTL support. Read the release notes

Collapsible Primitive

Use Collapsible to reveal details beneath a single trigger. The trigger tracks whether its content is open.

Anatomy

Use the Astro primitive adapter to render Collapsible anatomy with the Runtime wiring included.

---
import { Collapsible } from "@starwind-ui/astro/collapsible";
---
<Collapsible.Root>
<Collapsible.Trigger>Toggle details</Collapsible.Trigger>
<Collapsible.Panel>Collapsible content</Collapsible.Panel>
</Collapsible.Root>

Astro, React, Vue, and Svelte components use the same underlying interactions. Each framework has its own way to read and update state. See the examples and your installed packageโ€™s types for the available props and events. Plain HTML uses DOM events and Runtime methods.

API Reference

Root

The main element that owns the Collapsible Runtime instance.

Default element
div
Discovery hook
data-sw-collapsible
Role
-

Props

Prop Type Default Toggle
open boolean -
Description
Controls whether Collapsible is open.
Kind
control
Targets
-
Full type
boolean
React

Use open for controlled state and defaultOpen for default state, and onOpenChange for change proposals.

  • open
  • defaultOpen
  • onOpenChange
Astro

Use open or defaultOpen for initial state, listen for starwind:open-change, and call setOpen for later updates.

  • open
  • defaultOpen
  • starwind:open-change
  • setOpen
Runtime / HTML

Use data-default-open for initial state, listen for starwind:open-change, and call setOpen for later updates.

  • data-default-open
  • starwind:open-change
  • setOpen
defaultOpen boolean false
Description
Sets whether Collapsible starts open.
Kind
control
Targets
-
Full type
boolean
React

Use open for controlled state and defaultOpen for default state, and onOpenChange for change proposals.

  • open
  • defaultOpen
  • onOpenChange
Astro

Use open or defaultOpen for initial state, listen for starwind:open-change, and call setOpen for later updates.

  • open
  • defaultOpen
  • starwind:open-change
  • setOpen
Runtime / HTML

Use data-default-open for initial state, listen for starwind:open-change, and call setOpen for later updates.

  • data-default-open
  • starwind:open-change
  • setOpen
disabled boolean false
Description
Disables the Root part.
Kind
option
Targets
-
Full type
boolean
onOpenChange (open: boolean, details: CollapsibleOpenChangeDetails) => void -
Description
Runs when Collapsible opens or closes.
Kind
callback
Targets
-
Full type
(open: boolean, details: CollapsibleOpenChangeDetails) => void
React

Use open for controlled state and defaultOpen for default state, and onOpenChange for change proposals.

  • open
  • defaultOpen
  • onOpenChange
Astro

Use open or defaultOpen for initial state, listen for starwind:open-change, and call setOpen for later updates.

  • open
  • defaultOpen
  • starwind:open-change
  • setOpen
Runtime / HTML

Use data-default-open for initial state, listen for starwind:open-change, and call setOpen for later updates.

  • data-default-open
  • starwind:open-change
  • setOpen

Events

Event Callback Value Toggle
openChange onOpenChange open: boolean
Description
Fires when Collapsible opens or closes.
DOM event
starwind:open-change
Details type
CollapsibleOpenChangeDetails
Timing
before-state-commit
Cancelable
Yes
Cancellation sequence
  1. Check internal eligibility and intent.
  2. Create one details object for the proposal.
  3. Call the Runtime callback with the details object when the controller exposes one.
  4. Dispatch the cancelable DOM event with the same details object, including when the callback canceled it.
  5. Read details.isCanceled, including cancellation caused by preventDefault().
  6. Apply the accepted state.
  7. Notify Runtime subscribers and other accepted-only observers.

State

State Type Props Toggle
open boolean open / defaultOpen
Description
Tracks whether Collapsible is open.
Initial attribute
data-default-open
Runtime getter
getOpen
Runtime setter
setOpen
React

Use open for controlled state and defaultOpen for default state, and onOpenChange for change proposals.

  • open
  • defaultOpen
  • onOpenChange
Astro

Use open or defaultOpen for initial state, listen for starwind:open-change, and call setOpen for later updates.

  • open
  • defaultOpen
  • starwind:open-change
  • setOpen
Runtime / HTML

Use data-default-open for initial state, listen for starwind:open-change, and call setOpen for later updates.

  • data-default-open
  • starwind:open-change
  • setOpen

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-collapsible - Marks the Root part so Starwind Runtime can find it.
State
Attribute Value Description
data-state - Reflects the current state on the Root part.
Metadata
Attribute Value Description
data-default-open - Reflects the default open prop on the Root part.
data-disabled - Reflects the disabled prop on the Root part.

Trigger

The control that opens, closes, or targets the Collapsible content.

Default element
button
Discovery hook
data-sw-collapsible-trigger
Role
-

Props

Prop Type Default Toggle
asChild boolean false
Description
Merges behavior onto your child element instead of rendering the default Trigger element.
Kind
rendering
Targets
trigger
Full type
boolean

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-collapsible-trigger - Marks the Trigger part so Starwind Runtime can find it.
State
Attribute Value Description
data-state - Reflects the current state on the Trigger part.

Panel

The content panel controlled by Collapsible.

Default element
div
Discovery hook
data-sw-collapsible-panel
Role
-

Props

Prop Type Default Toggle
hiddenUntilFound boolean false
Description
Changes how the Panel part is rendered.
Kind
rendering
Targets
panel
Full type
boolean

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-collapsible-panel - Marks the Panel part so Starwind Runtime can find it.
State
Attribute Value Description
data-state - Reflects the current state on the Panel part.
Metadata
Attribute Value Description
data-hidden-until-found - Reflects the hidden until found prop on the Panel part.

Runtime API

Factory
createCollapsible
Import
@starwind-ui/runtime/collapsible
Root hook
root data-sw-collapsible
Option props
defaultOpen, disabled, open

Runtime Setters

Method Target Description
setOpen state: open Opens or closes Collapsible from Runtime code.
Component Relationship
Collapsible Direct primitive

Changelog

View version history v1.1.1 7 releases

v1.1.1

  • In Vue, `Collapsible.Trigger` with a bare `disabled` attribute is now disabled and does not toggle.
  • In Vue, this Primitive now uses the same `asChild` child handling as the other Vue Primitives. Its public API is unchanged. An invalid `asChild` child now gives one shorter error message.
  • In Vue, this component now needs Vue 3.5.21 or newer.
  • Internal code cleanup, such as unused code and import order. This cleanup does not change behavior.

v1.1.0

  • In Astro, an attribute that you set on a Collapsible part now wins over the part's own default for the same attribute, such as `role` or `aria-label`.
  • In Svelte, a defined `open` that you pass when Collapsible mounts now stays under your control, as in React and Vue. To change it, use `bind:` or update your state in the change callback. A form reset keeps it. If you pass `undefined` at mount, Collapsible keeps its own state.
  • In Svelte, the `children` snippet on the Collapsible root no longer receives `open`. Use `bind:open`.
  • In Vue, a Collapsible trigger rendered as a link no longer gets `disabled="false"` when it is enabled.

v1.0.2

  • Vue Collapsible keeps its previous open state when an open or close action is canceled.
  • Reduced repeated JavaScript when Astro Collapsible is used with other Starwind components.

v1.0.1

  • Removed old comments from the Vue Primitive files that said the code was internal and not for release. The CLI also removes the blank line that these comments left in the Vue Primitive `index.ts` files that it copies into your project.

v1.0.0

  • This Primitive is now stable at version `1.0.0`. Its API and behavior stay the same as in the previous release.

v0.1.1

  • The React Primitive files that the CLI copies into your project are now marked with `"use client"`, so they stay client components in React server frameworks.
  • In React, you can cancel a change from its change callback or DOM event before it applies. React then shows only the changes that apply.

v0.1.0

  • First release. Collapsible keeps its own open state or follows the open state from your app. Its trigger shows and hides its panel.