Checkbox
A 16px checkbox on Base UI with a 24px hit area.
With a label
import { Checkbox } from "@/components/ui/checkbox"
import { Label } from "@/components/ui/label"
export default function CheckboxWithALabel() {
return (
<div className="flex items-center gap-2">
<Checkbox id="checkbox-archive" defaultChecked />
<Label htmlFor="checkbox-archive">Include archived tasks</Label>
</div>
)
}
With a description
A summary of open tasks and unpaid invoices, sent on Monday morning.
import { Checkbox } from "@/components/ui/checkbox"
import { Field, FieldContent, FieldDescription, FieldLabel } from "@/components/ui/field"
export default function CheckboxWithADescription() {
return (
<Field orientation="horizontal" className="max-w-sm">
<Checkbox id="checkbox-digest" aria-describedby="checkbox-digest-description" />
<FieldContent>
<FieldLabel htmlFor="checkbox-digest">Weekly digest</FieldLabel>
<FieldDescription id="checkbox-digest-description">
A summary of open tasks and unpaid invoices, sent on Monday morning.
</FieldDescription>
</FieldContent>
</Field>
)
}
In a group
import { Checkbox } from "@/components/ui/checkbox"
import { Field, FieldGroup, FieldLabel, FieldLegend, FieldSet } from "@/components/ui/field"
const events = [
{ id: "assigned", label: "A task is assigned to me", checked: true },
{ id: "mentioned", label: "Someone mentions me", checked: true },
{ id: "released", label: "A release ships", checked: false },
]
export default function CheckboxInAGroup() {
return (
<FieldSet className="w-full max-w-xs">
<FieldLegend variant="label">Notify me when</FieldLegend>
<FieldGroup data-slot="checkbox-group">
{events.map((event) => (
<Field key={event.id} orientation="horizontal">
<Checkbox id={`checkbox-notify-${event.id}`} name="notify" value={event.id} defaultChecked={event.checked} />
<FieldLabel htmlFor={`checkbox-notify-${event.id}`}>{event.label}</FieldLabel>
</Field>
))}
</FieldGroup>
</FieldSet>
)
}
Disabled
import { Checkbox } from "@/components/ui/checkbox"
import { Field, FieldGroup, FieldLabel } from "@/components/ui/field"
export default function CheckboxDisabled() {
return (
<FieldGroup data-slot="checkbox-group" className="max-w-xs">
<Field orientation="horizontal" data-disabled="true">
<Checkbox id="checkbox-export-locked" disabled />
<FieldLabel htmlFor="checkbox-export-locked">Export to the ledger</FieldLabel>
</Field>
<Field orientation="horizontal" data-disabled="true">
<Checkbox id="checkbox-audit-locked" defaultChecked disabled />
<FieldLabel htmlFor="checkbox-audit-locked">Keep an audit trail</FieldLabel>
</Field>
</FieldGroup>
)
}
Invalid
Confirm the totals before you send the invoices.
import { Checkbox } from "@/components/ui/checkbox"
import { Field, FieldContent, FieldError, FieldLabel } from "@/components/ui/field"
export default function CheckboxInvalid() {
return (
<Field orientation="horizontal" data-invalid="true" className="max-w-sm">
<Checkbox id="checkbox-confirm" required aria-invalid aria-describedby="checkbox-confirm-error" />
<FieldContent>
<FieldLabel htmlFor="checkbox-confirm">I have checked the invoice totals</FieldLabel>
<FieldError id="checkbox-confirm-error">Confirm the totals before you send the invoices.</FieldError>
</FieldContent>
</Field>
)
}
Installation
npx shadcn@latest add @khaeli/checkboxbunx --bun shadcn@latest add @khaeli/checkboxInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
- Registry items
- npm packages
- @base-ui/react ^1.7.0
- @keyline-icons/react ^2.2.0
Keyboard
Space- Ticks and clears the checkbox. Enter does nothing.
Tab- Moves focus to the checkbox. A
disabledcheckbox leaves the tab order.
Accessible name
- It renders an element with
role="checkbox"andaria-checked. Name it with a @khaeli/label whosehtmlForis the checkbox'sid, or wrap both in the label. - Name a set of checkboxes as a group:
FieldSetwith aFieldLegendfrom @khaeli/field. - A description or an error is tied to the checkbox with
aria-describedby. An unmet required checkbox carriesaria-invalid.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/checkbox.json