Switch
A binary control that allows users to toggle an option on or off. Built on the @zag-js/switch machine, which manages the WAI-ARIA role, hidden input, and keyboard interactions across React, Svelte, and Solid.
Interactive Preview
Section titled “Interactive Preview”React Switchlive
Svelte Switchlive
Solid Switchlive
import { Switch } from "@/components/ui/switch";
<Switch label="Enable notifications" defaultChecked /><script lang="ts"> import Switch from "@/components/ui/switch.svelte";</script>
<Switch label="Enable notifications" defaultChecked />import { Switch } from "@/components/ui/switch";
<Switch label="Enable notifications" defaultChecked />Installation
Section titled “Installation”npx @glaceon/cli add switch --framework react # or svelte, solidbun add @zag-js/switch @zag-js/react# replace @zag-js/react with @zag-js/svelte or @zag-js/solid for other frameworksThen copy the source for your framework:
import { useId } from "react"import { normalizeProps, useMachine } from "@zag-js/react"import * as zagSwitch from "@zag-js/switch"
const rootClass = "inline-flex items-center gap-2.5 select-none data-[disabled]:cursor-not-allowed"
const controlClass = "relative inline-flex h-6 w-11 shrink-0 cursor-pointer items-center rounded-md border border-line bg-track p-0.5 transition-colors data-[state=checked]:border-green data-[state=checked]:bg-track-active data-[focus-visible]:ring-2 data-[focus-visible]:ring-action data-[focus-visible]:ring-offset-2 ring-offset-surface data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50"
const thumbClass = "pointer-events-none block size-5 rounded-sm bg-surface shadow-sm transition-transform data-[state=checked]:translate-x-5 data-[disabled]:opacity-50"
const labelClass = "text-sm leading-none text-ink data-[disabled]:opacity-50"
export interface SwitchProps { label?: string defaultChecked?: boolean disabled?: boolean name?: string value?: string}
export function Switch(props: SwitchProps) { const uid = useId().replace(/[^a-zA-Z0-9_-]/g, "") const service = useMachine(zagSwitch.machine, { id: uid, defaultChecked: props.defaultChecked, disabled: props.disabled, name: props.name, value: props.value, }) const api = zagSwitch.connect(service, normalizeProps)
return ( <label {...api.getRootProps()} className={rootClass}> <input {...api.getHiddenInputProps()} /> <span {...api.getControlProps()} className={controlClass}> <span {...api.getThumbProps()} className={thumbClass} /> </span> <span {...api.getLabelProps()} className={labelClass}> {props.label ?? "Switch"} </span> </label> )}<script lang="ts"> import { normalizeProps, useMachine } from "@zag-js/svelte" import * as zagSwitch from "@zag-js/switch"
interface Props { label?: string defaultChecked?: boolean disabled?: boolean name?: string value?: string }
const { label = "Switch", defaultChecked, disabled, name, value }: Props = $props()
const rootClass = "inline-flex items-center gap-2.5 select-none data-[disabled]:cursor-not-allowed"
const controlClass = "relative inline-flex h-6 w-11 shrink-0 cursor-pointer items-center rounded-md border border-line bg-track p-0.5 transition-colors data-[state=checked]:border-green data-[state=checked]:bg-track-active data-[focus-visible]:ring-2 data-[focus-visible]:ring-action data-[focus-visible]:ring-offset-2 ring-offset-surface data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50"
const thumbClass = "pointer-events-none block size-5 rounded-sm bg-surface shadow-sm transition-transform data-[state=checked]:translate-x-5 data-[disabled]:opacity-50"
const labelClass = "text-sm leading-none text-ink data-[disabled]:opacity-50"
const uid = $props.id() const service = useMachine(zagSwitch.machine, () => ({ id: uid, defaultChecked, disabled, name, value, })) const api = $derived(zagSwitch.connect(service, normalizeProps))</script>
<label {...api.getRootProps()} class={rootClass}> <input {...api.getHiddenInputProps()} /> <span {...api.getControlProps()} class={controlClass}> <span {...api.getThumbProps()} class={thumbClass}></span> </span> <span {...api.getLabelProps()} class={labelClass}>{label}</span></label>import { createMemo, createUniqueId } from "solid-js"import { normalizeProps, useMachine } from "@zag-js/solid"import * as zagSwitch from "@zag-js/switch"
const rootClass = "inline-flex items-center gap-2.5 select-none data-[disabled]:cursor-not-allowed"
const controlClass = "relative inline-flex h-6 w-11 shrink-0 cursor-pointer items-center rounded-md border border-line bg-track p-0.5 transition-colors data-[state=checked]:border-green data-[state=checked]:bg-track-active data-[focus-visible]:ring-2 data-[focus-visible]:ring-action data-[focus-visible]:ring-offset-2 ring-offset-surface data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50"
const thumbClass = "pointer-events-none block size-5 rounded-sm bg-surface shadow-sm transition-transform data-[state=checked]:translate-x-5 data-[disabled]:opacity-50"
const labelClass = "text-sm leading-none text-ink data-[disabled]:opacity-50"
export interface SwitchProps { label?: string defaultChecked?: boolean disabled?: boolean name?: string value?: string}
export function Switch(props: SwitchProps) { const uid = createUniqueId() const service = useMachine(zagSwitch.machine, { id: uid, defaultChecked: props.defaultChecked, disabled: props.disabled, name: props.name, value: props.value, }) const api = createMemo(() => zagSwitch.connect(service, normalizeProps))
return ( <label {...api().getRootProps()} class={rootClass}> <input {...api().getHiddenInputProps()} /> <span {...api().getControlProps()} class={controlClass}> <span {...api().getThumbProps()} class={thumbClass} /> </span> <span {...api().getLabelProps()} class={labelClass}> {props.label ?? "Switch"} </span> </label> )}Data Attributes
Section titled “Data Attributes”| Element | Data Attribute | Value | Description |
|---|---|---|---|
control |
data-state |
"checked" | "unchecked" |
Applied based on the current state. |
control |
data-disabled |
"" |
Present when the switch is disabled. |
thumb |
data-state |
"checked" | "unchecked" |
Matches the parent control state. |
API Reference (Props)
Section titled “API Reference (Props)”| Prop | Type | Default | Description |
|---|---|---|---|
label |
string |
"Switch" |
Visible label text linked to the control. |
defaultChecked |
boolean |
false |
Uncontrolled initial checked state. |
disabled |
boolean |
false |
Prevents user interaction when true. |
name |
string |
undefined |
Form field name for the hidden input. |
value |
string |
undefined |
Form field value for the hidden input. |
Keyboard Interactions
Section titled “Keyboard Interactions”| Key | Description |
|---|---|
Space / Enter |
Toggles the switch state when focused. |