Skip to content
ProLifetime access for $199, paid once, every future release included
Truffle UI

Switch

Free

A two-position toggle with a sliding thumb and inset track.

Installation

pnpm dlx shadcn@latest add @viewport/switch

First time? Add the @viewport registry to components.json. Setup takes one minute.

Usage

The code behind the preview above.

switch-demo.tsx
"use client";

import { useId } from "react";
import { Switch } from "@/components/viewport/switch";

const settings = [
  { id: "updates", label: "Product updates", hint: "New components, about twice a month.", on: true },
  { id: "tips", label: "Tips and tutorials", hint: "Occasional guides on motion and accessibility.", on: false },
];

export default function SwitchDemo() {
  const instance = useId();
  return (
    <div className="w-full max-w-sm divide-y divide-border rounded-2xl border border-border bg-card">
      {settings.map(({ id, label, hint, on }) => (
        <div key={id} className="flex items-center justify-between gap-6 p-4">
          <label htmlFor={`${instance}-${id}`} className="cursor-pointer">
            <span className="block text-sm font-medium">{label}</span>
            <span className="block text-xs text-muted-foreground">{hint}</span>
          </label>
          <Switch id={`${instance}-${id}`} defaultChecked={on} />
        </div>
      ))}
    </div>
  );
}

Accessibility

  • Built on Radix Switch with role=switch.
  • Space and Enter toggle it; works with a label.
  • Respects prefers-reduced-motion.