Toggle
A two-state button that stays pressed until it is pressed again. Built on the @zag-js/toggle machine, which manages aria-pressed, keyboard interactions, and the data-state attribute used for styling.
Interactive Preview
Section titled “Interactive Preview”React Togglelive
Svelte Togglelive
Solid Togglelive
import { Toggle } from "@/components/ui/toggle";
<Toggle label="Show filters" /><Toggle label="Show filters" defaultPressed /><script lang="ts"> import Toggle from "@/components/ui/toggle.svelte";</script>
<Toggle label="Show filters" /><Toggle label="Show filters" defaultPressed />import { Toggle } from "@/components/ui/toggle";
<Toggle label="Show filters" /><Toggle label="Show filters" defaultPressed />Installation
Section titled “Installation”npx @glaceon/cli add toggle --framework react # or svelte, solidbun add @zag-js/toggle @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 { normalizeProps, useMachine } from "@zag-js/react"import * as zagToggle from "@zag-js/toggle"
const rootClass = "inline-flex h-11 items-center justify-center rounded-md border border-line bg-surface px-4 text-sm font-bold text-ink transition-colors data-[state=on]:border-green data-[state=on]:bg-green-soft data-[state=on]:text-green-deep focus-visible:ring-2 focus-visible:ring-action focus-visible:ring-offset-2 ring-offset-surface data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50"
export interface ToggleProps { label?: string defaultPressed?: boolean disabled?: boolean}
export function Toggle(props: ToggleProps) { const service = useMachine(zagToggle.machine, { defaultPressed: props.defaultPressed, disabled: props.disabled, }) const api = zagToggle.connect(service, normalizeProps)
return ( <button {...api.getRootProps()} className={rootClass}> {props.label ?? "Toggle"} </button> )}<script lang="ts"> import { normalizeProps, useMachine } from "@zag-js/svelte" import * as zagToggle from "@zag-js/toggle"
interface Props { label?: string defaultPressed?: boolean disabled?: boolean }
const { label = "Toggle", defaultPressed, disabled }: Props = $props()
const rootClass = "inline-flex h-11 items-center justify-center rounded-md border border-line bg-surface px-4 text-sm font-bold text-ink transition-colors data-[state=on]:border-green data-[state=on]:bg-green-soft data-[state=on]:text-green-deep focus-visible:ring-2 focus-visible:ring-action focus-visible:ring-offset-2 ring-offset-surface data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50"
const service = useMachine(zagToggle.machine, () => ({ defaultPressed, disabled, })) const api = $derived(zagToggle.connect(service, normalizeProps))</script>
<button {...api.getRootProps()} class={rootClass}> {label}</button>import { createMemo } from "solid-js"import { normalizeProps, useMachine } from "@zag-js/solid"import * as zagToggle from "@zag-js/toggle"
const rootClass = "inline-flex h-11 items-center justify-center rounded-md border border-line bg-surface px-4 text-sm font-bold text-ink transition-colors data-[state=on]:border-green data-[state=on]:bg-green-soft data-[state=on]:text-green-deep focus-visible:ring-2 focus-visible:ring-action focus-visible:ring-offset-2 ring-offset-surface data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50"
export interface ToggleProps { label?: string defaultPressed?: boolean disabled?: boolean}
export function Toggle(props: ToggleProps) { const service = useMachine(zagToggle.machine, { defaultPressed: props.defaultPressed, disabled: props.disabled, }) const api = createMemo(() => zagToggle.connect(service, normalizeProps))
return ( <button {...api().getRootProps()} class={rootClass}> {props.label ?? "Toggle"} </button> )}Data Attributes
Section titled “Data Attributes”| Element | Data Attribute | Value | Description |
|---|---|---|---|
root |
data-state |
"on" | "off" |
Applied based on the current pressed state. |
root |
data-pressed |
"" |
Present when the toggle is pressed. |
root |
data-disabled |
"" |
Present when the toggle is disabled. |
API Reference (Props)
Section titled “API Reference (Props)”| Prop | Type | Default | Description |
|---|---|---|---|
label |
string |
"Toggle" |
Visible button text. |
defaultPressed |
boolean |
false |
Uncontrolled initial pressed state. |
disabled |
boolean |
false |
Prevents user interaction when true. |
Keyboard Interactions
Section titled “Keyboard Interactions”| Key | Description |
|---|---|
Space / Enter |
Toggles the pressed state when focused. |