Switch
Switch example: an on/off toggle with a pill track and sliding thumb, in default and small sizes. Shows focus rings and invalid styling, and inverts its thumb colors in dark mode.
pnpm dlx shadcn add @01orbital/switch"use client"
import { Switch as SwitchPrimitive } from "@base-ui/react/switch"
import { cn } from "cn"
function Switch({
className,
size = "default",
...props
}: SwitchPrimitive.Root.Props & {
size?: "sm" | "default"
}) {
return (
<SwitchPrimitive.Root
data-slot="switch"
data-size={size}
className={cn(
"peer group/switch relative inline-flex shrink-0 items-center rounded-full border-2 transition-all outline-none group-has-[:focus-visible]/field-label:ring-0 after:absolute after:-inset-x-3 after:-inset-y-2 focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/30 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 data-[size=default]:h-5 data-[size=default]:w-11 data-[size=sm]:h-4 data-[size=sm]:w-7 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 data-checked:border-primary data-checked:bg-primary group-has-[:focus-visible]/field-label:data-checked:border-primary data-unchecked:border-transparent data-unchecked:bg-input/90 group-has-[:focus-visible]/field-label:data-unchecked:border-transparent data-disabled:cursor-not-allowed data-disabled:opacity-50",
className
)}
{...props}
>
<SwitchPrimitive.Thumb
data-slot="switch-thumb"
className="pointer-events-none block rounded-full bg-background shadow-sm ring-0 transition-transform not-dark:bg-clip-padding group-data-[size=default]/switch:h-4 group-data-[size=default]/switch:w-6 group-data-[size=sm]/switch:h-3 group-data-[size=sm]/switch:w-4 data-checked:translate-x-[calc(100%-8px)] dark:data-checked:bg-primary-foreground data-unchecked:translate-x-0 dark:data-unchecked:bg-foreground"
/>
</SwitchPrimitive.Root>
)
}
export { Switch }
"use client"
import { Label } from "@/components/ui/label"
import { Switch } from "@/components/ui/switch"
// The copy: change text here, not in the markup.
const COPY = {
label: "Film grain",
} as const
/** Switch, on, with its Label. */
export default function SwitchDemo() {
return (
<div className="flex items-center gap-3">
<Switch id="grain" defaultChecked />
<Label htmlFor="grain">{COPY.label}</Label>
</div>
)
}
Examples
New
switch-cloud-dancer
New
switch-appointment-reminder
New
switch-shipping-hold
New
switch-bedtime-hide-tasks
New
switch-hide-balances
Dependencies
npm: @base-ui/react, cn. The CLI installs them for you.
No other registry pieces.