Skip to Content
Zuri
Components

Switch

A native checkbox exposed as an on/off switch.

Live preview
Theme
Preview width

Use Switch for a setting that takes effect immediately, such as turning notifications on. If the choice is submitted later with a form, a Checkbox is the better fit.

Installation

pnpm dlx shadcn@latest add https://components.abdulqadirmsingi.com/r/switch.json

Usage

import { Switch } from "@/components/ui/switch";
import { Label } from "@/components/ui/label";

<Switch id="notify" name="notify" defaultChecked />
<Label htmlFor="notify">Payment notifications</Label>

States

Live preview
Theme
Preview width

Props

Switch extends all native <input> props (except type and role) and spreads them onto the input, including ref (React 19). Use checked with onChange, or defaultChecked.

Props
PropTypeDefaultDescription
classNamestring–Merged onto the input; yours win on conflicts.

Accessibility

  • It is a native checkbox with role="switch": Tab focuses it, Space toggles it, and screen readers announce it as a switch that is on or off.
  • The track is 44 × 24 pixels.
  • The thumb moves and changes colour, so state is not shown by colour alone. In right-to-left layouts the thumb moves the other way.
  • Under prefers-reduced-motion, the thumb jumps with no transition.
  • The label should describe the setting, not the state ("Notifications", not "Turn notifications on").

Tokens used

--muted, --muted-foreground, --control-border, --primary, --primary-foreground, --ring, --duration-base, --ease-out.

Known limitations

  • No loading or pending state, so for slow saves disable the switch while you wait.
  • Windows forced-colours mode has not been checked.

Source

import type { ComponentProps } from "react";import { cn } from "@/lib/utils";export type SwitchProps = Omit<ComponentProps<"input">, "type" | "size" | "role">;/** * A native checkbox exposed as `role="switch"`. Use it for settings that apply immediately; * use a Checkbox when the choice is submitted later with a form. */export function Switch({ className, ...props }: SwitchProps) {  return (    <span className="relative inline-flex h-6 w-11 shrink-0">      <input        type="checkbox"        // biome-ignore lint/a11y/useAriaPropsForRole: a native checkbox exposes its checked state itself; aria-checked would duplicate it.        role="switch"        className={cn(          "peer h-6 w-11 cursor-pointer appearance-none rounded-full border border-control-border bg-muted transition-[background-color,border-color] duration-[var(--duration-base)] ease-[var(--ease-out)] checked:border-primary checked:bg-primary focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring disabled:cursor-not-allowed disabled:opacity-50 motion-reduce:transition-none",          className,        )}        {...props}      />      <span        aria-hidden="true"        className="pointer-events-none absolute start-0.5 top-0.5 size-5 rounded-full bg-muted-foreground transition-[transform,translate,scale,rotate,background-color] duration-[var(--duration-base)] ease-[var(--ease-out)] peer-checked:translate-x-5 peer-checked:bg-primary-foreground rtl:peer-checked:-translate-x-5 motion-reduce:transition-none"      />    </span>  );}

On this page