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 Primitive

Use Toggle for a button with a pressed state, such as Bold in a toolbar. Activating the button flips that state.

Anatomy

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

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

Default element
button
Discovery hook
data-sw-toggle
Role
-

Props

Prop Type Default Toggle
pressed boolean -
Description
Controls the pressed state for Toggle.
Kind
control
Targets
-
Full type
boolean
React

Use pressed for controlled state and defaultPressed for default state, and onPressedChange for change proposals.

  • pressed
  • defaultPressed
  • onPressedChange
Astro

Use pressed or defaultPressed for initial state, listen for starwind:pressed-change, and call setPressed for later updates.

  • pressed
  • defaultPressed
  • starwind:pressed-change
  • setPressed
Runtime / HTML

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

  • data-default-pressed
  • starwind:pressed-change
  • setPressed
defaultPressed boolean false
Description
Controls the default pressed state for Toggle.
Kind
control
Targets
-
Full type
boolean
React

Use pressed for controlled state and defaultPressed for default state, and onPressedChange for change proposals.

  • pressed
  • defaultPressed
  • onPressedChange
Astro

Use pressed or defaultPressed for initial state, listen for starwind:pressed-change, and call setPressed for later updates.

  • pressed
  • defaultPressed
  • starwind:pressed-change
  • setPressed
Runtime / HTML

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

  • data-default-pressed
  • starwind:pressed-change
  • setPressed
disabled boolean false
Description
Disables the Root part.
Kind
option
Targets
-
Full type
boolean
nativeButton boolean true
Description
Renders the control as a native button element.
Kind
rendering
Targets
root
Full type
boolean
syncGroup string -
Description
Configures the sync group option for the Root part.
Kind
option
Targets
-
Full type
string
value string -
Description
Controls the current Toggle value.
Kind
option
Targets
-
Full type
string
onPressedChange (pressed: boolean, details: TogglePressedChangeDetails) => void -
Description
Runs when on pressed change changes for Toggle.
Kind
callback
Targets
-
Full type
(pressed: boolean, details: TogglePressedChangeDetails) => void
React

Use pressed for controlled state and defaultPressed for default state, and onPressedChange for change proposals.

  • pressed
  • defaultPressed
  • onPressedChange
Astro

Use pressed or defaultPressed for initial state, listen for starwind:pressed-change, and call setPressed for later updates.

  • pressed
  • defaultPressed
  • starwind:pressed-change
  • setPressed
Runtime / HTML

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

  • data-default-pressed
  • starwind:pressed-change
  • setPressed

Events

Event Callback Value Toggle
pressedChange onPressedChange pressed: boolean
Description
Fires when the pressed state changes for Toggle.
DOM event
starwind:pressed-change
Details type
TogglePressedChangeDetails
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
pressed boolean pressed / defaultPressed
Description
Tracks whether Toggle is pressed.
Initial attribute
data-default-pressed
Runtime getter
getPressed
Runtime setter
setPressed
React

Use pressed for controlled state and defaultPressed for default state, and onPressedChange for change proposals.

  • pressed
  • defaultPressed
  • onPressedChange
Astro

Use pressed or defaultPressed for initial state, listen for starwind:pressed-change, and call setPressed for later updates.

  • pressed
  • defaultPressed
  • starwind:pressed-change
  • setPressed
Runtime / HTML

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

  • data-default-pressed
  • starwind:pressed-change
  • setPressed

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-toggle - Marks the Root part so Starwind Runtime can find it.
State
Attribute Value Description
data-pressed - Reflects the pressed state on the Root part.
data-state - Reflects the current state on the Root part.
data-unpressed - Reflects the unpressed state on the Root part.
Metadata
Attribute Value Description
data-default-pressed - Reflects the default pressed prop on the Root part.
data-disabled - Reflects the disabled prop on the Root part.
data-native - Reflects the native prop on the Root part.
data-sync-group - Reflects the sync group prop on the Root part.
data-value - Reflects the value prop on the Root part.

Runtime API

Factory
createToggle
Import
@starwind-ui/runtime/toggle
Root hook
root data-sw-toggle
Option props
defaultPressed, disabled, nativeButton, pressed, syncGroup, value

Runtime Setters

Method Target Description
setPressed state: pressed Updates whether Toggle is pressed from Runtime code.
setDisabled prop: disabled Updates whether Toggle is disabled from Runtime code.
Component Relationship
Toggle Direct primitive
Toggle Group Composite

Changelog

View version history v1.1.1 8 releases

v1.1.1

  • `data-default-pressed` now shows only the `defaultPressed` prop from when the Toggle mounted. A Toggle with both `pressed` and `defaultPressed` now prints it in Astro, React and Vue.
  • In Vue, this component now needs Vue 3.5.21 or newer.

v1.1.0

  • In Svelte, a defined `pressed` that you pass when Toggle 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, Toggle keeps its own state.
  • Fixed a Toggle 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.2

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

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

  • Toggle setup no longer takes over a Theme Toggle on the same page.

v0.1.0

  • First release. Toggle handles its pressed and disabled states and fires pressed-change events.