$npx @dinachi/cli@latest add switch
labeldefaultCheckeddisabledreadOnlysetting-switch.tsx
import { Switch, SwitchThumb } from "@/components/ui/switch"
export function SettingSwitch() {
return (
<label className="flex items-center gap-2">
<Switch>
<SwitchThumb />
</Switch>
Airplane Mode
</label>
)
}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>
);
}Status: Disabled
Hold the checked state to show it elsewhere.
import React from 'react';
import { Switch, SwitchThumb } from '@/components/ui/switch';
export function Example() {
const [enabled, setEnabled] = React.useState(false);
return (
<div className="space-y-3">
<label className="flex items-center space-x-2">
<Switch checked={enabled} onCheckedChange={setEnabled}>
<SwitchThumb />
</Switch>
<span className="text-sm font-medium">Dark Mode</span>
</label>
<p className="text-sm text-muted-foreground">
Status: {enabled ? 'Enabled' : 'Disabled'}
</p>
</div>
);
}On, off and disabled.
import { Switch, SwitchThumb } from '@/components/ui/switch';
export function Example() {
return (
<div className="space-y-4">
<label className="flex items-center space-x-2">
<Switch defaultChecked>
<SwitchThumb />
</Switch>
<span className="text-sm font-medium">Notifications (on)</span>
</label>
<label className="flex items-center space-x-2">
<Switch>
<SwitchThumb />
</Switch>
<span className="text-sm font-medium">Marketing emails (off)</span>
</label>
<label className="flex items-center space-x-2 text-muted-foreground">
<Switch disabled>
<SwitchThumb />
</Switch>
<span className="text-sm font-medium">Disabled</span>
</label>
</div>
);
}API
checkedboolean
Controlled checked state.defaultCheckedboolean = false
Starting state when uncontrolled.onCheckedChange
Runs for people and agents. agent
(checked, details) => voiddetails.cancel() refuses the change.disabled
Blocks people and agents.agent
boolean = falsereadOnly
Shows the state, refuses any change.agent
boolean = falsenamestring
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>.Takes the props of the element it renders.
Agent
Inside an AgentProvider, it appears in agent.snapshot() as switch.
Reads
labelThe label around the switch, or its aria-label.
checkedWhether the switch is on.
disabledTrue 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"andaria-checked; a hidden checkbox submits the value. - Name every switch with a wrapping
<label>or aria-label; the agent reads it. readOnlyand required are exposed asaria-readonlyandaria-required.- Reduced motion drops the thumb's slide.
Keyboard
SpaceEnterTurns the switch on or off
SpaceEnterTurns the switch on or off