Checkbox
A checkbox with its label, hint and error, controlled or not.
Examples
Playground
States
Every state side by side. Hover and focus are forced, so they can be reviewed without a pointer.
unchecked
hover
focus
checked
indeterminate
error
You have to accept to continue.
disabled
Content
A hint, a required mark, an error with its hint kept, and a label long enough to wrap.
hint
You can withdraw at any time.
required
error, hint kept
You have to accept to continue.You can withdraw at any time.
long label
Install
npx shadcn add @hipuku/checkboxUsage
import { Checkbox } from '@/components/haus/components/Checkbox'
<Checkbox label="Email me about releases" checked={subscribed} onChange={setSubscribed} />API
Checkbox also takes the input's own attributes and forwards its ref to the input. className
lands on the root and controlClassName on the row holding the box and label.
| Prop | Type | Description |
|---|---|---|
onChange | ((checked: boolean, event: React.ChangeEvent<HTMLInputElement>) => void) | (checked, event). The boolean first because it is what a caller almost
always wants; the event second because without it there was no way to read
event.target.name, which is how a form with many checkboxes tells them
apart. Toggle and RadioGroup hand back the same pair. |
label | string | |
hint | string | |
error | string | |
indeterminate | boolean | |
controlClassName | string | Lands on the row holding the box and the label, where className used to.
The named second target ruling B5 asks for. |
Controlled or not
It is uncontrolled unless you pass checked. onChange gives you (checked, event): read
event.target.name to tell checkboxes in one form apart.
States
indeterminate shows a bar for a parent whose children are partly checked. error marks it
invalid and shows the message under the label; hint adds a line of help.
Accessibility
- A native checkbox: Space toggles it, and it tabs as itself.
- The label is the accessible name. The hint and error are read as its description.
requiredanderrorsetaria-requiredandaria-invalid, and the error is not shown by colour alone.