Skip to the content
human-kit design
04 — Form

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 checkbox
npx @human-kit/design add checkbox
yarn dlx @human-kit/design add checkbox
bunx @human-kit/design add checkbox

The 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 description for a line that explains the choice, and for the error while the box is invalid.
  • For several boxes that answer one question, put them in a checkbox group.

Accessibility

  • The box is a role="checkbox" with aria-checked set to true, false or mixed. Space ticks it.
  • label is a <label> tied to the box, and description is tied to it with aria-describedby. Without a label, name the box with aria-label.
  • The box is 16px, and the area that presses it is 24px, the target size of WCAG 2.5.8.
  • invalid sets aria-invalid. readonly keeps the box in the tab order and refuses changes; disabled takes it out.
  • A hidden native input carries name, value and required, so the box takes part in a form.

API reference

PropTypeDefault
labelstring

The words beside the box. Without them, give an aria-label.

—
descriptionstring

A line under the label. It shows only with a label.

—
checkedboolean

Whether the box is ticked. Bindable.

—
defaultCheckedboolean

Ticked at the start, when you give no checked.

—
onCheckedChange(checked: boolean) => void

Called when the user ticks or clears the box.

—
controlledCheckedboolean

Stops the box writing back to checked: it only reports through onCheckedChange, so the parent can refuse a change.

false
indeterminateboolean

Draws a dash, for "some". The next press ticks the box. Bindable.

—
defaultIndeterminateboolean

Indeterminate at the start, when you give no indeterminate.

—
onIndeterminateChange(indeterminate: boolean) => void

Called when a press takes the box out of the indeterminate state.

—
invalidboolean

The value is wrong: the box and the label turn red.

false
disabledboolean

Turns the box off and takes it out of the tab order.

false
readonlyboolean

Shows the state and refuses changes, and keeps the focus.

false
requiredboolean

The form needs the box ticked.

false
namestring

The name of the hidden input, for the form.

—
valuestring

What the form sends when the box is ticked. In a group, the value it adds.

—
idstring

The id the label points to. One is made when you give none.

—
elementHTMLSpanElement | 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.