Checkbox
A drawn box that ticks with a pull up and out past the corner, and dashes when indeterminate.
Once a month, written by hand. Well, typed.
"use client";
import { useState } from "react";
import { Checkbox } from "@/components/ui/checkbox";
import { Field, FieldContent, FieldDescription, FieldGroup, FieldLabel } from "@/components/ui/field";
import { Label } from "@/components/ui/label";
const items = ["Paper", "Ink", "A steady hand"];
export default function CheckboxDemo() {
const [picked, setPicked] = useState(["Paper"]);
const all = picked.length === items.length;
return (
<FieldGroup className="max-w-md">
<Field orientation="horizontal">
<Checkbox id="cb-terms" defaultChecked seed="cb-terms" />
<FieldContent>
<FieldLabel htmlFor="cb-terms">Send me the newsletter</FieldLabel>
<FieldDescription>Once a month, written by hand. Well, typed.</FieldDescription>
</FieldContent>
</Field>
<div className="flex flex-col gap-3">
<Label>
<Checkbox
seed="cb-all"
checked={all}
indeterminate={picked.length > 0 && !all}
onCheckedChange={(checked) => setPicked(checked ? items : [])}
/>
Everything you need
</Label>
{items.map((item) => (
<Label key={item} className="pl-7">
<Checkbox
seed={`cb-${item}`}
checked={picked.includes(item)}
onCheckedChange={(checked) => setPicked((p) => (checked ? [...p, item] : p.filter((x) => x !== item)))}
/>
{item}
</Label>
))}
</div>
<Label>
<Checkbox disabled seed="cb-disabled" />
Disabled
</Label>
</FieldGroup>
);
}Installation
pnpm dlx shadcn@latest add @ballpoint/ checkbox"use client";
import { Checkbox as CheckboxPrimitive } from "@base-ui/react/checkbox";
import { usePen, type Pen } from "@/hooks/use-ink-box";
import { inkClassName, Stroke } from "@/lib/ink";
import { InkOutline } from "@/lib/ink-outline";
import { dashStroke, tickStroke } from "@/lib/ink-sketch";
/**
* A drawn box. Checking it ticks it, the pen running up and out past the
* corner; unchecking pulls the tick back out. Indeterminate is a dash.
*/
function Checkbox({
className,
seed,
roughness,
passes,
radius,
corners,
draw,
weight,
speed,
...props
}: CheckboxPrimitive.Root.Props & Omit<Pen, "fill" | "shadow"> & { seed?: string | number }) {
const pen = usePen({ roughness, passes, radius, corners, draw, weight, speed });
return (
<CheckboxPrimitive.Root
data-slot="checkbox"
className={inkClassName(
"peer group/checkbox relative inline-flex size-5 shrink-0 cursor-pointer items-center justify-center outline-none",
// A finger-sized hit area around the small box.
"after:absolute after:-inset-x-3 after:-inset-y-2",
"text-ink-3 transition-colors duration-(--dur-hover) not-data-checked:not-data-indeterminate:hover:text-ink data-checked:text-ink data-indeterminate:text-ink",
"aria-invalid:text-destructive data-invalid:text-destructive",
"focus-visible:outline-solid focus-visible:outline-[1.5px] focus-visible:outline-offset-4 focus-visible:outline-ring focus-visible:[border-radius:var(--hand-radius)]",
"data-disabled:cursor-not-allowed data-disabled:opacity-50",
className,
)}
{...props}
>
<InkOutline pen={pen} seed={seed} estimate={[20, 20]} pad={6} maxRadius={6}>
{({ w, h, s }) => (
<>
<Stroke d={tickStroke(s + 40, w)} draw="checked" duration={280} width={1.9} />
<Stroke d={dashStroke(s + 41, w, h / 2)} draw="indeterminate" duration={200} width={1.9} />
</>
)}
</InkOutline>
</CheckboxPrimitive.Root>
);
}
export { Checkbox };Usage
import { Checkbox } from "@/components/ui/checkbox"
import { Label } from "@/components/ui/label"
<Label>
<Checkbox defaultChecked />
Send me the newsletter
</Label>Examples
States
Unchecked, checked, mixed, disabled, and invalid with an error.
You need to agree to continue.
Please tick the box.
import { Checkbox } from "@/components/ui/checkbox";
import { Field, FieldContent, FieldDescription, FieldError, FieldLabel } from "@/components/ui/field";
import { Label } from "@/components/ui/label";
export default function CheckboxStates() {
return (
<div className="grid w-full max-w-md gap-5">
<div className="flex flex-wrap gap-x-8 gap-y-4">
<Label>
<Checkbox seed="cs-off" />
Unchecked
</Label>
<Label>
<Checkbox defaultChecked seed="cs-on" />
Checked
</Label>
<Label>
<Checkbox indeterminate seed="cs-mixed" />
Mixed
</Label>
</div>
<div className="flex flex-wrap gap-x-8 gap-y-4">
<Label>
<Checkbox disabled seed="cs-disabled" />
Disabled
</Label>
<Label>
<Checkbox disabled defaultChecked seed="cs-disabled-on" />
Disabled, checked
</Label>
</div>
<Field orientation="horizontal" data-invalid>
<Checkbox id="cs-terms" aria-invalid seed="cs-terms" />
<FieldContent>
<FieldLabel htmlFor="cs-terms">Accept the terms</FieldLabel>
<FieldDescription>You need to agree to continue.</FieldDescription>
<FieldError>Please tick the box.</FieldError>
</FieldContent>
</Field>
</div>
);
}Choice cards
Wrap a Field in FieldLabel and the whole row becomes the target.
import { Checkbox } from "@/components/ui/checkbox";
import { Field, FieldContent, FieldDescription, FieldGroup, FieldLabel, FieldTitle } from "@/components/ui/field";
const extras = [
["gift", "Gift wrap", "Tied with string."],
["note", "Handwritten note", "In blue ballpoint, naturally."],
["express", "Express post", "Arrives in two days."],
] as const;
// Wrap a whole Field in FieldLabel and the entire row becomes the click target.
export default function CheckboxCards() {
return (
<FieldGroup className="w-full max-w-md gap-4">
{extras.map(([id, title, description], i) => (
<FieldLabel key={id} htmlFor={`cc-${id}`}>
<Field orientation="horizontal">
<Checkbox id={`cc-${id}`} defaultChecked={i === 1} seed={`cc-${id}`} />
<FieldContent>
<FieldTitle>{title}</FieldTitle>
<FieldDescription>{description}</FieldDescription>
</FieldContent>
</Field>
</FieldLabel>
))}
</FieldGroup>
);
}Select all
A parent that is mixed when only some children are ticked.
"use client";
import { useState } from "react";
import { Checkbox } from "@/components/ui/checkbox";
import { Label } from "@/components/ui/label";
const items = ["Notebooks", "Stamps", "Stickers", "Tape"];
export default function CheckboxSelectAll() {
const [picked, setPicked] = useState<string[]>(["Stamps", "Tape"]);
const all = picked.length === items.length;
return (
<div className="flex flex-col gap-4">
<Label className="font-bold">
<Checkbox
checked={all}
indeterminate={picked.length > 0 && !all}
onCheckedChange={(on) => setPicked(on ? items : [])}
seed="csa-all"
/>
Everything on the desk
</Label>
<div className="flex flex-col gap-3 pl-8">
{items.map((item) => (
<Label key={item}>
<Checkbox checked={picked.includes(item)} onCheckedChange={(on) => setPicked((list) => (on ? [...list, item] : list.filter((x) => x !== item)))} seed={`csa-${item}`} />
{item}
</Label>
))}
</div>
</div>
);
}In a form
A required box with a description and an error after a try.
"use client";
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Checkbox } from "@/components/ui/checkbox";
import { Field, FieldDescription, FieldError, FieldGroup, FieldLabel } from "@/components/ui/field";
export default function CheckboxForm() {
const [agreed, setAgreed] = useState(false);
const [tried, setTried] = useState(false);
const [done, setDone] = useState(false);
const invalid = tried && !agreed;
return (
<form
className="w-full max-w-md"
onSubmit={(event) => {
event.preventDefault();
setTried(true);
setDone(agreed);
}}
>
<FieldGroup>
<Field orientation="horizontal" data-invalid={invalid || undefined}>
<Checkbox id="cf-agree" checked={agreed} onCheckedChange={(on) => setAgreed(on)} aria-invalid={invalid} seed="cf-agree" />
<div className="grid gap-1">
<FieldLabel htmlFor="cf-agree">I have read the terms</FieldLabel>
<FieldDescription>Nothing in them is surprising. We checked.</FieldDescription>
{invalid && <FieldError>Tick the box to go on.</FieldError>}
</div>
</Field>
<div className="flex items-center gap-4">
<Button type="submit" seed="cf-submit">
Sign up
</Button>
{done && <span className="text-ink-2">Welcome aboard.</span>}
</div>
</FieldGroup>
</form>
);
}API reference
- indeterminate
booleanDraws a dash instead of a tick.
- seed
string | numberPins the drawing.
Every other prop goes to Base UI's Checkbox.
Pen settings
The drawing also takes draw, roughness, passes, radius, corners, weight and speed, as props or from the nearest InkProvider. What each one does.