Checkbox
This is a checkbox with three states. The checked state and the indeterminate state have separate bindings. A hidden input gives the form support, and you write the indicator yourself.
Anatomy
The checkbox has two parts. Checkbox.Root makes the control and a hidden checkbox input for the form. Checkbox.Indicator makes the check mark or the mixed mark. It is in the DOM only while the checkbox is checked or indeterminate.
<script>
import { Checkbox } from '@human-kit/ui';
</script>
<Checkbox.Root aria-label="Accept terms">
<Checkbox.Indicator>
<CheckIcon />
</Checkbox.Indicator>
</Checkbox.Root>States
Both parts show each visual state in data-* attributes. Thus you write all of the styles in plain CSS or in utility classes. This includes the styles for the disabled state, the readonly state, and the indeterminate state.
Indeterminate
The mixed state has its own indeterminate and defaultIndeterminate bindings. The mixed state has more importance than the checked state. From the indeterminate state, the first user action makes the checkbox checked.
Forms
Checkbox.Root keeps a hidden checkbox input correct. Give the root a name and a value. If the checkbox is checked, the form submits the pair. If the checkbox is not checked, or if the checkbox is indeterminate, the form submits nothing.
Usage guidelines
- Use
checkedanddefaultCheckedfor the checked state. UseindeterminateanddefaultIndeterminatefor the mixed state. - The
indeterminateprop has more importance than thecheckedprop. If both props aretrue, the checkbox is indeterminate. - Use
valueonly for the form submission through the hidden native input. Thevalueprop does not show the visual state. - Put the checkbox in a native
<label>element. This is the most simple label pattern. As an alternative, point alabel[for]element at theidof the checkbox.
Accessibility
Checkbox.Roothasrole="checkbox"witharia-checked="true" | "false" | "mixed".- Push the
Spacekey to change the checkbox. - The
readonlyprop keeps the checkbox focusable, but it stops the state changes.
API reference
Root
The checkbox control. It makes a span with role="checkbox", and it keeps a hidden checkbox input correct for the form.
* required. Native HTML attributes of the underlying element are also accepted.
Indicator
The check mark or the mixed mark. It is in the DOM only while the checkbox is checked or indeterminate, but `forceMount` keeps it there.
* required. Native HTML attributes of the underlying element are also accepted.