Switch
A track and thumb that turns one setting on or off. Use it for settings that take effect at once, such as notifications or model access.
Installation
pnpm dlx shadcn@latest add https://sprout.tulipfarm.ai/r/switch.jsonCopies Switch into your app, installs the packages, and adds the styles. Then import it from @/components/ui/switch. Needs Tailwind CSS 4 and a components.json from shadcn init.
Install the following dependencies:
pnpm add clsx tailwind-merge @base-ui/reactAdd Sprout base once per app: the
cn()helper and the design tokens.lib/sprout/classes.ts- Design tokens, for your global CSS
Copy and paste the following code into your project.
'use client'; import { Switch as BaseSwitch } from '@base-ui/react/switch'; import { cn, stateClassName } from '@/lib/sprout/classes'; import { useFieldsetDisabled } from '@/lib/sprout/fieldset'; export const switchSizeNames = ['md', 'sm'] as const; export type SwitchSize = (typeof switchSizeNames)[number]; export interface SwitchProps extends BaseSwitch.Root.Props { size?: SwitchSize | undefined; } export function Switch({ size = 'md', className, disabled, ref, ...props }: SwitchProps) { const [fieldsetDisabled, fieldsetRef] = useFieldsetDisabled(ref); return ( <BaseSwitch.Root data-slot="switch" data-size={size} {...props} ref={fieldsetRef} disabled={disabled || fieldsetDisabled} className={stateClassName<BaseSwitch.Root.State>(cn('sprout-switch', `sprout-switch-size-${size}`), className)} > <BaseSwitch.Thumb data-slot="switch-thumb" className="sprout-switch-thumb" /> </BaseSwitch.Root> ); }And the files it imports:
lib/sprout/fieldset.ts
Add the styles to your global CSS, after
@import "tailwindcss".@layer components { .sprout-switch { --sprout-switch-thumb-off: var(--sprout-surface); --sprout-switch-travel: 14px; box-sizing: border-box; background-color: var(--sprout-muted); vertical-align: middle; cursor: pointer; outline-offset: 0; -webkit-tap-highlight-color: transparent; border: 1px solid #0000; border-radius: 999px; outline: 0 solid #0000; flex-shrink: 0; align-items: center; width: 32px; height: 18px; margin: 0; padding: 0; display: inline-flex; position: relative; } .sprout-switch-size-sm { --sprout-switch-travel: 10px; width: 24px; height: 14px; } .sprout-switch:after { content: ""; position: absolute; inset: -8px -12px; } .dark .sprout-switch { --sprout-switch-thumb-off: var(--sprout-text); } @media (prefers-color-scheme: dark) { :root:not(.light):not(.dark) .sprout-switch { --sprout-switch-thumb-off: var(--sprout-text); } } .sprout-switch:focus-visible { outline: var(--sprout-focus-width) solid var(--sprout-focus-ring); } .sprout-switch[data-checked] { background-color: var(--sprout-primary); } .sprout-switch[aria-invalid="true"] { border-color: var(--sprout-danger); box-shadow: 0 0 0 3px color-mix(in oklab, var(--sprout-danger) 20%, transparent); } .sprout-switch[aria-invalid="true"]:focus-visible { outline-color: var(--sprout-danger); } .sprout-switch[data-readonly] { cursor: default; } .sprout-switch[data-disabled] { cursor: not-allowed; opacity: .5; } .sprout-switch[aria-busy="true"] { cursor: progress; } .sprout-switch-thumb { background-color: var(--sprout-switch-thumb-off); pointer-events: none; border-radius: 999px; width: 16px; height: 16px; display: block; box-shadow: 0 1px 2px oklch(0% 0 0 / .12); } .sprout-switch-size-sm .sprout-switch-thumb { width: 12px; height: 12px; } .sprout-switch-thumb[data-checked] { background-color: var(--sprout-on-primary); transform: translateX(var(--sprout-switch-travel)); } .sprout-switch-thumb[data-checked]:dir(rtl) { transform: translateX(calc(var(--sprout-switch-travel) * -1)); } @media (prefers-reduced-motion: no-preference) { .sprout-switch { transition: background-color var(--sprout-motion-fast, .12s) var(--sprout-motion-ease, ease-out), border-color var(--sprout-motion-fast, .12s) var(--sprout-motion-ease, ease-out), box-shadow var(--sprout-motion-fast, .12s) var(--sprout-motion-ease, ease-out), opacity var(--sprout-motion-fast, .12s) var(--sprout-motion-ease, ease-out); } .sprout-switch-thumb { transition: transform var(--sprout-motion-normal, .18s) var(--sprout-motion-ease, ease-out), background-color var(--sprout-motion-fast, .12s) var(--sprout-motion-ease, ease-out); } } @media (forced-colors: active) { .sprout-switch { background-color: canvas; border-color: canvastext; } .sprout-switch[data-checked] { background-color: highlight; border-color: highlight; } .sprout-switch-thumb { background-color: canvastext; } .sprout-switch-thumb[data-checked] { background-color: highlighttext; } .sprout-switch:focus-visible { outline-color: highlight; } } }Import it from
@/components/ui/switch. Update the paths if your@/alias points elsewhere.
pnpm add @tulipfarm/sprout@import "tailwindcss";
@import "@tulipfarm/sprout/styles.css";The package keeps the source and ships fixes with each release. Import from @tulipfarm/sprout/switch.
Sizes and states
md is a 32 × 18px track for settings rows. sm is a 24 × 14px track for dense control strips.
Off
On
Disabled
Disabled on
Invalid
Controlled with a label
Wrap the switch in a Label and pass a state setter to onCheckedChange. Clicking the text toggles it.
Bud is idling.
In settings rows
Give the switch an id and pass the same id to SettingsRow htmlFor, so the row label names and toggles it.
Local preferences
Project name
Model access
Each row names its switch with aria-label. While a save runs, the row says Saving… and its switch is aria-busy. Every switch is disabled while a save, reload, or conflict is unresolved.
- Claude Sonnet 4.5Credits per 1M input / output tokens: 3 / 15
- GPT-5Credits per 1M input / output tokens: 1.25 / 10
- DeepSeek V3.2Credits per 1M input / output tokens: 0.28 / 0.42
- Kimi K2 Thinking with extended reasoningCredits per 1M input / output tokens: 0.6 / 2.5
- Grok 4Credits per 1M input / output tokens: Unknown / Unknown
Usage
import { Switch } from "@tulipfarm/sprout/switch";
import { Label } from "@tulipfarm/sprout/label";
import { SettingsRow } from "@tulipfarm/sprout/settings-list";
const [paused, setPaused] = useState(false);
<Label>
<Switch checked={paused} onCheckedChange={setPaused} />
Pause motion
</Label>
<SettingsRow label="Email summaries" htmlFor="summary">
<Switch id="summary" defaultChecked />
</SettingsRow>
<Switch
aria-label={`Allow ${model.name}`}
checked={model.enabled}
disabled={saving}
aria-busy={pending}
onCheckedChange={(enabled) => save(model.id, enabled)}
/>Pass checked and onCheckedChange to control the switch, or defaultChecked to let it keep its own state.onCheckedChange gets a boolean, so a state setter can be passed straight in.
Props
size is "md" (the default, a 32 × 18px track with a 16px thumb) or "sm" (a 24 × 14px track with a 12px thumb). Both have a hit area 12px wider and 8px taller on each side.
disabled dims the switch and blocks changes; a disabled<fieldset> around it does the same. Setaria-invalid to draw a danger border and ring. Setaria-busy while a change is saving to show a progress cursor.
The switch also takes Base UI props such as id,name, value, required,readOnly, inputRef, and state functions forclassName.
Accessibility
Switch renders an element with role="switch" andaria-checked, plus a hidden input for forms. Tab moves focus to it, and Space or Enter toggles it. A focused switch shows a 2px focus ring. Name every switch with a wrapping Label, aLabel htmlFor or SettingsRow htmlFor, oraria-label.
The track color changes in 120ms and the thumb slides in 180ms. Both stop when reduced motion is requested. The thumb slides the other way in right-to-left text. In forced colors mode the track gets a system border and an on switch is highlighted.
Theme tokens
Off uses a --sprout-muted track with a--sprout-surface thumb (--sprout-text in dark mode). On uses --sprout-primary with a--sprout-on-primary thumb, so the opt-in TulipFarm theme tints it green. Invalid uses --sprout-danger, and focus uses--sprout-focus.