Skip to content
Corsair UI
Browse the docs

ComponentsNo. 55 of 109

Switch

On/off control for settings that apply immediately.

Installation

pnpm dlx shadcn@latest add @corsair-ui/switch

No setup needed: the CLI finds @corsair-ui in the shadcn registry directory. The files land in your project as source: edit them like your own code. More in Installation.

Also adds
utils
npm
@radix-ui/react-switchclsxtailwind-merge

Examples

Default

Settings list

API

Built on Radix Switch; every prop it takes is passed through.

Accessibility

  • Announced as a switch with its on or off state; Space toggles it.
  • For settings that apply at once; inside a submitted form, prefer a checkbox.

Compatibility

Tailwind CSS
3.4 and 4, both checked in CI
React
19
Rendering
Client component ("use client")

Source

1 file, installed as source you own
switch.tsxView on GitHub
"use client";

import * as SwitchPrimitive from "@radix-ui/react-switch";
import type { ComponentProps } from "react";

import { cn } from "@/registry/default/lib/utils";

/** For settings that apply immediately. Inside a form that is submitted, prefer a Checkbox. */
function Switch({ className, ...props }: ComponentProps<typeof SwitchPrimitive.Root>) {
  return (
    <SwitchPrimitive.Root
      data-slot="switch"
      className={cn(
        "peer inline-flex h-[1.15rem] w-8 shrink-0 cursor-pointer items-center rounded-full border border-transparent",
        "transition-[color,background-color,box-shadow] outline-none motion-reduce:transition-none",
        "focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px]",
        "data-[state=checked]:bg-primary data-[state=unchecked]:bg-input",
        "disabled:cursor-not-allowed disabled:opacity-50",
        className
      )}
      {...props}
    >
      <SwitchPrimitive.Thumb
        data-slot="switch-thumb"
        className={cn(
          "bg-background pointer-events-none block size-4 rounded-full ring-0",
          // `translate` is what Tailwind 4 moves; the colour changes with it in dark mode.
          "transition-[transform,translate,background-color] duration-200 ease-out motion-reduce:transition-none",
          "data-[state=checked]:translate-x-[calc(100%-2px)] data-[state=unchecked]:translate-x-0",
          "dark:data-[state=checked]:bg-primary-foreground dark:data-[state=unchecked]:bg-foreground"
        )}
      />
    </SwitchPrimitive.Root>
  );
}

export { Switch };