Collapsible Primitive
Use Collapsible to reveal details beneath a single trigger. The trigger tracks whether its content is open.
Component library
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>Use the React primitive adapter when Collapsible state participates in React rendering.
import { Collapsible } from "@starwind-ui/react/collapsible";
export function Example() { return ( <Collapsible.Root> <Collapsible.Trigger>Toggle details</Collapsible.Trigger> <Collapsible.Panel>Collapsible content</Collapsible.Panel> </Collapsible.Root> );}Use the Vue components to build Collapsible.
<script setup lang="ts">import Collapsible from "@starwind-ui/vue/collapsible";</script>
<template> <Collapsible.Root> <Collapsible.Trigger>Toggle details</Collapsible.Trigger> <Collapsible.Panel>Collapsible content</Collapsible.Panel> </Collapsible.Root></template>Use the Svelte components to build Collapsible.
<script lang="ts"> import { Collapsible } from "@starwind-ui/svelte/collapsible";</script>
<Collapsible.Root> <Collapsible.Trigger>Toggle details</Collapsible.Trigger> <Collapsible.Panel>Collapsible content</Collapsible.Panel></Collapsible.Root>Render the Collapsible data-sw-* contract yourself, then initialize createCollapsible.
<div data-sw-collapsible> <button data-sw-collapsible-trigger>Toggle details</button> <div data-sw-collapsible-panel hidden>Collapsible content</div></div>
<script type="module"> import { createCollapsible } from "@starwind-ui/runtime/collapsible";
const root = document.querySelector("[data-sw-collapsible]"); if (root) { createCollapsible(root); }</script>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
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.
opendefaultOpenonOpenChange
- Astro
Use open or defaultOpen for initial state, listen for starwind:open-change, and call setOpen for later updates.
opendefaultOpenstarwind:open-changesetOpen
- Runtime / HTML
Use data-default-open for initial state, listen for starwind:open-change, and call setOpen for later updates.
data-default-openstarwind:open-changesetOpen
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.
opendefaultOpenonOpenChange
- Astro
Use open or defaultOpen for initial state, listen for starwind:open-change, and call setOpen for later updates.
opendefaultOpenstarwind:open-changesetOpen
- Runtime / HTML
Use data-default-open for initial state, listen for starwind:open-change, and call setOpen for later updates.
data-default-openstarwind:open-changesetOpen
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.
opendefaultOpenonOpenChange
- Astro
Use open or defaultOpen for initial state, listen for starwind:open-change, and call setOpen for later updates.
opendefaultOpenstarwind:open-changesetOpen
- Runtime / HTML
Use data-default-open for initial state, listen for starwind:open-change, and call setOpen for later updates.
data-default-openstarwind:open-changesetOpen
Events
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
- Check internal eligibility and intent.
- Create one details object for the proposal.
- Call the Runtime callback with the details object when the controller exposes one.
- Dispatch the cancelable DOM event with the same details object, including when the callback canceled it.
- Read details.isCanceled, including cancellation caused by preventDefault().
- Apply the accepted state.
- Notify Runtime subscribers and other accepted-only observers.
State
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.
opendefaultOpenonOpenChange
- Astro
Use open or defaultOpen for initial state, listen for starwind:open-change, and call setOpen for later updates.
opendefaultOpenstarwind:open-changesetOpen
- Runtime / HTML
Use data-default-open for initial state, listen for starwind:open-change, and call setOpen for later updates.
data-default-openstarwind:open-changesetOpen
Data Attributes
Runtime hooks
State
Metadata
Trigger
The control that opens, closes, or targets the Collapsible content.
- Default element
- button
- Discovery hook
- data-sw-collapsible-trigger
- Role
- -
Props
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
State
Panel
The content panel controlled by Collapsible.
- Default element
- div
- Discovery hook
- data-sw-collapsible-panel
- Role
- -
Props
hiddenUntilFound boolean false
- Description
- Changes how the Panel part is rendered.
- Kind
- rendering
- Targets
- panel
- Full type
- boolean
Data Attributes
Runtime hooks
State
Metadata
Runtime API
- Factory
createCollapsible- Import
@starwind-ui/runtime/collapsible- Root hook
- root
data-sw-collapsible - Option props
- defaultOpen, disabled, open
Runtime Setters
Related Styled Components
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.