Checkbox
A native checkbox for selecting one or more options.
Overview
Checkbox renders a native checkbox. Use it when a person can select any number of independent options, including none. It participates in a form through name and value. It does not store form state or validate the field.
Preview
Customize
Installation
Add the component with the CLI. It copies the files in and installs anything they depend on.
npx vinyaas add checkboxUsage
import { Checkbox } from "@/components/ui/checkbox";
import { Label } from "@/components/ui/label";
export function TermsField() {
return (
<div className="flex items-center gap-2">
<Checkbox id="terms" name="terms" value="accepted" />
<Label htmlFor="terms">Accept terms and conditions</Label>
</div>
);
}Examples
With a label
htmlFor matches the checkbox id. The label names the control.
Checked
defaultChecked starts the box selected.
Disabled
A disabled checkbox cannot be toggled.
Indeterminate
Use indeterminate when a parent checkbox represents a partial selection.
In practice
Independent notification channels use checkboxes. Save writes the selection.
API
Other attributes for the underlying element are passed through.
| Name | Type | Default | Description |
|---|---|---|---|
checked | boolean | — | Controlled checked state. Pair it with onChange. |
defaultChecked | boolean | — | Uncontrolled initial checked state. |
disabled | boolean | — | Native disabled state. The control stays in the tab order. |
required | boolean | — | Native required state for the surrounding form. |
name | string | — | Submitted with the form when the box is checked. |
value | string | — | Submitted value. The default is on. |
indeterminate | boolean | — | Mixed state, such as a parent of partially selected items. |
className | string | — | Merged onto the checkbox with cn. |
Accessibility
Checkbox is a native checkbox. It does not add an ARIA role.
- Name it with a label whose htmlFor matches the checkbox id.
- Space toggles it when it has focus.
- disabled blocks toggling. The control stays in the accessibility tree.
- Focus uses a visible focus-visible ring.
- aria-invalid and other ARIA attributes are passed through. indeterminate is the native mixed state.