Skip to main content

Starwind UI v3.0 beta is now available! Read the migration guide

Installation

Framework Availability

Astro Available React Available

Switch is available for Astro and React.

Usage

variant

Runtime example adjustment

Mutable Astro examples use defaultChecked so the Runtime owns subsequent state changes. Reserve checked for controlled framework adapters that resynchronize the value.

size

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

Runtime example adjustment

Event listeners now attach to the Switch root and use the shared starwind:checked-change contract. The legacy component-specific event and exported event type are no longer required.

API Reference

Styled Component API

These props are added or materially changed by the installed Astro styled component. Standard HTML attributes remain available through the inherited interfaces noted below. 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

v2.0.0

  • Rebuilt Switch on Starwind Runtime for checked state, native form participation, and checked-change events.
  • See the Switch Primitive for the underlying unstyled anatomy and behavior API.

v1.3.3

  • Refactor tailwind variants functions into separate variants.ts file

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.