Skip to main content

Starwind UI v3.0 is now available! Migration guide

Accordion Primitive

Accordion is a Starwind Runtime primitive in the controlled-value-group contract family. Astro and React share one semantic component API. React coordinates reactive state through controlled and default props plus callbacks. Astro renders initial state and coordinates later changes through DOM events and Runtime methods. Raw HTML uses Runtime attributes, DOM events, and imperative methods.

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>

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.0.0 5 releases

v1.0.0

  • Promoted this Primitive's vendoring version to the stable 1.0.0 baseline. Its existing API and Runtime behavior carry forward from the previous release.

v0.1.3

  • Marked generated React Primitive files as client modules so vendored installs preserve client boundaries in React server frameworks.
  • Kept generated React state-change callbacks and DOM events cancelable until the Runtime accepts the proposed state, then synchronized rendered state.

v0.1.2

  • Made value-change proposals synchronously cancelable before uncontrolled state commits.

v0.1.1

  • Made single-value accordions collapsible by default while preserving the option to require one item to remain open.

v0.1.0

  • Introduced Runtime-managed single and multiple disclosure state, keyboard navigation, and panel presence.