Cnippet

Switch

Component

About

An accessible toggle switch built on Base UI's Switch primitive. Animated thumb with a springy press/scale interaction, two sizes (default and sm), controlled or uncontrolled usage via checked / defaultChecked, disabled state, and easy accent recoloring through the data-checked:* utilities. Re-exports SwitchPrimitive for advanced composition.

Built with React and Tailwind. Copy the prompt and paste it into Claude Code, Cursor or Lovable: your agent adds Switch to your project and fits it to your theme.

Code

How Switch is used: the demo's code. The component's own source comes with a free account.

Usage.tsx

"use client";

import { MoonIcon, SunIcon } from "lucide-react";
import { useState } from "react";
import { Switch } from "@/components/ui/cnippet-switch";

export default function SwitchThemeToggle() {
  const [dark, setDark] = useState(false);

  return (
    <div
      className={`flex w-full max-w-xs flex-col items-center gap-6 rounded-2xl border p-8 transition-colors duration-300 ${dark ? "border-zinc-700 bg-zinc-900 text-white" : "bg-white text-zinc-900"}`}
    >
      <div
        className={`flex size-16 items-center justify-center rounded-full transition-colors duration-300 ${dark ? "bg-zinc-800" : "bg-amber-50"}`}
      >
        {dark ? (
          <MoonIcon aria-hidden="true" className="size-7 text-indigo-400" />
        ) : (
          <SunIcon aria-hidden="true" className="size-7 text-amber-500" />
        )}
      </div>
      <div className="text-center">
        <p className="font-semibold">{dark ? "Dark Mode" : "Light Mode"}</p>
        <p
          className={`text-sm ${dark ? "text-zinc-400" : "text-zinc-500"} mt-1`}
        >
          {dark ? "Easy on the eyes at night" : "Crisp and clear in daylight"}
        </p>
      </div>
      <div className="flex items-center gap-3">
        <SunIcon
          aria-hidden="true"
          className={`size-4 ${dark ? "text-zinc-500" : "text-amber-500"}`}
        />
        <Switch
          checked={dark}
          className="data-checked:bg-indigo-600"
          onCheckedChange={setDark}
        />
        <MoonIcon
          aria-hidden="true"
          className={`size-4 ${dark ? "text-indigo-400" : "text-zinc-400"}`}
        />
      </div>
    </div>
  );
}

Dependencies

More by Cnippet

View all
  • Team

    Team

    1.3K
  • Davincho Hero-1

    Davincho Hero-1

    683
  • Aero Hero-3

    Aero Hero-3

    559
  • Features

    Features

    510
  • Random Letter Swap Navigation

    Random Letter Swap Navigation

    487
  • Services with Animated Hover Modal

    Services with Animated Hover Modal

    465
  • Blogs

    Blogs

    396
  • Aero Hero-1

    Aero Hero-1

    356