components / form
Checkbox
Checkbox with checked, unchecked, and indeterminate states.
688 B gzip client componentbase ui · checkbox
checkbox-default.tsx
import { Checkbox } from "@/components/ui/checkbox";
export default function CheckboxDefault() {
return (
<div className="flex flex-col gap-3 text-sm">
<label htmlFor="email" className="flex items-center gap-2">
<Checkbox defaultChecked /> get product updates by email
</label>
<label htmlFor="device" className="flex items-center gap-2">
<Checkbox /> remember this device
</label>
<label htmlFor="disabled" className="flex items-center gap-2 text-muted-foreground">
<Checkbox disabled /> disabled option
</label>
</div>
);
}Installation
$ npx shadcn@latest add https://cd-ui.vercel.app/r/checkbox.jsonfirst time? set up components.json and the theme first: see installation.
Usage
import { Checkbox } from "@/components/ui/checkbox"
<label className="flex items-center gap-2">
<Checkbox /> I accept the terms
</label>Examples
Indeterminate
A parent that reflects its children: checked, unchecked, or partial.
checkbox-indeterminate.tsx
"use client";
import { useState } from "react";
import { Checkbox } from "@/components/ui/checkbox";
const items = ["scheduling", "records", "billing"];
export default function CheckboxIndeterminate() {
const [checked, setChecked] = useState<string[]>(["scheduling"]);
const all = checked.length === items.length;
return (
<div className="flex flex-col gap-3 text-sm">
<label htmlFor="all" className="flex items-center gap-2 font-medium">
<Checkbox
checked={all}
indeterminate={!all && checked.length > 0}
onCheckedChange={(value) => setChecked(value ? items : [])}
/>
all modules
</label>
<div className="flex flex-col gap-3 pl-6">
{items.map((item) => (
<label htmlFor={item} className="flex items-center gap-2" key={item}>
<Checkbox
checked={checked.includes(item)}
onCheckedChange={(value) => setChecked((prev) => (value ? [...prev, item] : prev.filter((i) => i !== item)))}
/>
{item}
</label>
))}
</div>
</div>
);
}API
Checkbox
Base UI · Checkbox.RootCheckbox. The check is an SVG stroke that draws itself in 160ms.
| prop | type | default |
|---|---|---|
checked · defaultCheckedControlled or initial state. | boolean | - |
onCheckedChangeCalled when checked or unchecked. | (checked: boolean) => void | - |
indeterminateShows the "partial" dash. | boolean | false |
name · valueTo submit in a form. | string | - |
classNameExtra classes, merged with | string | - |
Keyboard
| key | action |
|---|---|
| Space | Checks or unchecks. |