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

Checkbox

FreeNew

A checkbox with a drawn check and an indeterminate dash.

Installation

pnpm dlx shadcn@latest add @viewport/checkbox

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

Usage

The code behind the preview above.

checkbox-demo.tsx
"use client";

import { useState } from "react";
import { Checkbox } from "@/components/viewport/checkbox";

const initial = [
  { id: "tokens", label: "Audit color tokens for contrast", done: true },
  { id: "motion", label: "Add reduced-motion fallbacks", done: true },
  { id: "keys", label: "Test every flow with a keyboard", done: false },
  { id: "ship", label: "Ship the release notes", done: false },
];

export default function CheckboxDemo() {
  const [tasks, setTasks] = useState(initial);
  const done = tasks.filter((t) => t.done).length;
  const all = done === tasks.length ? true : done === 0 ? false : "indeterminate";

  return (
    <div className="w-full max-w-sm rounded-2xl border border-border bg-card">
      <div className="flex items-center gap-3 border-b border-border px-4 py-3">
        <Checkbox id="all" checked={all} onCheckedChange={(v) => setTasks((ts) => ts.map((t) => ({ ...t, done: v === true })))} />
        <label htmlFor="all" className="flex-1 cursor-pointer text-sm font-medium">
          Launch checklist
        </label>
        <span className="text-xs tabular-nums text-muted-foreground">
          {done}/{tasks.length}
        </span>
      </div>
      <ul className="p-1.5">
        {tasks.map((task) => (
          <li key={task.id} className="flex items-center gap-3 rounded-lg px-2.5 py-2 transition-colors hover:bg-muted/60">
            <Checkbox
              id={task.id}
              checked={task.done}
              onCheckedChange={(v) => setTasks((ts) => ts.map((t) => (t.id === task.id ? { ...t, done: v === true } : t)))}
            />
            <label htmlFor={task.id} className="flex-1 cursor-pointer text-sm">
              {/* The strike is a background on the text itself, so it is exactly as long as the label and wraps with it. */}
              <span
                data-done={task.done || undefined}
                className="bg-[linear-gradient(currentColor,currentColor)] bg-[length:0%_1px] bg-[position:0_55%] bg-no-repeat transition-[background-size,color] duration-300 ease-out data-[done]:bg-[length:100%_1px] data-[done]:text-muted-foreground"
              >
                {task.label}
              </span>
            </label>
          </li>
        ))}
      </ul>
    </div>
  );
}

Accessibility

  • Built on Radix Checkbox with role=checkbox and a mixed state.
  • Space toggles it; works with a label and inside forms.
  • Pure CSS transitions, removed under reduced motion.