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

Accordion Primitive

Use Accordion to organize a set of sections that readers can expand. The headers open their panels and support keyboard navigation between sections.

Anatomy

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

---
import { Accordion } from "@starwind-ui/astro/accordion";
---
<Accordion.Root>
<Accordion.Item value="details">
<Accordion.Header>
<Accordion.Trigger>Toggle details</Accordion.Trigger>
</Accordion.Header>
<Accordion.Panel>Accordion panel content</Accordion.Panel>
</Accordion.Item>
</Accordion.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 Accordion Runtime instance.

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

Props

Prop Type Default Toggle
value AccordionValue -
Description
Controls the current Accordion value.
Kind
control
Targets
root
Full type
AccordionValue
React

Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.

  • value
  • defaultValue
  • onValueChange
Astro

Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.

  • value
  • defaultValue
  • starwind:value-change
  • setValue
Runtime / HTML

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

  • data-default-value
  • starwind:value-change
  • setValue
defaultValue AccordionValue -
Description
Sets the initial Accordion value for uncontrolled usage.
Kind
control
Targets
-
Full type
AccordionValue
React

Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.

  • value
  • defaultValue
  • onValueChange
Astro

Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.

  • value
  • defaultValue
  • starwind:value-change
  • setValue
Runtime / HTML

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

  • data-default-value
  • starwind:value-change
  • setValue
type "single" | "multiple" "single"
Description
Sets the native type for the Root part.
Kind
option
Targets
-
Full type
"single" | "multiple"
collapsible boolean true
Description
Allows all Accordion items to be collapsed.
Kind
option
Targets
-
Full type
boolean
onValueChange (value: AccordionValue, details: AccordionValueChangeDetails) => void -
Description
Runs when the Accordion value changes.
Kind
callback
Targets
-
Full type
(value: AccordionValue, details: AccordionValueChangeDetails) => void
React

Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.

  • value
  • defaultValue
  • onValueChange
Astro

Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.

  • value
  • defaultValue
  • starwind:value-change
  • setValue
Runtime / HTML

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

  • data-default-value
  • starwind:value-change
  • setValue

Events

Event Callback Value Toggle
valueChange onValueChange value: AccordionValue
Description
Fires when the value changes for Accordion.
DOM event
starwind:value-change
Details type
AccordionValueChangeDetails
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
value AccordionValue value / defaultValue
Description
Tracks the current Accordion value.
Initial attribute
data-default-value
Runtime getter
getValue
Runtime setter
setValue
React

Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.

  • value
  • defaultValue
  • onValueChange
Astro

Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.

  • value
  • defaultValue
  • starwind:value-change
  • setValue
Runtime / HTML

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

  • data-default-value
  • starwind:value-change
  • setValue

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-accordion - 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-type - Reflects the type prop on the Root part.
data-default-value - Reflects the default value prop on the Root part.
data-collapsible - Reflects the collapsible prop on the Root part.

Item

An interactive item inside the Accordion collection.

Default element
div
Discovery hook
data-sw-accordion-item
Role
-

Props

Prop Type Default Toggle
value string -
Description
Controls the current Accordion value.
Kind
option
Targets
item
Full type
string
React

Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.

  • value
  • defaultValue
  • onValueChange
Astro

Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.

  • value
  • defaultValue
  • starwind:value-change
  • setValue
Runtime / HTML

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

  • data-default-value
  • starwind:value-change
  • setValue
disabled boolean false
Description
Disables the Item part.
Kind
option
Targets
item
Full type
boolean

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-accordion-item - Marks the Item part so Starwind Runtime can find it.
State
Attribute Value Description
data-state - Reflects the current state on the Item part.
Metadata
Attribute Value Description
data-value - Reflects the value prop on the Item part.
data-disabled - Reflects the disabled prop on the Item part.

The header container for a Accordion item.

Default element
h3
Discovery hook
data-sw-accordion-header
Role
-

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-accordion-header - Marks the Header part so Starwind Runtime can find it.

Trigger

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

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

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-accordion-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 Accordion.

Default element
div
Discovery hook
data-sw-accordion-content
Role
region

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-accordion-content - Marks the Panel part so Starwind Runtime can find it.
State
Attribute Value Description
data-state - Reflects the current state on the Panel part.

Runtime API

Factory
createAccordion
Import
@starwind-ui/runtime/accordion
Root hook
root data-sw-accordion
Option props
type, defaultValue, collapsible, value

Runtime Setters

Method Target Description
setValue state: value Updates the current Accordion value from Runtime code.
Component Relationship
Accordion Direct primitive

Changelog

View version history v1.2.0 9 releases

v1.2.0

  • In Vue, the default slot of `Accordion.Root` no longer receives `value`. If you read `value` from that slot, use `v-model` on the root instead.
  • In Svelte, the `children` snippet of `Accordion.Root` no longer receives arguments. If you read the value from a snippet argument, use `bind:value` on the root instead.
  • In Vue, this component now needs Vue 3.5.21 or newer.

v1.1.0

  • In Astro, an attribute that you set on an Accordion part now wins over the part's own default for the same attribute, such as `role` or `aria-label`.
  • In Svelte, a defined `value` that you pass when Accordion 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, Accordion keeps its own state. Accordion also follows `push()` and `splice()` on a `$state` array.

v1.0.2

  • Vue Accordion keeps its previous expanded items when an open or close action is canceled.
  • Reduced repeated JavaScript when Astro Accordion 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.3

  • 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.2

  • You can now cancel an Accordion value change from its change event. When Accordion manages its own value, a canceled change does not open or close any item.

v0.1.1

  • An Accordion that opens one item at a time now lets you close the open item by default. To keep one item open at all times, set `collapsible={false}`.

v0.1.0

  • First release. Accordion can open one item or many items at a time. It supports keyboard navigation and shows and hides each panel.