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>Use the React primitive adapter when Toggle state participates in React rendering.
import { Toggle } from "@starwind-ui/react/toggle";
export function Example() { return ( <Toggle.Root>Toggle</Toggle.Root> );}Use the Vue components to build Toggle.
<script setup lang="ts">import Toggle from "@starwind-ui/vue/toggle";</script>
<template> <Toggle.Root>Toggle</Toggle.Root></template>Use the Svelte components to build Toggle.
<script lang="ts"> import { Toggle } from "@starwind-ui/svelte/toggle";</script>
<Toggle.Root>Toggle</Toggle.Root>Render the Toggle data-sw-* contract yourself, then initialize createToggle.
<button data-sw-toggle>Toggle</button>
<script type="module"> import { createToggle } from "@starwind-ui/runtime/toggle";
const root = document.querySelector("[data-sw-toggle]"); if (root) { createToggle(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 Toggle Runtime instance.
- Default element
- button
- Discovery hook
- data-sw-toggle
- Role
- -
Props
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.
presseddefaultPressedonPressedChange
- Astro
Use pressed or defaultPressed for initial state, listen for starwind:pressed-change, and call setPressed for later updates.
presseddefaultPressedstarwind:pressed-changesetPressed
- Runtime / HTML
Use data-default-pressed for initial state, listen for starwind:pressed-change, and call setPressed for later updates.
data-default-pressedstarwind:pressed-changesetPressed
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.
presseddefaultPressedonPressedChange
- Astro
Use pressed or defaultPressed for initial state, listen for starwind:pressed-change, and call setPressed for later updates.
presseddefaultPressedstarwind:pressed-changesetPressed
- Runtime / HTML
Use data-default-pressed for initial state, listen for starwind:pressed-change, and call setPressed for later updates.
data-default-pressedstarwind:pressed-changesetPressed
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.
presseddefaultPressedonPressedChange
- Astro
Use pressed or defaultPressed for initial state, listen for starwind:pressed-change, and call setPressed for later updates.
presseddefaultPressedstarwind:pressed-changesetPressed
- Runtime / HTML
Use data-default-pressed for initial state, listen for starwind:pressed-change, and call setPressed for later updates.
data-default-pressedstarwind:pressed-changesetPressed
Events
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
- 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
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.
presseddefaultPressedonPressedChange
- Astro
Use pressed or defaultPressed for initial state, listen for starwind:pressed-change, and call setPressed for later updates.
presseddefaultPressedstarwind:pressed-changesetPressed
- Runtime / HTML
Use data-default-pressed for initial state, listen for starwind:pressed-change, and call setPressed for later updates.
data-default-pressedstarwind:pressed-changesetPressed
Data Attributes
Runtime hooks
State
Metadata
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
Related Styled Components
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.