Skip to content
← Components

Switch

Switch with thoughtful defaults, semantic styling, and editable source.

Open ↗

Make it yours.

Install the editable source and its dependencies into your React + Tailwind project.

Scroll horizontally for long lines
pnpm dlx shadcn@4.0.8 add http://localhost:3000/r/switch.json

Use it

Scroll horizontally for long lines
"use client";
import { Switch } from "@/components/jez-ui/ui/switch";

export default function Example() {
  return (
    <>
      <div className="grid w-full max-w-sm gap-6">
        <div>
          <h3 className="text-2xl">Keep your focus.</h3>
          <p className="mt-2 text-sm text-muted-foreground">
            A quieter workspace, on your terms.
          </p>
        </div>
        {[
          {
            name: "Focus mode",
            hint: "Make a little room to think.",
            on: true,
          },
          {
            name: "Project updates",
            hint: "Only the changes that matter.",
            on: true,
          },
          {
            name: "Weekly roundup",
            hint: "The whole picture, once a week.",
            on: false,
          },
        ].map((o) => (
          <label
            key={o.name}
            className="flex items-center justify-between gap-6 border-b border-border pb-5"
          >
            <div>
              <span className="text-sm font-medium">{o.name}</span>
              <p className="mt-1 text-xs text-muted-foreground">{o.hint}</p>
            </div>
            <Switch defaultChecked={o.on} />
          </label>
        ))}
      </div>
    </>
  );
}

Props & types

Scroll horizontally for long lines
React.ComponentProps<typeof P.Root>

Source

View 2 source files

registry/ui/switch.tsx

Scroll horizontally for long lines
"use client";
import * as React from "react";
import { Switch as P } from "radix-ui";
import { cn } from "./utils";
export function Switch({
  className,
  ...props
}: React.ComponentProps<typeof P.Root>) {
  return (
    <P.Root
      className={cn(
        "relative inline-flex h-6 w-11 shrink-0 items-center rounded-full border border-border bg-muted p-[2px] align-middle transition-colors data-[state=checked]:border-primary data-[state=checked]:bg-primary disabled:opacity-50",
        className,
      )}
      {...props}
    >
      <P.Thumb className="pointer-events-none block size-[18px] rounded-full bg-white shadow-sm transition-transform duration-200 ease-out data-[state=checked]:translate-x-5" />
    </P.Root>
  );
}

registry/ui/utils.ts

Scroll horizontally for long lines
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...values: ClassValue[]) {
  return twMerge(clsx(values));
}

Dependencies

radix-ui@1.6.7 · clsx@2.1.1 · tailwind-merge@3.5.0

Accessibility & behaviour

Provide meaningful labels and preserve keyboard focus styles. Check contrast when customising theme colours.

Read the accessibility and performance guide →