Switch Primitive
Use Switch to turn a setting on or off. Its thumb and accessible state follow the current value.
Anatomy
Use the Astro primitive adapter to render Switch anatomy with the Runtime wiring included.
---import { Switch } from "@starwind-ui/astro/switch";---
<Switch.Root> <Switch.Thumb /></Switch.Root>Use the React primitive adapter when Switch state participates in React rendering.
import { Switch } from "@starwind-ui/react/switch";
export function Example() { return ( <Switch.Root> <Switch.Thumb /> </Switch.Root> );}Use the Vue components to build Switch.
<script setup lang="ts">import Switch from "@starwind-ui/vue/switch";</script>
<template> <Switch.Root> <Switch.Thumb /> </Switch.Root></template>Use the Svelte components to build Switch.
<script lang="ts"> import { Switch } from "@starwind-ui/svelte/switch";</script>
<Switch.Root> <Switch.Thumb /></Switch.Root>Render the Switch data-sw-* contract yourself, then initialize createSwitch.
<span data-sw-switch role="switch"> <span data-sw-switch-thumb></span> <input data-sw-switch-input type="checkbox" aria-hidden="true" tabindex="-1" /></span>
<script type="module"> import { createSwitch } from "@starwind-ui/runtime/switch";
const root = document.querySelector("[data-sw-switch]"); if (root) { createSwitch(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 Switch Runtime instance.
- Default element
- span
- Discovery hook
- data-sw-switch
- Role
- switch
Props
checked boolean -
- Description
- Controls whether Switch is checked.
- Kind
- control
- Targets
- -
- Full type
- boolean
- React
Use checked for controlled state and defaultChecked for default state, and onCheckedChange for change proposals.
checkeddefaultCheckedonCheckedChange
- Astro
Use checked or defaultChecked for initial state, listen for starwind:checked-change, and call setChecked for later updates.
checkeddefaultCheckedstarwind:checked-changesetChecked
- Runtime / HTML
Use data-default-checked for initial state, listen for starwind:checked-change, and call setChecked for later updates.
data-default-checkedstarwind:checked-changesetChecked
defaultChecked boolean false
- Description
- Sets whether Switch starts checked for uncontrolled usage.
- Kind
- control
- Targets
- -
- Full type
- boolean
- React
Use checked for controlled state and defaultChecked for default state, and onCheckedChange for change proposals.
checkeddefaultCheckedonCheckedChange
- Astro
Use checked or defaultChecked for initial state, listen for starwind:checked-change, and call setChecked for later updates.
checkeddefaultCheckedstarwind:checked-changesetChecked
- Runtime / HTML
Use data-default-checked for initial state, listen for starwind:checked-change, and call setChecked for later updates.
data-default-checkedstarwind:checked-changesetChecked
disabled boolean false
- Description
- Disables the Root part.
- Kind
- option
- Targets
- -
- Full type
- boolean
form string -
- Description
- Associates the control with a form element.
- Kind
- option
- Targets
- -
- Full type
- string
id string -
- Description
- Sets the id used by the associated native control.
- Kind
- option
- Targets
- -
- Full type
- string
name string -
- Description
- Sets the submitted form field name.
- Kind
- option
- Targets
- -
- Full type
- string
nativeButton boolean false
- Description
- Renders the control as a native button element.
- Kind
- rendering
- Targets
- root
- Full type
- boolean
readOnly boolean false
- Description
- Marks the control as read-only.
- Kind
- option
- Targets
- -
- Full type
- boolean
required boolean false
- Description
- Marks the form control as required.
- Kind
- option
- Targets
- -
- Full type
- boolean
uncheckedValue string -
- Description
- Sets the value submitted when the checkbox is unchecked.
- Kind
- option
- Targets
- -
- Full type
- string
value string -
- Description
- Controls the current Switch value.
- Kind
- option
- Targets
- -
- Full type
- string
onCheckedChange (checked: boolean, details: SwitchCheckedChangeDetails) => void -
- Description
- Runs when the Switch checked state changes.
- Kind
- callback
- Targets
- -
- Full type
- (checked: boolean, details: SwitchCheckedChangeDetails) => void
- React
Use checked for controlled state and defaultChecked for default state, and onCheckedChange for change proposals.
checkeddefaultCheckedonCheckedChange
- Astro
Use checked or defaultChecked for initial state, listen for starwind:checked-change, and call setChecked for later updates.
checkeddefaultCheckedstarwind:checked-changesetChecked
- Runtime / HTML
Use data-default-checked for initial state, listen for starwind:checked-change, and call setChecked for later updates.
data-default-checkedstarwind:checked-changesetChecked
Events
checkedChange onCheckedChange checked: boolean
- Description
- Fires when the checked state changes for Switch.
- DOM event
- starwind:checked-change
- Details type
- SwitchCheckedChangeDetails
- 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
checked boolean checked / defaultChecked
- Description
- Tracks whether Switch is checked.
- Initial attribute
- data-default-checked
- Runtime getter
- getChecked
- Runtime setter
- setChecked
- React
Use checked for controlled state and defaultChecked for default state, and onCheckedChange for change proposals.
checkeddefaultCheckedonCheckedChange
- Astro
Use checked or defaultChecked for initial state, listen for starwind:checked-change, and call setChecked for later updates.
checkeddefaultCheckedstarwind:checked-changesetChecked
- Runtime / HTML
Use data-default-checked for initial state, listen for starwind:checked-change, and call setChecked for later updates.
data-default-checkedstarwind:checked-changesetChecked
Data Attributes
Runtime hooks
State
Metadata
Thumb
The draggable thumb for Switch.
- Default element
- span
- Discovery hook
- data-sw-switch-thumb
- Role
- -
Data Attributes
Runtime hooks
Input
The native input synchronized by Switch.
- Default element
- input
- Discovery hook
- data-sw-switch-input
- Role
- -
Data Attributes
Runtime hooks
Unchecked Input
The hidden native input used when Switch submits an unchecked value.
- Default element
- input
- Discovery hook
- data-sw-switch-unchecked-input
- Role
- -
Data Attributes
Runtime hooks
Runtime API
- Factory
createSwitch- Import
@starwind-ui/runtime/switch- Root hook
- root
data-sw-switch - Option props
- checked, defaultChecked, disabled, form, id, name, readOnly, required, uncheckedValue, value
Runtime Setters
Form Participation
| Fact | Value |
|---|---|
| Form props | form, id, name, required, uncheckedValue, value |
| Hidden input | input (checkbox) |
| Field integration | Yes |
Related Styled Components
Changelog
View version history v1.1.1 8 releases
v1.1.1
- In Vue, this component now needs Vue 3.5.21 or newer.
v1.1.0
- In Astro, an attribute that you set on a Switch part now wins over the part's own default for the same attribute, such as `role` or `aria-label`.
- In Svelte, a defined `checked` that you pass when Switch 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, Switch keeps its own state.
- Inside a `Field` in Astro, Switch now clears when the form resets. Before, it stayed on.
v1.0.3
- When the browser flags a required Switch as invalid, focus now goes to the switch that the user sees.
- In Svelte apps that render on the server, a form reset now puts Switch back to its default checked state. Before, a reset after the page loaded cleared it.
v1.0.2
- React and Vue Switch now use the app-provided checked state for their initial native input value.
- Vue Switch keeps its previous checked value when a change is canceled.
- Fixed React and Vue form resets restoring the wrong value after a control is recreated. Resets now use its original default.
- Reduced the JavaScript loaded by Switch when it is used without Field.
- Reduced repeated JavaScript when Astro Switch is used with other Starwind components.
- Reduced JavaScript used by Svelte Switch. Element props, event handlers, and refs keep the same behavior.
- Reduced JavaScript used by Vue Switch. 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.1
- 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.
- In React, Switch stays connected to its native form when your app controls `checked`.
v0.1.0
- First release. Switch handles its checked state, sends its value with the form, and fires checked-change events.