Skip to main content

Starwind UI v3.0 beta is now available! Check out the new docs and try it out!

Switch

Installation

Usage

variant

size

Event Handling

Whenever toggled, the switch emits a starwind-switch:change event. This can be listened to in order to update the UI based on the event.detail.checked value.

Switch Event Test

Switch is currently: off

The event emission happens on state change and looks like:

const event = new CustomEvent<SwitchChangeEvent["detail"]>("starwind-switch:change", {
detail: {
checked: newState, // boolean
switchId: this.switchButton.id,
},
bubbles: true,
cancelable: true,
});
this.switchButton.dispatchEvent(event);

API Reference

Switch

An accessible toggle switch component with multiple variants and sizes.

PropTypeDefault
idstringRequired
labelstring-
checkedbooleanfalse
size"sm" | "md" | "lg""md"
variant"default" | "primary" | "secondary" | "info" | "success" | "warning" | "error""default"
classstring-
<Switch id="notifications" label="Notifications" variant="primary" size="md" checked />

Additional Notes:

  • id: Used to associate the internal button with the label
  • label: Accessible label rendered next to the switch
  • This component accepts all standard HTML <button> attributes except role, type, and aria-checked

Changelog

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.