Checkbox
A checkbox turns one option on or off. In a group, people can pick any number of options, including none.
Installation
npm gives you every component from the package. shadcn copies the source into your app instead: a copy pinned to this version that you can change. The shadcn commands need the @kanok-ui registry in your components.json once, see Installation.
Use it
The label is the element's content on the web component and the label prop in React + Tailwind. Import checkbox-group only when you use the group.
One checkbox
checkedsets the state.changefires when the user toggles it.- A checked box submits
value(onby default) undername; an unchecked box submits nothing. requiredblocks submit until the box is checked, as for accepting terms.
checked:
A group of checkboxes
ui-checkbox-group labels a set of boxes and is announced as one group:
requiredmeans "at least one": the form won't submit until a box is checked.orientation="horizontal"puts the boxes in a row.disabledandreadonlyapply to every box, without overwriting each box's own setting.- Each checkbox still submits its own
nameandvalue, so read them withFormData.getAll().
React + Tailwind has no checkbox group: use FormGroup (Carbon's fieldset), lay the boxes out yourself, and check "at least one" in your own validation. In the example, docsError comes from your own validation, such as "Select at least one option."
Helper text, warnings and errors on one box
A single checkbox has its own message line under the label: helper-text, a non-blocking warn with warn-text, and invalid with invalid-text. An error beats a warning, and a warning beats the helper text.
Warnings and errors on a group
On ui-checkbox-group, the message belongs to the whole group and shows once under it. An error also turns every enabled box red, and each box repeats the error for screen readers.
React + Tailwind's FormGroup has invalid and message (the message turns red) but no warning state.
Indeterminate
indeterminate shows a bar instead of a check: a parent box whose children are partly checked. The user's next click clears it. Keep the parent in step with its children in your code:
all, chosen, some and pick are the same in every React version. On the web component, do the same in change listeners: set checked and indeterminate on the parent from its children.
Hide the label
hide-label keeps the label for screen readers but doesn't show it, such as for a checkbox in a table row.
Read-only and disabled
readonly (readOnly in React + Tailwind) keeps the state and submits it, but clicks and Space change nothing. disabled greys the box out, and it isn't submitted.
Required in a form
A required group needs at least one box; a required single box must be checked. The errors wait until the user has used the field or pressed Submit. Press Submit with nothing checked to see both.
Submitted:
submit prevents the navigation and puts new FormData(form) into sent, as in the HTML. The group's message is built in, in Thai and English; required-text or invalid-text replaces it. More: Forms and validation.
Selectable tiles
When each option needs a title and a description, use ui-selectable-tile: a checkbox drawn as a tile (Carbon's SelectableTile), from @purpurabog/ui/selection-tiles. It submits name and value like a checkbox, and Space toggles it. In a ui-checkbox-group the tiles are members like checkboxes: the group's required, disabled, readonly and error apply to them.
Calculate and pay
Thai ⇄ English
Signed copies by messenger
Skeleton
While the options load, show one ui-checkbox-skeleton per row: an empty box and a label bar. It is hidden from screen readers, so say "loading" on the container (for example aria-busy).
On the web component, import it from @purpurabog/ui/checkbox-skeleton.
Keyboard
| Key | Does |
|---|---|
| Space | Checks or unchecks the focused box |
| Tab | Moves to the next box: each checkbox is its own Tab stop |
Accessibility
- Each box is a native checkbox inside a
<label>, so the whole row is clickable and screen readers hear its state, "mixed" included. ui-checkbox-groupis arole="group"named by its label; its helper, warning or error text describes the group, and every box repeats the group's error so it is heard on the first box.- Don't use a checkbox for a setting that applies at once; use a Toggle.
Unstyled React parts with the same behaviour: Headless Checkbox.
API
Generated from the package. In React, pass a property as a prop of the same name; events are props too (the React column).
<ui-checkbox> · React UiCheckbox
Attributes
| Attribute | Property | Type | Default | Description |
|---|---|---|---|---|
checked |
checked |
boolean |
false |
|
indeterminate |
indeterminate |
boolean |
false |
Mixed state, e.g. a "select all" box with some children checked. Cleared on user toggle. |
disabled |
disabled |
boolean |
false |
Disables the control; a disabled <fieldset> ancestor does the same. |
required |
required |
boolean |
false |
Blocks form submission while the field is empty. |
readonly |
readonly |
boolean |
false |
Shows and submits the value but blocks changes. |
hide-label |
hideLabel |
boolean |
false |
Keep the label text for screen readers but don't show it. |
name |
name |
string |
'' |
Reflected: form-associated elements submit under the name attribute. |
value |
value |
string |
'on' |
|
helper-text |
helperText |
string |
'' |
Helper text under the field, shown while there is no error or warning. |
invalid-text |
invalidText |
string |
'' |
Error text shown while invalid or while the field fails validation. |
invalid |
invalid |
boolean |
false |
Forces the error state, e.g. after server-side validation. |
warn |
warn |
boolean |
false |
Non-blocking warning; shown when there is no error. |
warn-text |
warnText |
string |
'' |
Text of the warning shown while warn. |
Properties
| Property | Type | Description |
|---|---|---|
groupDisabled |
boolean |
Set by ui-checkbox-group; kept apart from the author's own disabled / readonly. |
groupMessage |
string |
Error text of the owning ui-checkbox-group while the group is invalid. The group shows it once below the boxes; each box repeats it as its own accessible description and reports aria-invalid, so a screen reader landing on the first box hears why (an IDREF cannot cross the group's shadow root). |
willValidate |
(read-only) Whether the field takes part in constraint validation (not while disabled), as natively. | |
labels |
(read-only) The <label>s that point at this field. |
Methods
| Method | Description |
|---|---|
setCustomValidity(message) |
Like <input>.setCustomValidity(): a non-empty message makes the field invalid (and is shown as its error once touched) until cleared with ''. |
Events
| Event | React | Description |
|---|---|---|
change |
none (use a ref) | When the user toggles the box |
Slots
| Slot | Description |
|---|---|
| (default) | Label text |
CSS parts
| Part | Description |
|---|---|
base |
The clickable row (label element) |
control |
The native checkbox input |
box |
The drawn 16px box |
label |
The text beside the box |
status-icon |
The error / warning icon |
helper-text |
The helper line |
warning-text |
The warning line |
warn-text |
Same line as warning-text (named after the warn-text attribute / slot) |
error-text |
The error line |
invalid-text |
Same line as error-text (named after the invalid-text attribute / slot) |
CSS custom properties
| Custom property | Default | Description |
|---|---|---|
--ui-checkbox-border |
Border of the box (default --ui-ink; Kanok: --ui-ink-subtle) |
|
--ui-checkbox-checked |
Fill and border when checked or mixed (default --ui-ink; Kanok: --ui-primary) |
|
--ui-checkbox-mark |
Check mark colour (default --ui-icon-inverse; Kanok: --ui-on-primary) |
<ui-checkbox-group> · React UiCheckboxGroup
Attributes
| Attribute | Property | Type | Default | Description |
|---|---|---|---|---|
label |
label |
string |
'' |
Visible label text; the label slot replaces it when markup is needed. |
hide-label |
hideLabel |
boolean |
false |
Keep the group label for screen readers but don't show it. |
helper-text |
helperText |
string |
'' |
Helper text under the field, shown while there is no error or warning. |
required |
required |
boolean |
false |
Blocks form submission while the field is empty. |
required-text |
requiredText |
string |
'' |
Error for an empty required group. |
invalid |
invalid |
boolean |
false |
Forces the error state, e.g. after server-side validation. |
invalid-text |
invalidText |
string |
'' |
Error text shown while invalid or while the field fails validation. |
warn |
warn |
boolean |
false |
Non-blocking warning; shown when there is no error. |
warn-text |
warnText |
string |
'' |
Text of the warning shown while warn. |
disabled |
disabled |
boolean |
false |
Disables the control; a disabled <fieldset> ancestor does the same. |
readonly |
readonly |
boolean |
false |
Shows and submits the value but blocks changes. |
orientation |
orientation |
'vertical' | 'horizontal' |
'vertical' |
Properties
| Property | Type | Description |
|---|---|---|
willValidate |
(read-only) Whether the field takes part in constraint validation (not while disabled), as natively. | |
labels |
(read-only) The <label>s that point at this field. |
Methods
| Method | Description |
|---|---|
setCustomValidity(message) |
Like <input>.setCustomValidity(): a non-empty message makes the field invalid (and is shown as its error once touched) until cleared with ''. |
Slots
| Slot | Description |
|---|---|
| (default) | <ui-checkbox> or <ui-selectable-tile> elements |
label |
Label content with markup; the label attribute is the plain-text fallback |
helper-text |
Helper text with markup (links, bold); overrides helper-text |
warn-text |
Warning text shown while warn; overrides warn-text |
invalid-text |
Error text shown while invalid; overrides invalid-text |
CSS parts
| Part | Description |
|---|---|
base |
The group box holding the checkboxes |
label |
The group label |
status-icon |
The error / warning icon |
helper-text |
The helper line |
warning-text |
The warning line |
warn-text |
Same line as warning-text (named after the warn-text attribute / slot) |
error-text |
The error line |
invalid-text |
Same line as error-text (named after the invalid-text attribute / slot) |
<ui-selectable-tile> · React UiSelectableTile
Attributes
| Attribute | Property | Type | Default | Description |
|---|---|---|---|---|
checked |
checked |
boolean |
false |
|
indeterminate |
indeterminate |
boolean |
false |
Mixed state, e.g. a "select all" box with some children checked. Cleared on user toggle. |
disabled |
disabled |
boolean |
false |
Disables the control; a disabled <fieldset> ancestor does the same. |
required |
required |
boolean |
false |
Blocks form submission while the field is empty. |
readonly |
readonly |
boolean |
false |
Shows and submits the value but blocks changes. |
hide-label |
hideLabel |
boolean |
false |
Keep the label text for screen readers but don't show it. |
name |
name |
string |
'' |
Reflected: form-associated elements submit under the name attribute. |
value |
value |
string |
'on' |
|
helper-text |
helperText |
string |
'' |
Helper text under the field, shown while there is no error or warning. |
invalid-text |
invalidText |
string |
'' |
Error text shown while invalid or while the field fails validation. |
invalid |
invalid |
boolean |
false |
Forces the error state, e.g. after server-side validation. |
warn |
warn |
boolean |
false |
Non-blocking warning; shown when there is no error. |
warn-text |
warnText |
string |
'' |
Text of the warning shown while warn. |
Properties
| Property | Type | Description |
|---|---|---|
groupDisabled |
boolean |
Set by ui-checkbox-group; kept apart from the author's own disabled / readonly. |
groupMessage |
string |
Error text of the owning ui-checkbox-group while the group is invalid. The group shows it once below the boxes; each box repeats it as its own accessible description and reports aria-invalid, so a screen reader landing on the first box hears why (an IDREF cannot cross the group's shadow root). |
willValidate |
(read-only) Whether the field takes part in constraint validation (not while disabled), as natively. | |
labels |
(read-only) The <label>s that point at this field. |
Methods
| Method | Description |
|---|---|
setCustomValidity(message) |
Like <input>.setCustomValidity(): a non-empty message makes the field invalid (and is shown as its error once touched) until cleared with ''. |
Events
| Event | React | Description |
|---|---|---|
change |
none (use a ref) | When the user toggles the box |
Slots
| Slot | Description |
|---|---|
| (default) | Tile content (title, description…) |
CSS parts
| Part | Description |
|---|---|
base |
The clickable row (label element) |
control |
The native checkbox input |
box |
The drawn 16px box |
label |
The text beside the box |
status-icon |
The error / warning icon |
helper-text |
The helper line |
warning-text |
The warning line |
warn-text |
Same line as warning-text (named after the warn-text attribute / slot) |
error-text |
The error line |
invalid-text |
Same line as error-text (named after the invalid-text attribute / slot) |
CSS custom properties
| Custom property | Default | Description |
|---|---|---|
--ui-tile-border-color |
Border of an unchosen tile (default transparent; Kanok: a hairline); --ui-card-radius its corners |
|
--ui-card-background |
Its fill (default --ui-surface-1; Kanok paper: a cool grey that sets it apart from the page, no border) |
|
--ui-tile-selected-border |
Border of the chosen tile (default --ui-layer-selected-inverse; Kanok: primary blue); --ui-tile-selected-mark its check mark (default --ui-icon-primary) |
|
--ui-tile-selected-background |
Fill of the chosen tile (default --ui-card-background; Kanok: soft highlight wash) |
|
--ui-tile-mark-space |
End padding kept clear for the mark (default 48px, Carbon's); e.g. 16px lets the content run under the mark in the top-end corner (beyond Carbon) | |
--ui-checkbox-border |
Border of the box (default --ui-ink; Kanok: --ui-ink-subtle) |
|
--ui-checkbox-checked |
Fill and border when checked or mixed (default --ui-ink; Kanok: --ui-primary) |
|
--ui-checkbox-mark |
Check mark colour (default --ui-icon-inverse; Kanok: --ui-on-primary) |