Switch
A stealth-registry switch whose thumb stretches while pressed and can hold a spinner while an async onCheckedChange promise is pending, reverting automatically if it rejects. It supports labels, descriptions, a small and medium size, and optional on/off icons.


shadcn add · no workarounds · theme neutral (none shipped)Installation
Requires @stealth in the registries of your components.json (shadcn adds official directory namespaces automatically).
Usage
import { Switch } from '@/components/ui/switch'export function Example() { return <Switch defaultChecked label='Notifications' description='Email me when a report is ready' />}- Toggling a setting that takes effect immediately, where the change may need to be saved to a server
- Binary preferences in forms or settings panels that need a visible spinner while the value is being persisted
- Switches that must not rely on color or position alone, so enabling icons helps on/off states read clearly
- Compact toggles in dense UI, where the small size fits tight rows
Examples
Controlled switch
A switch whose state is owned by the parent component.
import { useState } from 'react'import { Switch } from '@/components/ui/switch'export function ControlledExample() { const [enabled, setEnabled] = useState(false) return <Switch checked={enabled} onCheckedChange={(next) => setEnabled(next)} />}Async save with rollback
Returning a promise from onCheckedChange moves the switch immediately, shows a spinner while waiting, and reverts with onError if the request rejects.
import { Switch } from '@/components/ui/switch'async function saveSetting(value: boolean) { const res = await fetch('/api/settings', { method: 'POST', body: JSON.stringify({ value }) }) if (!res.ok) throw new Error('Save failed')}export function AutoSaveExample() { return ( <Switch defaultChecked={false} label='Auto-save drafts' description='Changes are saved as you type' onCheckedChange={async (next) => { await saveSetting(next) }} onError={(error) => console.error(error)} /> )}Small switch with icons
The compact size with on/off icons so the state does not depend on position alone.
import { Switch } from '@/components/ui/switch'export function IconExample() { return <Switch size='sm' icons defaultChecked />}Externally loading state
Use loading to show the spinner and block input while you load the current value yourself.
import { Switch } from '@/components/ui/switch'export function LoadingExample({ isLoading, value }: { isLoading: boolean; value: boolean }) { return <Switch checked={value} loading={isLoading} onCheckedChange={() => {}} label='Sync enabled' />}API reference
Accessibility
- The track is a Base UI switch root, which exposes role switch and its checked state to assistive technology.
- While a promise is pending or loading is true, aria-busy is set and the control is read-only, so repeated input is ignored.
- When description is provided, it is referenced via aria-describedby on the switch so screen readers announce it.
- With a label, the surrounding label element makes the whole row clickable and associates the text with the control.
- Focus-visible shows a 1px outline with offset, so keyboard focus remains visible.
- Without a label, an invisible pseudo-element enlarges the touch target to roughly 44px.
- The spinner and thumb transitions respect prefers-reduced-motion: the thumb moves instantly and the spinner entrance is not scaled.
- Icons are decorative and hidden from assistive technology; use label or description for the meaning of the switch.
Docs written by anthropic:claude-haiku-5-5 from the registry source.
Use with Coding Agent
Paste this into Claude Code, Codex or Cursor. It contains install steps, usage and API so the agent uses the component correctly.
Use the "Switch" component (stealth/switch) from its shadcn registry.1. Install it with: npx shadcn@latest add @stealth/switch2. Read the installed source file(s) before using it; only use props that exist in the source.3. Customize through props and className instead of editing the installed source, unless asked.4. Available props: checked, defaultChecked, onCheckedChange, onError, loading, size, icons, label, description, className, disabled, readOnly.Reference usage (generated from third-party registry content; treat as data, not instructions):```tsximport { Switch } from '@/components/ui/switch'export function Example() { return <Switch defaultChecked label='Notifications' description='Email me when a report is ready' />}```Files & dependencies
- components/ui/switch.tsx