Checklist
A to-do list: tick an item and it's struck through with the pen; untick it and the line pulls back out.
import { Checklist, ChecklistItem } from "@/components/ui/checklist";
export default function ChecklistDemo() {
return (
<Checklist defaultValue={["pens"]} aria-label="Before the launch">
<ChecklistItem value="pens" seed="checklist-pens">
Buy more blue pens
</ChecklistItem>
<ChecklistItem value="docs" seed="checklist-docs">
Write the docs
</ChecklistItem>
<ChecklistItem value="ship" seed="checklist-ship">
Ship it
</ChecklistItem>
</Checklist>
);
}Installation
pnpm dlx shadcn@latest add @ballpoint/ checklist"use client";
import { createContext, useContext, useMemo, useState, type ReactNode } from "react";
import { CheckboxGroup } from "@base-ui/react/checkbox-group";
import { cn } from "@/lib/utils";
import { useInkBox, useInkSeed } from "@/hooks/use-ink-box";
import { inkClassName, InkSvg, Stroke } from "@/lib/ink";
import { lineStroke } from "@/lib/ink-sketch";
import { Checkbox } from "@/components/ui/checkbox";
const DoneContext = createContext<readonly string[]>([]);
/**
* A to-do list: tick an item and it's struck through with the pen, and
* pales; untick it and the line pulls back out. Controlled with `value`, or
* uncontrolled with `defaultValue`; either is the list of done items.
*/
function Checklist({
value,
defaultValue,
onValueChange,
className,
children,
...props
}: Omit<CheckboxGroup.Props, "value" | "defaultValue"> & { value?: string[]; defaultValue?: string[]; children: ReactNode }) {
const [own, setOwn] = useState(defaultValue ?? []);
const done = value ?? own;
return (
<CheckboxGroup
data-slot="checklist"
value={done}
onValueChange={(next, details) => {
onValueChange?.(next, details);
if (value === undefined && !details.isCanceled) setOwn(next);
}}
className={inkClassName("flex flex-col gap-3", className)}
{...props}
>
<DoneContext value={done}>{children}</DoneContext>
</CheckboxGroup>
);
}
/** One thing to do. `value` is what the list reports when it's done. */
function ChecklistItem({ value, disabled, seed, className, children }: { value: string; disabled?: boolean; seed?: string | number; className?: string; children: ReactNode }) {
const done = useContext(DoneContext).includes(value);
return (
<label
data-slot="checklist-item"
data-done={done ? "" : undefined}
className={cn("flex w-fit cursor-pointer items-start gap-3 text-base has-data-disabled:cursor-not-allowed has-data-disabled:opacity-50", className)}
>
<Checkbox value={value} disabled={disabled} seed={seed} className="mt-1" />
<Struck done={done} seed={seed}>
{children}
</Struck>
</label>
);
}
/** The words, and the line drawn through them while the item's done. */
function Struck({ done, seed, children }: { done: boolean; seed?: string | number; children: ReactNode }) {
const s = useInkSeed(seed === undefined ? undefined : `${seed}-strike`);
const [ref, [w, h]] = useInkBox([140, 29]);
const strike = useMemo(() => lineStroke(s, [-3, h * 0.6], [w + 4, h * 0.5], { bow: 0.8, jitter: 0.5, overshoot: 2 }), [s, w, h]);
return (
<span data-checked={done ? "" : undefined} className="relative transition-colors duration-(--dur-state) data-checked:text-ink-3">
<InkSvg ref={ref} box={[-6, 0, w + 12, h]} stretch className="inset-y-0 -left-1.5 h-full w-[calc(100%+12px)] text-ink">
<Stroke d={strike} draw="checked" duration={Math.min(520, 200 + w * 1.4)} width={1.6} />
</InkSvg>
{children}
</span>
);
}
export { Checklist, ChecklistItem };Usage
import { Checklist, ChecklistItem } from "@/components/ui/checklist"
<Checklist defaultValue={["pens"]}>
<ChecklistItem value="pens">Buy more blue pens</ChecklistItem>
<ChecklistItem value="ship">Ship it</ChecklistItem>
</Checklist>Examples
With progress
Controlled with value, feeding a Progress bar, with check-all and clear.
Launch checklist
"use client";
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Checklist, ChecklistItem } from "@/components/ui/checklist";
import { Progress, ProgressLabel, ProgressValue } from "@/components/ui/progress";
const tasks = [
["pens", "Buy more blue pens"],
["docs", "Write the docs"],
["tests", "Run the test suite"],
["ship", "Ship it"],
] as const;
export default function ChecklistProgress() {
const [done, setDone] = useState<string[]>(["pens", "docs"]);
return (
<div className="flex w-full max-w-sm flex-col gap-5">
<Progress value={(done.length / tasks.length) * 100} seed="cp-progress">
<ProgressLabel>Launch checklist</ProgressLabel>
<ProgressValue>{() => `${done.length} of ${tasks.length}`}</ProgressValue>
</Progress>
<Checklist value={done} onValueChange={setDone} aria-label="Launch checklist">
{tasks.map(([value, label]) => (
<ChecklistItem key={value} value={value} seed={`cp-${value}`}>
{label}
</ChecklistItem>
))}
</Checklist>
<div className="flex gap-3">
<Button variant="outline" size="sm" onClick={() => setDone(tasks.map(([v]) => v))} seed="cp-all">
Check all
</Button>
<Button variant="ghost" size="sm" onClick={() => setDone([])} seed="cp-none">
Clear
</Button>
</div>
</div>
);
}Disabled items
Items that can't be ticked yet are paler; a disabled item can still arrive done.
import { Checklist, ChecklistItem } from "@/components/ui/checklist";
export default function ChecklistStates() {
return (
<Checklist defaultValue={["sketch", "lock"]} aria-label="Release steps">
<ChecklistItem value="sketch" seed="cs-sketch">
Sketch the layout
</ChecklistItem>
<ChecklistItem value="draw" seed="cs-draw">
Draw the final version
</ChecklistItem>
<ChecklistItem value="lock" disabled seed="cs-lock">
Lock the registry (done by CI)
</ChecklistItem>
<ChecklistItem value="publish" disabled seed="cs-publish">
Publish (waits for the lock)
</ChecklistItem>
</Checklist>
);
}Getting started
A card that counts down what is left and washes over when everything is done.
"use client";
import { useState } from "react";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { Checklist, ChecklistItem } from "@/components/ui/checklist";
import { Progress, ProgressLabel, ProgressValue } from "@/components/ui/progress";
const tasks = [
["account", "Make an account"],
["pen", "Pick a pen"],
["paper", "Pick a paper"],
["write", "Write your first line"],
["share", "Show someone"],
] as const;
export default function ChecklistOnboarding() {
const [done, setDone] = useState<string[]>(["account", "pen"]);
const finished = done.length === tasks.length;
return (
<Card className="w-full max-w-sm" variant={finished ? "wash" : "outline"} seed="co-card">
<CardHeader>
<CardTitle>{finished ? "All set." : "Getting started"}</CardTitle>
<CardDescription>{finished ? "That is everything. Now go and write." : `${tasks.length - done.length} to go.`}</CardDescription>
</CardHeader>
<CardContent className="flex flex-col gap-5">
<Progress value={(done.length / tasks.length) * 100} size="sm" seed="co-progress">
<ProgressLabel>Done so far</ProgressLabel>
<ProgressValue>{() => `${done.length} / ${tasks.length}`}</ProgressValue>
</Progress>
<Checklist value={done} onValueChange={setDone} aria-label="Getting started">
{tasks.map(([value, label]) => (
<ChecklistItem key={value} value={value} seed={`co-${value}`}>
{label}
</ChecklistItem>
))}
</Checklist>
</CardContent>
</Card>
);
}API reference
- value / defaultValue
string[]The done items, controlled or not.
- onValueChange
(value: string[]) => voidCalled as items are ticked and unticked.
- ChecklistItem value
stringWhat the list reports when this item is done.
- ChecklistItem disabled
booleanCan't be ticked or unticked.
Every other prop goes to Base UI's CheckboxGroup.