Checkbox
A tick box for a choice that waits for a Save button, or for one of several options that can all be on. It has a third, indeterminate state for a box that stands for a partly ticked set. For a setting that applies at once use the switch; for a set with a select-all box use the checkbox group.
Install
npx shadcn@latest add https://thrumui.dev/r/checkbox.jsonAdds components/ui/checkbox.tsx and installs @base-ui/react@1.9.0. Nothing else in your project changes.
import { Checkbox } from "@/components/ui/checkbox";Automatically installs icons check and minus from the icon set.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
checked | boolean | – | Whether it is ticked, when the parent holds the state. Use it with onCheckedChange. |
defaultChecked | boolean | Default false | Whether it starts ticked, when the checkbox holds its own state. |
onCheckedChange | (checked: boolean) => void | – | Called with the new state when it changes. |
indeterminate | boolean | Default false | Draws a dash instead of a tick and reports the mixed state. The parent sets and clears it; a press does not. |
size | "md" | "lg" | Default "md" | md is a 16 pixel box, lg is 20 pixels. Both are a 24 pixel target. |
disabled | boolean | Default false | Dimmed, skipped by Tab, and cannot be changed. |
readOnly | boolean | Default false | Keeps its state and stays focusable, but a press does not change it. |
required | boolean | Default false | The box must be ticked before its form submits. |
name | string | – | The field name when the checkbox sits in a form. |
value | string | – | What a ticked box submits, and its name inside a checkbox group. Without it a ticked box submits on. |
uncheckedValue | string | – | What an unticked box submits. By default it submits nothing, as a native checkbox does. |
parent | boolean | Default false | Inside a checkbox group: this box ticks and clears all the others, and shows a dash when some are ticked. |
aria-invalid | boolean | – | Marks the choice as wrong: the border takes the destructive colour. Pair it with a message linked by aria-describedby. |
sound | boolean | Default true | Play a rising pair of clicks when it is ticked and a falling pair when it is cleared, if the visitor has sound on. |
className | string | – | Extra classes for the box, merged last. mt-0.5 lines the 16 pixel box up with the first line of 14 pixel text. |
Motion and sound
Motion
- The box changes colour over 150 ms while the tick or dash fades in and grows from half size.
- Clearing plays the same change backwards, and a second press during it reverses it from where it is.
- Reduced motion: the box and its mark change at once.
Sound
- Two rising clicks when the box is ticked, two falling clicks when it is cleared.
Sound is off until the visitor turns it on. Pass sound={false} to keep this component silent. How sound works
Accessibility
- It follows the WAI-ARIA checkbox pattern: the checkbox role with aria-checked true, false or mixed. A hidden native checkbox carries its value in a form.
- Name it by wrapping it in a label, as the example does, or with an aria-label. Pressing the label's text also changes it.
- The states differ in shape as well as colour: a tick, a dash, or an empty box.
- An invalid box changes its border colour. The message next to it carries the meaning, so colour is never the only sign.
- The box's border keeps 3 to 1 contrast against the page in both themes, and the mark keeps 4.5 to 1 against the fill.
- A long label wraps beside the box. Nothing overflows at 160 pixels wide.
| Keys | Action |
|---|---|
| Tab | Focus the checkbox. A disabled one is skipped. |
| Space | Tick or clear it. A read-only checkbox does not change. |
Source
"use client";
import { Checkbox as CheckboxPrimitive } from "@base-ui/react/checkbox";
import { cn } from "@/lib/utils";
import { play } from "@/lib/thrum-sound";
import { CheckIcon } from "@/components/icons/check";
import { MinusIcon } from "@/components/icons/minus";
export type CheckboxProps = CheckboxPrimitive.Root.Props & {
/** `md` is a 16 pixel box, `lg` is 20. Both are a 24 pixel target. */
size?: "md" | "lg";
/** Play a short rising or falling pair of clicks when it changes, if the visitor has sound on. */
sound?: boolean;
};
/**
* A tick box for a choice that waits for a Save button, or one of several options that can all be on.
* Name it with a `<label>` around it or an `aria-label`. `indeterminate` draws a dash: some, but not all, of what it stands for.
*/
export function Checkbox({ size = "md", sound = true, className, onCheckedChange, ...props }: CheckboxProps) {
return (
<CheckboxPrimitive.Root
data-slot="checkbox" data-surface="control"
onCheckedChange={(checked, details) => {
if (sound) play(checked ? "toggleOn" : "toggleOff");
onCheckedChange?.(checked, details);
}}
className={cn(
// The pseudo-element keeps both sizes a 24 pixel target.
"relative inline-flex shrink-0 cursor-pointer items-center justify-center rounded-[min(var(--radius-sm),5px)] border border-input bg-background text-primary-foreground select-none after:absolute",
"transition-[background-color,border-color] duration-150 ease-out motion-reduce:transition-none",
"focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring",
"data-checked:border-primary data-checked:bg-primary data-indeterminate:border-primary data-indeterminate:bg-primary",
"aria-invalid:border-destructive data-disabled:cursor-not-allowed data-disabled:opacity-50 data-readonly:cursor-default",
size === "lg" ? "size-5 after:-inset-0.5" : "size-4 after:-inset-1",
className,
)}
{...props}
>
{/* Kept in the page so the mark can fade out as well as in. */}
<CheckboxPrimitive.Indicator
keepMounted
className="group/mark flex transition-[opacity,scale] duration-150 ease-out data-unchecked:scale-50 data-unchecked:opacity-0 motion-reduce:transition-none"
>
{/* Drawn at 12 or 14 pixels, so the stroke is doubled to stay 1.5 pixels on screen. */}
<CheckIcon size={size === "lg" ? 14 : 12} absoluteStrokeWidth={size !== "lg"} strokeWidth={size === "lg" ? 1.5 : 1.25} className="group-data-indeterminate/mark:hidden" />
<MinusIcon size={size === "lg" ? 14 : 12} absoluteStrokeWidth={size !== "lg"} strokeWidth={size === "lg" ? 1.5 : 1.25} className="hidden group-data-indeterminate/mark:block" />
</CheckboxPrimitive.Indicator>
</CheckboxPrimitive.Root>
);
}Files and dependencies
- Adds
components/ui/checkbox.tsx- Packages
@base-ui/react@1.9.0- Brings along
- Sound engine
- Icons
checkminus
Changelog
- 1.0.0 · 2026-12-03First release.