Switch

An on/off setting that applies at once.

Source
$npx @dinachi/cli@latest add switch
label
defaultChecked
disabled
readOnly

Usage

import { Switch, SwitchThumb } from "@/components/ui/switch"

export function AirplaneMode() {
  return (
    <label className="flex items-center gap-2">
      <Switch>
        <SwitchThumb />
      </Switch>
      Airplane Mode
    </label>
  )
}

Examples

A switch named by the label around it.
import { Switch, SwitchThumb } from '@/components/ui/switch';

export function Example() {
  return (
    <label className="flex items-center space-x-2">
      <Switch>
        <SwitchThumb />
      </Switch>
      <span className="text-sm font-medium">Airplane Mode</span>
    </label>
  );
}

API

checkedboolean
Controlled checked state.
defaultCheckedboolean = false
Starting state when uncontrolled.
onCheckedChange
agent
(checked, details) => void
Runs for people and agents. details.cancel() refuses the change.
disabled
agent
boolean = false
Blocks people and agents.
readOnly
agent
boolean = false
Shows the state, refuses any change.
namestring
Submits the switch with a form.
valuestring
Submitted while on. The browser sends on without it.
uncheckedValuestring
Submitted while off. Without it, nothing is sent.
requiredboolean = false
Must be on before the form submits.
nativeButtonboolean = false
Set when render swaps in a real <button>.

Agent

Inside an AgentProvider, it appears in agent.snapshot() as switch.

Reads

label

The label around the switch, or its aria-label.

checked

Whether the switch is on.

disabled

True when the switch is blocked.

Actions

toggle()

Flips the switch.

setChecked(true)

Sets it on or off. The current state is a no-op.

An agent's change runs through onCheckedChange. isAgentChange(details) is true for it; details.cancel() refuses it and the agent is told. Disabled or readOnly switches refuse on their own.

await agent.act({
  steps: [{ ref: "Airplane Mode", action: "setChecked", args: [true] }],
})

Accessibility

  • The track has role="switch" and aria-checked; a hidden checkbox submits the value.
  • Name every switch with a wrapping <label> or aria-label; the agent reads it.
  • readOnly and required are exposed as aria-readonly and aria-required.
  • Reduced motion drops the thumb's slide.