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

Toggle Group Primitive

Use Toggle Group to arrange related toggle buttons in a toolbar. The group manages which buttons are pressed and supports arrow-key navigation.

Anatomy

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

---
import { ToggleGroup } from "@starwind-ui/astro/toggle-group";
---
<ToggleGroup.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 Toggle Group Runtime instance.

Default element
div
Discovery hook
data-sw-toggle-group
Role
group

Props

Prop Type Default Toggle
value ToggleGroupValue -
Description
Controls the current Toggle Group value.
Kind
control
Targets
-
Full type
ToggleGroupValue
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 ToggleGroupValue -
Description
Sets the initial Toggle Group value for uncontrolled usage.
Kind
control
Targets
-
Full type
ToggleGroupValue
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 Root part.
Kind
option
Targets
-
Full type
boolean
loopFocus boolean true
Description
Lets keyboard focus wrap around the Toggle Group items.
Kind
option
Targets
-
Full type
boolean
multiple boolean false
Description
Allows multiple Toggle Group values to be selected.
Kind
option
Targets
-
Full type
boolean
orientation "horizontal" | "vertical" "horizontal"
Description
Sets the Toggle Group orientation.
Kind
option
Targets
-
Full type
"horizontal" | "vertical"
onValueChange (value: ToggleGroupValue, details: ToggleGroupValueChangeDetails) => void -
Description
Runs when the Toggle Group value changes.
Kind
callback
Targets
-
Full type
(value: ToggleGroupValue, details: ToggleGroupValueChangeDetails) => 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: ToggleGroupValue
Description
Fires when the value changes for Toggle Group.
DOM event
starwind:value-change
Details type
ToggleGroupValueChangeDetails
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 ToggleGroupValue value / defaultValue
Description
Tracks the current Toggle Group 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-toggle-group - Marks the Root part so Starwind Runtime can find it.
State
Attribute Value Description
data-value - Reflects the value state on the Root part.
Metadata
Attribute Value Description
data-default-value - Reflects the default value prop on the Root part.
data-disabled - Reflects the disabled prop on the Root part.
data-loop-focus - Reflects the loop focus prop on the Root part.
data-multiple - Reflects the multiple prop on the Root part.
data-orientation - Reflects the orientation prop on the Root part.

Runtime API

Factory
createToggleGroup
Import
@starwind-ui/runtime/toggle-group
Root hook
root data-sw-toggle-group
Option props
defaultValue, disabled, loopFocus, multiple, orientation, value

Runtime Setters

Method Target Description
setValue state: value Updates the current Toggle Group value from Runtime code.
setDisabled prop: disabled Updates whether Toggle Group is disabled from Runtime code.
setLoopFocus prop: loopFocus Updates Toggle Group keyboard focus wrapping from Runtime code.
setMultiple prop: multiple Updates whether Toggle Group allows multiple values from Runtime code.
setOrientation prop: orientation Updates the Toggle Group orientation from Runtime code.
Component Relationship
Toggle Group Composite

Changelog

View version history v1.2.0 7 releases

v1.2.0

  • Toggle Group horizontal arrow keys follow visible positions in RTL, including inside a section with its own `dir`.
  • In Svelte, toggle items print `data-default-pressed` from their `defaultPressed` prop only, as the Toggle Primitive does.
  • 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 Toggle Group part now wins over the part's own default for the same attribute, such as `role` or `aria-label`.
  • In Svelte, a defined `value`, or a defined `pressed` on an item, that you pass when ToggleGroup 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, ToggleGroup keeps its own state.
  • Fixed a Toggle Group trigger on a link or span that stayed disabled when a framework wrote `disabled="false"`. On a native form control, any `disabled` attribute still disables it.

v1.0.1

  • React toggles now use the group's selected and disabled state before the page becomes interactive.
  • Reduced repeated JavaScript when Astro Toggle Group is used with other Starwind components.
  • Reduced JavaScript used by Vue Toggle Group. Props and defaults stay the same.

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

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

  • The React Toggle Group Primitive files that the CLI copies into your project now include the Toggle Group context provider, so items get the current group state.

v0.1.0

  • First release. Toggle Group lets one toggle or many toggles be pressed, and it moves focus between them with the keyboard.