Skip to main content

Starwind UI v3.0 is now available! Migration guide

Installation

Usage

Give each Switch an accessible name with the label prop, an aria-label, or a <label for> that points at its id.

---
import { Switch } from "@/components/starwind/switch";
---
<Switch id="demo-switch" label="Switch" />

Variants

Sizes

Event Handling

Whenever toggled, the Switch root emits starwind:checked-change. Read the next state from event.detail.checked.

Switch Event Test

Switch is currently: off

API Reference

Styled Component API

These props are added or materially changed by the installed styled component. Standard HTML attributes remain available through the inherited interfaces noted below. Expand a prop to see named type definitions and framework-specific imports. Follow the Primitive and Runtime links for lower-level behavior props.

Switch

Inherits button attributes. Omits `aria-checked`, `defaultChecked`, `onChange`, `role`, and `type`.

Contains the following additional props:

Prop Type Default Toggle details
defaultChecked boolean —
Description
Sets the initial checked state when the choice is uncontrolled.
Classification
Primitive override
Primitive prop
switch.Root.defaultChecked
id Required string —
Description
Sets the element id used by labels and related controls.
Classification
Primitive override
Primitive prop
switch.Root.id
label string —
Description
Provides accessible text for the component.
Classification
Wrapper prop
padding number —
Description
Sets the component's internal padding.
Classification
Wrapper prop
size "sm" | "md" | "lg" "md"
Description
Selects the component's visual size.
Classification
Styled variant
variant "primary" | "secondary" | "default" | "info" | "success" | "warning" | "error" "default"
Description
Selects the component's visual variant.
Classification
Styled variant

Primitive And Runtime API

Use these references when you need the lower-level behavior APIs behind Switch.

Primitive API

Runtime API

Switch primitive
createSwitch from @starwind-ui/runtime/switch

Changelog

View version history v2.1.0 10 releases

v2.1.0

  • 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.
  • In Vue, a Boolean prop that you leave out, such as defaultChecked, no longer becomes false. The default of the underlying component applies.
  • Inside a Field in Astro, Switch now clears when the form resets. Before, it stayed on.

v2.0.3

  • A Switch now gets its name from a <label for> that points at its id. Before, a Switch without a label or aria-label prop was always read as “switch”, and screen readers ignored the label next to it.
  • If a Switch has no visible label, give it a label prop, an aria-label, or a <label for> that points at its id. Without one of these, screen readers do not announce a name for it.
  • When the browser flags a required Switch as invalid, focus now goes to the switch that the user sees.

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

v2.0.1

  • In React, you can cancel a change before it applies. The component then keeps its previous state, and it shows only the changes that apply.
  • Associated the React Switch with its native form owner for submission and reset behavior.

v2.0.0

  • Switch now gets its behavior from Starwind Runtime. Runtime handles the checked state, the native form value, and the checked-change events.
  • See the Switch Primitive for the unstyled parts and the behavior API.

v1.3.3

  • The Tailwind variants now live in their own variants.ts file in the component folder.

v1.3.1

  • Add starwind:init event listener to enable initialization of additional Switches loaded after an initial page load, such as when using server islands

v1.3.0

  • style and focus state updates

v1.2.0

  • Add a data-slot attribute to all components to enable global styling updates

v1.1.0

  • tailwind-variants now implemented. This uses tailwind-merge under the hood to merge Tailwind classes without style conflicts, allowing you to override any existing classes using the “class” prop.