Checkbox
An animated React checkbox built on shadcn/ui and Radix: the drop-in Checkbox draws its check mark with an SVG stroke and supports indeterminate — plus Motion blocks for a to-do item that strikes its label through, and selectable checkbox cards that trace their outline.
- Drop-in shadcn API
- Radix Checkbox
- SVG pathLength
- Indeterminate
- prefers-reduced-motion
Due today · Marketing
Tomorrow
Friday
import { TaskCheckbox } from "@/components/dusk/checkbox/task-checkbox";
export default function TaskCheckboxDemo() {
return (
<div className="grid w-full max-w-sm gap-4 rounded-xl border bg-card p-5">
<TaskCheckbox label="Write the launch announcement" description="Due today · Marketing" defaultChecked />
<TaskCheckbox label="Record a 30-second demo video of the new animated components" description="Tomorrow" />
<TaskCheckbox label="Ship v1.0" description="Friday" />
</div>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/checkbox.json1. Install the dependencies.
npm install @radix-ui/react-checkbox2. Copy the source into
components/ui/checkbox.tsx.checkbox.tsx "use client"; import * as React from "react"; import * as CheckboxPrimitive from "@radix-ui/react-checkbox"; import { cn } from "@/lib/utils"; /** * DuskUI Checkbox — a drop-in upgrade of the shadcn/ui Checkbox. * * Same API and data-slot attributes as shadcn plus: * - The check mark draws itself (SVG `pathLength` + stroke-dashoffset) and * erases faster than it draws. * - `checked="indeterminate"` draws a dash instead of a check. * - A smooth fill transition. * - Still works uncontrolled, in forms and with form reset (pure CSS, no * extra React state). * - Under `prefers-reduced-motion` the mark appears instantly. * Powered by Radix for keyboard support, form submission and aria. */ function Checkbox({ className, ...props }: React.ComponentProps<typeof CheckboxPrimitive.Root>) { return ( <CheckboxPrimitive.Root data-slot="checkbox" className={cn( "peer group/checkbox relative grid size-4 shrink-0 place-items-center rounded-[4px] border border-input bg-input/30 shadow-xs outline-none", "transition-[background-color,border-color,box-shadow] duration-150 ease-out motion-reduce:transition-none", "focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50", "aria-invalid:border-destructive aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40", "disabled:cursor-not-allowed disabled:opacity-50", "data-[state=checked]:border-primary data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground", "data-[state=indeterminate]:border-primary data-[state=indeterminate]:bg-primary data-[state=indeterminate]:text-primary-foreground", className )} {...props} > {/* forceMount keeps the SVG in the DOM so the mark can animate out as well as in. */} <CheckboxPrimitive.Indicator forceMount data-slot="checkbox-indicator" className="pointer-events-none grid place-content-center text-current" > <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth={2.25} strokeLinecap="round" strokeLinejoin="round" className="size-3.5" aria-hidden > <path d="M3.5 8.5l3 3 6-7" pathLength={1} className={cn( "opacity-0 [stroke-dasharray:1] [stroke-dashoffset:1]", "transition-[stroke-dashoffset,opacity] duration-100 ease-in", "group-data-[state=checked]/checkbox:opacity-100 group-data-[state=checked]/checkbox:[stroke-dashoffset:0]", "group-data-[state=checked]/checkbox:delay-75 group-data-[state=checked]/checkbox:duration-250 group-data-[state=checked]/checkbox:ease-out", "motion-reduce:transition-none" )} /> <path d="M4 8h8" pathLength={1} className={cn( "opacity-0 [stroke-dasharray:1] [stroke-dashoffset:1]", "transition-[stroke-dashoffset,opacity] duration-100 ease-in", "group-data-[state=indeterminate]/checkbox:opacity-100 group-data-[state=indeterminate]/checkbox:[stroke-dashoffset:0]", "group-data-[state=indeterminate]/checkbox:delay-75 group-data-[state=indeterminate]/checkbox:duration-200 group-data-[state=indeterminate]/checkbox:ease-out", "motion-reduce:transition-none" )} /> </svg> </CheckboxPrimitive.Indicator> </CheckboxPrimitive.Root> ); } export { Checkbox };
Replaces components/ui/checkbox.tsx with the same API. The check animation is pure CSS (stroke-dashoffset on a pathLength path), so uncontrolled checkboxes and form resets keep working.
Usage
import { Checkbox } from "@/components/ui/checkbox"
export function Terms() {
return (
<div className="flex items-center gap-2">
<Checkbox id="terms" />
<label htmlFor="terms">Accept terms and conditions</label>
</div>
)
}Examples
Default
The check draws itself when ticked and erases faster when cleared. Link hints with aria-describedby.
One email a month. Unsubscribe anytime.
"use client";
import * as React from "react";
import { Checkbox } from "@/components/ui/checkbox";
export default function CheckboxBasicDemo() {
const id = React.useId();
return (
<div className="grid gap-4">
<div className="flex items-center gap-2.5">
<Checkbox id={`${id}-terms`} defaultChecked />
<label htmlFor={`${id}-terms`} className="text-sm font-medium">
Accept terms and conditions
</label>
</div>
<div className="flex items-start gap-2.5">
<Checkbox id={`${id}-updates`} className="mt-0.5" aria-describedby={`${id}-updates-hint`} />
<div className="grid gap-1">
<label htmlFor={`${id}-updates`} className="text-sm font-medium">
Product updates
</label>
<p id={`${id}-updates-hint`} className="text-xs text-muted-foreground">
One email a month. Unsubscribe anytime.
</p>
</div>
</div>
<div className="flex items-center gap-2.5">
<Checkbox id={`${id}-disabled`} disabled defaultChecked />
<label htmlFor={`${id}-disabled`} className="text-sm font-medium opacity-50">
Disabled
</label>
</div>
</div>
);
}Indeterminate
Pass checked="indeterminate" for a partial selection; a dash draws in instead of the check. A classic “select all” for lists and tables.
"use client";
import * as React from "react";
import { Checkbox } from "@/components/ui/checkbox";
const files = ["hero.png", "logo.svg", "og-image.jpg"];
export default function CheckboxIndeterminateDemo() {
const id = React.useId();
const [selected, setSelected] = React.useState<string[]>(["logo.svg"]);
const all = selected.length === files.length;
const some = selected.length > 0 && !all;
return (
<div className="w-full max-w-xs rounded-xl border bg-card p-4">
<div className="flex items-center gap-2.5 border-b pb-3">
<Checkbox
id={`${id}-all`}
checked={all ? true : some ? "indeterminate" : false}
onCheckedChange={(checked) => setSelected(checked === true ? files : [])}
/>
<label htmlFor={`${id}-all`} className="text-sm font-medium">
Select all
</label>
<span className="ms-auto text-xs text-muted-foreground tabular-nums" aria-live="polite">
{selected.length}/{files.length}
</span>
</div>
<ul className="mt-3 grid gap-2.5">
{files.map((file) => (
<li key={file} className="flex items-center gap-2.5">
<Checkbox
id={`${id}-${file}`}
checked={selected.includes(file)}
onCheckedChange={(checked) =>
setSelected((prev) => (checked === true ? [...prev, file] : prev.filter((f) => f !== file)))
}
/>
<label htmlFor={`${id}-${file}`} className="font-mono text-xs">
{file}
</label>
</li>
))}
</ul>
</div>
);
}Blocks
Ready-made variations composed from the base component with Motion. Each installs on its own and pulls in its dependencies automatically.
Task Checkbox
A to-do row. Checking it draws the check, strikes the label through from left to right — across every wrapped line, one after another — and dims the text. Set celebrate for a small ring and particle burst around the box. The whole label is the click target.
Due today · Marketing
Tomorrow
Friday
import { TaskCheckbox } from "@/components/dusk/checkbox/task-checkbox";
export default function TaskCheckboxDemo() {
return (
<div className="grid w-full max-w-sm gap-4 rounded-xl border bg-card p-5">
<TaskCheckbox label="Write the launch announcement" description="Due today · Marketing" defaultChecked />
<TaskCheckbox label="Record a 30-second demo video of the new animated components" description="Tomorrow" />
<TaskCheckbox label="Ship v1.0" description="Friday" />
</div>
);
}$ npx shadcn@latest add https://duskui.com/r/task-checkbox.json1. Install the dependencies.
npm install motion2. Copy the source into
components/dusk/checkbox/task-checkbox.tsx.task-checkbox.tsx "use client"; import * as React from "react"; import { AnimatePresence, motion, useReducedMotion } from "motion/react"; import { Checkbox } from "@/components/ui/checkbox"; import { cn } from "@/lib/utils"; type CheckedState = boolean | "indeterminate"; export interface TaskCheckboxProps extends Omit<React.ComponentProps<typeof Checkbox>, "children" | "checked" | "defaultChecked"> { checked?: CheckedState; defaultChecked?: CheckedState; /** The task text. A line draws through it when checked, across every wrapped line. */ label: React.ReactNode; /** Optional secondary line (due date, project…). */ description?: React.ReactNode; /** Small ring and particle burst around the box when checked. Off by default. */ celebrate?: boolean; /** Classes for the outer row. */ containerClassName?: string; } // Six particles around a circle, precomputed so SSR and client markup match. const particles = [ [0, -1], [0.866, -0.5], [0.866, 0.5], [0, 1], [-0.866, 0.5], [-0.866, -0.5], ] as const; /** * A to-do row: checking it draws the check, strikes the label through from * left to right (wrapping onto every line) and dims the text; `celebrate` * adds a small ring + particle burst. The whole label is clickable. * Built on the DuskUI Checkbox (Radix), so it works in forms and with the keyboard. */ export function TaskCheckbox({ checked: checkedProp, defaultChecked = false, onCheckedChange, label, description, celebrate = false, id, className, containerClassName, ...props }: TaskCheckboxProps) { const autoId = React.useId(); const checkboxId = id ?? autoId; const descriptionId = `${checkboxId}-description`; const reduceMotion = useReducedMotion(); const [uncontrolled, setUncontrolled] = React.useState<CheckedState>(defaultChecked); // Bumped on every check so the burst replays. const [burst, setBurst] = React.useState(0); const checked = checkedProp ?? uncontrolled; const done = checked === true; return ( <div data-slot="task-checkbox" data-done={done || undefined} className={cn("group/task flex items-start gap-3", containerClassName)} > <span className="relative mt-0.5 grid"> <Checkbox id={checkboxId} checked={checked} aria-describedby={description ? descriptionId : undefined} onCheckedChange={(next) => { if (checkedProp === undefined) setUncontrolled(next); if (next === true) setBurst((b) => b + 1); onCheckedChange?.(next); }} className={cn("size-[1.125rem] rounded-[5px]", className)} {...props} /> {celebrate && !reduceMotion && ( <AnimatePresence> {done && burst > 0 && ( <motion.span key={burst} aria-hidden className="pointer-events-none absolute inset-0" exit={{ opacity: 0, transition: { duration: 0.1 } }} > <motion.span className="absolute inset-0 rounded-[5px] border border-primary" initial={{ scale: 1, opacity: 0.5 }} animate={{ scale: 1.8, opacity: 0 }} transition={{ duration: 0.5, ease: [0.16, 1, 0.3, 1] }} /> {particles.map(([x, y]) => ( <motion.span key={`${x}-${y}`} className="absolute top-1/2 left-1/2 -mt-[2px] -ml-[2px] size-1 rounded-full bg-primary" initial={{ x: 0, y: 0, scale: 1, opacity: 0.7 }} animate={{ x: x * 12, y: y * 12, scale: 0, opacity: 0 }} transition={{ duration: 0.55, ease: [0.16, 1, 0.3, 1] }} /> ))} </motion.span> )} </AnimatePresence> )} </span> <div className="grid min-w-0 gap-0.5"> <label htmlFor={checkboxId} className="cursor-pointer text-sm leading-6 font-medium text-foreground transition-colors duration-300 group-data-done/task:text-muted-foreground" > {/* An inline background line: it draws across wrapped lines one after another. */} <span className={cn( "bg-[linear-gradient(currentColor,currentColor)] bg-[length:0%_1.5px] bg-[position:0_58%] bg-no-repeat", "transition-[background-size] duration-300 ease-[cubic-bezier(0.65,0,0.35,1)] motion-reduce:transition-none", "group-data-done/task:bg-[length:100%_1.5px]" )} > {label} </span> </label> {description && ( <p id={descriptionId} className="text-xs text-muted-foreground transition-opacity duration-300 group-data-done/task:opacity-80" > {description} </p> )} </div> </div> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| label | ReactNode | — | The task text. Required. |
| description | ReactNode | — | Secondary line, linked with aria-describedby. |
| checked / onCheckedChange | boolean | "indeterminate" | — | Controlled state. |
| defaultChecked | boolean | "indeterminate" | false | Uncontrolled initial state. |
| celebrate | boolean | false | Small ring + particle burst when checked. |
| containerClassName | string | — | Classes for the row; className styles the checkbox. |
Checkbox Card
A selectable card for plans, add-ons and multi-select options. Selecting it traces a mint outline around the card (an SVG rect drawn with pathLength) and draws the check. The card is announced as one checkbox named by its title.
import { BarChart3, Globe, ShieldCheck } from "lucide-react";
import { CheckboxCard } from "@/components/dusk/checkbox/checkbox-card";
export default function CheckboxCardDemo() {
return (
<fieldset className="grid w-full max-w-md gap-3">
<legend className="mb-3 text-sm font-medium">Add-ons</legend>
<CheckboxCard
name="addons"
value="analytics"
defaultChecked
icon={<BarChart3 />}
title="Analytics"
description="Privacy-friendly page views and funnels."
>
<span className="text-xs text-muted-foreground">+$9 / month</span>
</CheckboxCard>
<CheckboxCard
name="addons"
value="domains"
icon={<Globe />}
title="Custom domains"
description="Bring your own domain with automatic TLS."
>
<span className="text-xs text-muted-foreground">+$5 / month</span>
</CheckboxCard>
<CheckboxCard
name="addons"
value="sso"
icon={<ShieldCheck />}
title="Single sign-on"
description="SAML and OIDC for your whole team."
disabled
>
<span className="text-xs text-muted-foreground">Enterprise only</span>
</CheckboxCard>
</fieldset>
);
}$ npx shadcn@latest add https://duskui.com/r/checkbox-card.json1. Install the dependencies.
npm install motion2. Copy the source into
components/dusk/checkbox/checkbox-card.tsx.checkbox-card.tsx "use client"; import * as React from "react"; import { motion, useReducedMotion } from "motion/react"; import { Checkbox } from "@/components/ui/checkbox"; import { cn } from "@/lib/utils"; type CheckedState = boolean | "indeterminate"; export interface CheckboxCardProps extends Omit<React.ComponentProps<typeof Checkbox>, "children" | "title" | "checked" | "defaultChecked"> { checked?: CheckedState; defaultChecked?: CheckedState; title: React.ReactNode; description?: React.ReactNode; /** Leading icon element. */ icon?: React.ReactNode; /** Extra content under the description (price, badges…). */ children?: React.ReactNode; /** Classes for the card (the `<label>`). `className` styles the checkbox. */ containerClassName?: string; } /** * A selectable card for plans, add-ons or multi-select options. Selecting it * traces a mint outline around the card (an SVG rect drawn with `pathLength`) * and draws the check. The whole card is * the click target, while the checkbox inside keeps Radix semantics, so it is * announced as one checkbox named by the title and described by the text. */ export function CheckboxCard({ checked: checkedProp, defaultChecked = false, onCheckedChange, title, description, icon, children, id, disabled, className, containerClassName, ...props }: CheckboxCardProps) { const autoId = React.useId(); const checkboxId = id ?? autoId; const reduceMotion = useReducedMotion(); const [uncontrolled, setUncontrolled] = React.useState<CheckedState>(defaultChecked); const checked = checkedProp ?? uncontrolled; const selected = checked !== false; return ( <motion.label htmlFor={checkboxId} data-slot="checkbox-card" data-state={checked === true ? "checked" : checked === "indeterminate" ? "indeterminate" : "unchecked"} data-disabled={disabled || undefined} whileTap={disabled ? undefined : { scale: 0.985 }} transition={{ type: "spring", bounce: 0, duration: 0.2 }} className={cn( "group/card relative isolate flex cursor-pointer items-start gap-3 rounded-xl border bg-card p-4 select-none", "transition-[background-color,border-color] duration-200 ease-out motion-reduce:transition-none", "hover:border-white/20", "has-focus-visible:ring-[3px] has-focus-visible:ring-ring/50", "data-disabled:cursor-not-allowed data-disabled:opacity-50 data-disabled:hover:border-border", containerClassName )} > {/* The outline that traces itself around the card. Sits on the 1px border. */} <svg aria-hidden className="pointer-events-none absolute -top-[0.5px] -left-[0.5px] size-[calc(100%+1px)] overflow-visible" > <motion.rect width="100%" height="100%" rx="13.5" fill="none" stroke="var(--color-primary)" strokeWidth={1.5} initial={false} animate={{ pathLength: selected ? 1 : 0, opacity: selected ? 1 : 0 }} transition={ reduceMotion ? { duration: 0 } : selected ? { pathLength: { duration: 0.4, ease: [0.65, 0, 0.35, 1] }, opacity: { duration: 0.1 } } : { pathLength: { duration: 0.3, ease: "easeIn" }, opacity: { duration: 0.2, delay: 0.15 } } } /> </svg> {icon && ( <span aria-hidden className={cn( "grid size-9 shrink-0 place-items-center rounded-lg border border-white/10 bg-white/[0.03] text-muted-foreground", "transition-[color,border-color] duration-200 ease-out motion-reduce:transition-none", "group-data-[state=checked]/card:border-white/20 group-data-[state=checked]/card:text-foreground", "[&_svg:not([class*='size-'])]:size-4" )} > {icon} </span> )} <span className="grid min-w-0 flex-1 gap-1"> <span id={`${checkboxId}-title`} className="text-sm leading-5 font-medium"> {title} </span> {description && ( <span id={`${checkboxId}-description`} className="text-sm text-muted-foreground"> {description} </span> )} {children} </span> <Checkbox id={checkboxId} checked={checked} disabled={disabled} aria-labelledby={`${checkboxId}-title`} aria-describedby={description ? `${checkboxId}-description` : undefined} onCheckedChange={(next) => { if (checkedProp === undefined) setUncontrolled(next); onCheckedChange?.(next); }} className={cn("mt-0.5 size-[1.125rem] rounded-full", className)} {...props} /> </motion.label> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| title | ReactNode | — | Card title; becomes the checkbox's accessible name. |
| description | ReactNode | — | Supporting text, linked with aria-describedby. |
| icon | ReactNode | — | Leading icon element. |
| children | ReactNode | — | Extra content under the description (price, badges). |
| checked / defaultChecked / onCheckedChange | boolean | "indeterminate" | — | Same as Checkbox. |
| name / value / required / disabled | Checkbox props | — | Forwarded to the checkbox for forms. |
| containerClassName | string | — | Classes for the card; className styles the checkbox. |
API Reference
Checkbox forwards every Radix Checkbox prop, exactly like shadcn/ui. Style states with data-[state=checked], data-[state=indeterminate] and aria-invalid.
| Prop | Type | Default | Description |
|---|---|---|---|
| checked | boolean | "indeterminate" | — | Controlled state. |
| defaultChecked | boolean | "indeterminate" | false | Uncontrolled initial state. |
| onCheckedChange | (checked: boolean | "indeterminate") => void | — | Called when the user toggles it. |
| name / value / required | string / string / boolean | — | Form submission, via a hidden input inside forms. |
| disabled | boolean | false | Dims the checkbox and blocks interaction. |
Accessibility
- Built on Radix Checkbox: a button with role="checkbox" and aria-checked (including "mixed" for indeterminate), toggled with Space.
- Label every checkbox with
<label htmlFor>; Task Checkbox and Checkbox Card wire labels and descriptions for you. - The animated mark, outline and particles are aria-hidden and never intercept clicks.
- Under prefers-reduced-motion the mark appears instantly, the strike-through doesn't animate and the burst is skipped.