Checkbox
A box to tick, with its label, for a yes or no and for a list of choices.
Installation
pnpm dlx @human-kit/design add checkboxnpx @human-kit/design add checkboxyarn dlx @human-kit/design add checkboxbunx @human-kit/design add checkboxThe command copies the files to src/lib/components/ui/checkbox. It also
installs the packages that the files import.
Usage
<script lang="ts">
import * as Checkbox from '$lib/components/ui/checkbox';
let notify = $state(true);
</script>
<Checkbox.Root label="Send a copy to the patient" bind:checked={notify} /> Examples
Indeterminate
A box over a list says whether all, none or some of it is ticked: indeterminate draws a dash for "some". A press on it ticks the whole list.
Invalid
invalid turns the box and the label red. Say what to do in description while it holds, and drop it once the box is ticked.
Accept the terms to go on.
Guidelines
- Use a checkbox for a choice the user confirms later, with a form’s button. For a setting that applies at once, use a switch.
- Give the words in
label, not beside the box: the label then presses the box too, and a long one wraps with the box on its first line. - Write the label as what is true when the box is ticked: "Send a copy to the patient", not "Don’t send".
- Use
descriptionfor a line that explains the choice, and for the error while the box isinvalid. - For several boxes that answer one question, put them in a checkbox group.
Accessibility
- The box is a
role="checkbox"witharia-checkedset totrue,falseormixed.Spaceticks it. labelis a<label>tied to the box, anddescriptionis tied to it witharia-describedby. Without a label, name the box witharia-label.- The box is 16px, and the area that presses it is 24px, the target size of WCAG 2.5.8.
invalidsetsaria-invalid.readonlykeeps the box in the tab order and refuses changes;disabledtakes it out.- A hidden native input carries
name,valueandrequired, so the box takes part in a form.
API reference
| Prop | Type | Default |
|---|---|---|
label | string The words beside the box. Without them, give an | — |
description | string A line under the label. It shows only with a label. | — |
checked | boolean Whether the box is ticked. Bindable. | — |
defaultChecked | boolean Ticked at the start, when you give no | — |
onCheckedChange | (checked: boolean) => void Called when the user ticks or clears the box. | — |
controlledChecked | boolean Stops the box writing back to | false |
indeterminate | boolean Draws a dash, for "some". The next press ticks the box. Bindable. | — |
defaultIndeterminate | boolean Indeterminate at the start, when you give no | — |
onIndeterminateChange | (indeterminate: boolean) => void Called when a press takes the box out of the indeterminate state. | — |
invalid | boolean The value is wrong: the box and the label turn red. | false |
disabled | boolean Turns the box off and takes it out of the tab order. | false |
readonly | boolean Shows the state and refuses changes, and keeps the focus. | false |
required | boolean The form needs the box ticked. | false |
name | string The name of the hidden input, for the form. | — |
value | string What the form sends when the box is ticked. In a group, the value it adds. | — |
id | string The id the label points to. One is made when you give none. | — |
element | HTMLSpanElement | null The rendered box. Bindable. | — |
Plus every attribute of a <span>, set on the box. With a label, class goes on the row that holds the box and its words.