A Radix toggle switch in two sizes with a CSS-animated thumb, for settings that take effect immediately. Stops animating under reduced motion.
What changed
- 1.0.1 Oct 11, 2026Switch track, thumb travel, and checkbox and radio size follow the platform feel tokens (--bm-switch-*, --bm-check-size); select and command surfaces take the feel's frosting. Unchanged without a feel.
- 1.0.0 Sep 28, 2026First release.
import { Label } from "@/components/ballmac/label"
import { Switch } from "@/components/ballmac/switch"
export default function SwitchDemo() {
return (
<div className="flex flex-col gap-4">
<div className="flex items-center gap-3">
<Switch id="airplane-mode" defaultChecked />
<Label htmlFor="airplane-mode">Airplane mode</Label>
</div>
<div className="flex items-center gap-3">
<Switch id="compact" size="sm" />
<Label htmlFor="compact">Compact view</Label>
</div>
</div>
)
}Installation
The default, and what a Radix-style project installs.
$ pnpm dlx shadcn@latest add @ballmac/switchInstall the dependencies.
$ pnpm add radix-ui class-variance-authorityCopy the source into your project.
components/ballmac/switch.tsx// Ballmac UI: Switch. https://ui.ballmac.com/components/switch // Based on shadcn/ui's Switch (MIT, Copyright (c) 2023 shadcn), restyled with sizes. "use client" import * as React from "react" import { cva, type VariantProps } from "class-variance-authority" import { Switch as SwitchPrimitive } from "radix-ui" import { cn } from "@/lib/utils" const switchVariants = cva( "peer group/switch relative inline-flex shrink-0 items-center rounded-full border border-transparent shadow-xs outline-none transition-[background-color,box-shadow] duration-200 motion-reduce:transition-none after:absolute after:-inset-2 after:content-[''] focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:ring-[3px] aria-invalid:ring-destructive/20 data-[state=checked]:bg-primary data-[state=unchecked]:bg-input dark:data-[state=unchecked]:bg-input/80", { variants: { size: { sm: "h-4 w-7", default: "h-[var(--bm-switch-h,calc(var(--spacing)*5))] w-[var(--bm-switch-w,calc(var(--spacing)*9))]", }, }, defaultVariants: { size: "default" }, } ) type SwitchProps = React.ComponentProps<typeof SwitchPrimitive.Root> & VariantProps<typeof switchVariants> function Switch({ className, size = "default", ...props }: SwitchProps) { return ( <SwitchPrimitive.Root data-slot="switch" data-size={size ?? "default"} className={cn(switchVariants({ size }), className)} {...props} > <SwitchPrimitive.Thumb data-slot="switch-thumb" className={cn( "pointer-events-none block rounded-full bg-background shadow-[0_1px_2px_0_rgb(0_0_0/0.2)] ring-0 transition-transform duration-200 ease-[cubic-bezier(0.2,0.8,0.2,1)] motion-reduce:transition-none data-[state=unchecked]:translate-x-px data-[state=checked]:translate-x-[calc(100%+1px)] rtl:data-[state=unchecked]:-translate-x-px rtl:data-[state=checked]:-translate-x-[calc(100%+1px)] dark:data-[state=unchecked]:bg-foreground dark:data-[state=checked]:bg-primary-foreground", "size-[calc(var(--bm-switch-h,calc(var(--spacing)*5))_-_var(--spacing))] group-data-[size=sm]/switch:size-3 group-data-[size=default]/switch:data-[state=checked]:translate-x-[calc(var(--bm-switch-w,calc(var(--spacing)*9))_-_var(--bm-switch-h,calc(var(--spacing)*5))_+_1px)] group-data-[size=default]/switch:rtl:data-[state=checked]:-translate-x-[calc(var(--bm-switch-w,calc(var(--spacing)*9))_-_var(--bm-switch-h,calc(var(--spacing)*5))_+_1px)]" )} /> </SwitchPrimitive.Root> ) } export { Switch, switchVariants, type SwitchProps }Update the import paths to match your project setup.
$ pnpm dlx shadcn@latest add https://ui.ballmac.com/r/base/switch.jsonInstall the dependencies.
$ pnpm add class-variance-authority @base-ui/react@^1.9.0Add the Ballmac items it builds on (their Base UI versions where they have one).
$ pnpm dlx shadcn@latest add https://ui.ballmac.com/r/base/fieldset.jsonCopy the source into your project.
components/ballmac/switch.tsx// Ballmac UI: Switch. https://ui.ballmac.com/components/switch // Based on shadcn/ui's Switch (MIT, Copyright (c) 2023 shadcn), restyled with sizes. "use client" import * as React from "react" import { cva, type VariantProps } from "class-variance-authority" import { Switch as SwitchPrimitive } from "@base-ui/react/switch" import { useFieldsetDisabled } from "@/lib/ballmac/fieldset" import { cn } from "@/lib/utils" const switchVariants = cva( "peer group/switch relative inline-flex shrink-0 items-center rounded-full border border-transparent shadow-xs outline-none transition-[background-color,box-shadow] duration-200 motion-reduce:transition-none after:absolute after:-inset-2 after:content-[''] focus-visible:ring-[3px] focus-visible:ring-ring/50 data-disabled:cursor-not-allowed data-disabled:opacity-50 aria-invalid:ring-[3px] aria-invalid:ring-destructive/20 data-checked:bg-primary data-unchecked:bg-input dark:data-unchecked:bg-input/80", { variants: { size: { sm: "h-4 w-7", default: "h-[var(--bm-switch-h,calc(var(--spacing)*5))] w-[var(--bm-switch-w,calc(var(--spacing)*9))]", }, }, defaultVariants: { size: "default" }, } ) type SwitchProps = Omit<React.ComponentProps<typeof SwitchPrimitive.Root>, "className"> & { className?: string } & VariantProps<typeof switchVariants> function Switch({ className, size = "default", disabled, ref, ...props }: SwitchProps) { const fieldset = useFieldsetDisabled<HTMLElement>(disabled, ref as React.Ref<HTMLElement>) return ( <SwitchPrimitive.Root ref={fieldset.ref} disabled={fieldset.disabled} data-slot="switch" data-size={size ?? "default"} className={cn(switchVariants({ size }), className)} {...props} > <SwitchPrimitive.Thumb data-slot="switch-thumb" className={cn( "pointer-events-none block rounded-full bg-background shadow-[0_1px_2px_0_rgb(0_0_0/0.2)] ring-0 transition-transform duration-200 ease-[cubic-bezier(0.2,0.8,0.2,1)] motion-reduce:transition-none data-unchecked:translate-x-px data-checked:translate-x-[calc(100%+1px)] rtl:data-unchecked:-translate-x-px rtl:data-checked:-translate-x-[calc(100%+1px)] dark:data-unchecked:bg-foreground dark:data-checked:bg-primary-foreground", "size-[calc(var(--bm-switch-h,calc(var(--spacing)*5))_-_var(--spacing))] group-data-[size=sm]/switch:size-3 group-data-[size=default]/switch:data-checked:translate-x-[calc(var(--bm-switch-w,calc(var(--spacing)*9))_-_var(--bm-switch-h,calc(var(--spacing)*5))_+_1px)] group-data-[size=default]/switch:rtl:data-checked:-translate-x-[calc(var(--bm-switch-w,calc(var(--spacing)*9))_-_var(--bm-switch-h,calc(var(--spacing)*5))_+_1px)]" )} /> </SwitchPrimitive.Root> ) } export { Switch, switchVariants, type SwitchProps }Update the import paths to match your project setup.
Usage
import { Switch } from "@/components/ballmac/switch"The full example is in the Code tab above.
Examples
Settings rows
A summary of activity every Monday.
Notify me when someone @mentions me.
Occasional emails about new features.
import { Label } from "@/components/ballmac/label"
import { Switch } from "@/components/ballmac/switch"
const settings = [
{ id: "email-digest", label: "Weekly digest", description: "A summary of activity every Monday.", on: true },
{ id: "mentions", label: "Mentions", description: "Notify me when someone @mentions me.", on: true },
{ id: "marketing", label: "Product news", description: "Occasional emails about new features.", on: false },
]
export default function SwitchSettings() {
return (
<div className="w-full max-w-md divide-y rounded-xl border bg-card">
{settings.map((s) => (
<div key={s.id} className="flex items-center justify-between gap-4 p-4">
<div className="grid gap-1">
<Label htmlFor={s.id}>{s.label}</Label>
<p id={`${s.id}-description`} className="text-sm text-muted-foreground">
{s.description}
</p>
</div>
<Switch id={s.id} defaultChecked={s.on} aria-describedby={`${s.id}-description`} />
</div>
))}
</div>
)
}API reference
| Prop | Type | Default |
|---|---|---|
size | "sm" | "default" | "default" |
Also accepts the standard attributes of its root element.
Accessibility
| Key | Action |
|---|---|
| Space | Toggles the switch |
| Enter | Toggles the switch |
| Tab | Moves focus to the switch |
Quality scorecard
Every applicable check passes
Measured in Chromium 153.0.8010.12 on October 19, 2026, on version 1.0.1, across 2 examples.
Passed: Accessibility scan
No serious or critical issues in light and dark, on a phone and a desktop (axe-core 4.13.0, WCAG 2.1 AA and best-practice rules).
Passed: Small screens
No sideways scrolling at 360, 390 or 1280 px wide.
Passed: Reduced motion
No endless animation to begin with.
Passed: Right to left
Mirrors correctly in Arabic, Hebrew, Persian and Urdu layouts (0% of elements off the mirror, limit 15%).
Passed: Clean console
No console errors or React hydration errors while loading.
Passed: Keyboard
5 tab stops, each showing a visible change when it takes focus. 3 documented keys.
Not applicable: Loading, empty and error
Does not show data or take submissions.
Passed: Automated tests
Covered by switch.test.tsx.
For information: Footprint
1.0 KB of source (gzip) in 1 file. Installs radix-ui, class-variance-authority.
Automated checks find a subset of problems and do not replace testing with a screen reader and a keyboard in your own app. What they miss.
Use with AI
On/off control for settings that apply immediately. Pair with a Label via id/htmlFor; use size="sm" in dense lists. With the shadcn MCP server set up (guide), ask your agent:
Add the Ballmac UI Switch (@ballmac/switch) to this project with the shadcn MCP, then use it where it fits.
Use it for
- Enabling or disabling a feature or notification
- Settings pages with labelled rows
- Toggles inside dense menus or tables (size sm)
Not for
- Choices submitted later with a form, or agreeing to terms (use checkbox)
- More than two options (use select)
Registry JSON: https://ui.ballmac.com/r/switch.json
Credits
Based on shadcn/ui Switch, adapted by Ballmac. Free to use in personal and commercial projects.
- Registry
- shadcn/utils
Pairs well with
Label
An accessible form label built on Radix Label that dims itself when the paired peer control is disabled and never selects text on double-click.
Badge
A small pill label in four variants with an optional status dot and success, warning and error tones. Style links with badgeVariants().
Accordion
Vertically stacked disclosure sections on Radix Accordion, single or multiple open, with hairline dividers, a rotating plus and height animation.
Avatar
A Radix avatar with image and initials fallback, three sizes, an optional presence dot, and AvatarGroup for overlapping stacks with a +N overflow counter.
Ballmac UI Pro
Need the whole screen, not one piece?
311 premium blocks (dashboards, settings, calendars, mail, shops, onboarding), a private shadcn registry and Figma tokens. Blocks that show data come with loading, empty and error states built in.
Not ready? Try three Pro blocks free
Leave your email and the blocks appear right here, ready to copy. Yours to use in any project.