Cnippet

Switch

Cards 1

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 { BellOffIcon, MoonIcon } from "lucide-react";
import { useState } from "react";
import { Switch } from "@/components/ui/cnippet-switch";

const scheduleOptions = ["1 hour", "2 hours", "4 hours", "Until tomorrow"];

export default function SwitchDoNotDisturb() {
  const [dnd, setDnd] = useState(false);
  const [selected, setSelected] = useState("1 hour");

  return (
    <div className="w-full max-w-sm space-y-4 rounded-xl border p-5">
      <div className="flex items-center justify-between">
        <div className="flex items-center gap-2">
          <div
            className={`flex size-9 items-center justify-center rounded-lg transition-colors ${dnd ? "bg-primary/10 text-primary" : "bg-muted text-muted-foreground"}`}
          >
            {dnd ? (
              <BellOffIcon aria-hidden="true" className="size-4" />
            ) : (
              <MoonIcon aria-hidden="true" className="size-4" />
            )}
          </div>
          <div>
            <p className="font-medium text-sm">Do Not Disturb</p>
            <p className="text-muted-foreground text-xs">
              {dnd ? `Active for ${selected}` : "All notifications enabled"}
            </p>
          </div>
        </div>
        <Switch checked={dnd} onCheckedChange={setDnd} />
      </div>

      {dnd && (
        <div className="space-y-2">
          <p className="font-medium text-muted-foreground text-xs uppercase tracking-wide">
            Duration
          </p>
          <div className="grid grid-cols-2 gap-2">
            {scheduleOptions.map((opt) => (
              <button
                className={`rounded-lg border px-3 py-2 font-medium text-xs transition-colors ${
                  selected === opt
                    ? "border-primary bg-primary/5 text-primary"
                    : "hover:bg-muted"
                }`}
                key={opt}
                onClick={() => setSelected(opt)}
                type="button"
              >
                {opt}
              </button>
            ))}
          </div>
        </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

    558
  • 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

More in Cards

View all
  • Statistics Card 2

    Statistics Card 2

    264
  • Course Design Cards

    Course Design Cards

    1.1K
  • Card

    Card

    70
  • Stats cards with links

    Stats cards with links

    39
  • The Item one

    The Item one

    3
  • Card Studio

    Card Studio

    7
  • Statistics Card 7

    Statistics Card 7

    292
  • Item

    Item

    10