checkbox
checked is false
import { state } from '@markless/core';import { checkbox } from '@markless/ui';export default function CheckboxBasic() @{ let ticked = state(false); <checkbox.root class="row" name="terms" checked={ticked} onChange={(next) => { ticked = next === true; }} > <checkbox.trigger class="box"> <checkbox.indicator class="tick">✓</checkbox.indicator> </checkbox.trigger> <checkbox.label class="label">I accept the terms</checkbox.label> <checkbox.description class="hint">Read them before you tick this.</checkbox.description> <p class="note"> checked is <span class="code">{ticked ? 'true' : 'false'}</span> </p> <checkbox.field /> </checkbox.root>;}
.row { display: grid; grid-template-columns: auto 1fr; gap: 0.25em 0.75em; align-items: center; box-sizing: border-box; width: min(22rem, 100%); font-family: 'Shantell Sans', system-ui, sans-serif; font-size: var(--step--1); line-height: 1.5; color: var(--ink);}.box { display: grid; place-items: center; box-sizing: border-box; width: 1.75em; height: 1.75em; padding: 0; border: 1px solid var(--ink); border-radius: 6px; background: var(--paper); color: var(--ink); cursor: pointer; transition: background 140ms ease;}.box:hover { background: color-mix(in srgb, var(--purple) 16%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.box[ui-checked] { background: color-mix(in srgb, var(--purple) 26%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.box:focus-visible { outline: 2px solid var(--ink); outline-offset: 4px;}.box:disabled { cursor: not-allowed;}.tick { font-family: 'Shantell Sans', system-ui, sans-serif; font-size: 1.15em; line-height: 1;}.label { font-family: 'Shantell Sans', system-ui, sans-serif; font-size: var(--step-0); cursor: pointer;}.row:has(:required) .label::after { content: ' (required)'; color: var(--muted-ink); font-size: var(--step--1);}.row[ui-disabled] { opacity: 0.55;}.row[ui-disabled] .label { cursor: not-allowed;}.hint,.note { grid-column: 2; margin: 0; color: var(--muted-ink); font-family: 'Shantell Sans', system-ui, sans-serif;}.code { color: var(--ink); font-family: var(--font-mono);}@media (prefers-reduced-motion: reduce) { .box { transition: none; }}
Click the box or its words. The line under it shows the checked value.
Overview
A checkbox is one yes-or-no choice with a label. It can also be mixed, for a box that stands over a list where only some rows are ticked. For a whole list with a select-all box, use checklist.
The box you click is a <button role="checkbox">, not a native input. A form never sees that
button. If the choice has to reach a form, add checkbox.field. It renders a hidden native
checkbox for the form to read.
Anatomy
The checkbox itself; the trigger, label, description, error and field parts go inside it.
checkbox.label is a real <label> tied to the trigger, so a click on the words toggles the box.
checkbox.indicator holds your tick. Its children show only while the box is on or mixed.
The checked value
checked on the root is the starting value. Leave it out and the box starts off. Each click calls
onChange with the new value, so your own state can follow it.
<checkbox.root>…</checkbox.root>
<checkbox.root checked>…</checkbox.root>The mixed value
Set checked="mixed" for a box that stands over a part-ticked list. The trigger reports
aria-checked="mixed", and checkbox.field sets indeterminate on its native input.
import { state } from '@markless/core';import { checkbox } from '@markless/ui';export default function CheckboxMixed() @{ let tea = state(true); let cake = state(false); <div class="list"> <checkbox.root class="row all" checked={tea && cake ? true : tea || cake ? 'mixed' : false} onChange={(next) => { tea = next === true; cake = next === true; }} > <checkbox.trigger class="box"> <checkbox.indicator class="tick">✓</checkbox.indicator> </checkbox.trigger> <checkbox.label class="label">Order both</checkbox.label> </checkbox.root> <checkbox.root class="row" checked={tea} onChange={(next) => { tea = next === true; }} > <checkbox.trigger class="box"> <checkbox.indicator class="tick">✓</checkbox.indicator> </checkbox.trigger> <checkbox.label class="label">Tea</checkbox.label> </checkbox.root> <checkbox.root class="row" checked={cake} onChange={(next) => { cake = next === true; }} > <checkbox.trigger class="box"> <checkbox.indicator class="tick">✓</checkbox.indicator> </checkbox.trigger> <checkbox.label class="label">Cake</checkbox.label> </checkbox.root> </div>;}
.list { display: grid; gap: 0.6em; width: min(16rem, 100%); font-family: 'Shantell Sans', system-ui, sans-serif; font-size: var(--step--1); line-height: 1.5; color: var(--ink);}.row { display: flex; gap: 0.75em; align-items: center; margin-inline-start: 2.35em;}.all { margin-inline-start: 0; padding-block-end: 0.6em; border-block-end: 1px solid var(--ink);}.box { display: grid; place-items: center; box-sizing: border-box; width: 1.75em; height: 1.75em; padding: 0; border: 1px solid var(--ink); border-radius: 6px; background: var(--paper); color: var(--ink); cursor: pointer; transition: background 140ms ease;}.box:hover { background: color-mix(in srgb, var(--purple) 16%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.box[ui-checked],.box[ui-mixed] { background: color-mix(in srgb, var(--purple) 26%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.box:focus-visible { outline: 2px solid var(--ink); outline-offset: 4px;}.tick { font-family: 'Shantell Sans', system-ui, sans-serif; font-size: 1.15em; line-height: 1;}.box[ui-mixed] .tick { display: none;}.box[ui-mixed]::after { content: '–'; font-family: 'Shantell Sans', system-ui, sans-serif; line-height: 1;}.label { font-family: 'Shantell Sans', system-ui, sans-serif; cursor: pointer;}@media (prefers-reduced-motion: reduce) { .box { transition: none; }}
Tick only Tea or only Cake, and Order both turns mixed. Its checked is worked out from the two rows.
A click on a mixed box turns it on. The next click turns it off. A click never leads back to mixed,
so mixed only comes from your checked prop.
A disabled box
disabled on the root disables the trigger button, so a click does nothing and Tab skips it. The
root and the trigger both get ui-disabled.
<checkbox.root checked disabled>
<checkbox.trigger>
<checkbox.indicator>✓</checkbox.indicator>
</checkbox.trigger>
<checkbox.label>Two factor sign in, set by your administrator</checkbox.label>
</checkbox.root>A disabled box sends nothing with its form, even when it is ticked. That is how a native disabled checkbox behaves too.
Sending it with a form
Add checkbox.field to put a hidden <input type="checkbox"> in the form. It takes no props of
its own. It reads name, value and required from the root.
import { state } from '@markless/core';import { checkbox, textbox } from '@markless/ui';export default function CheckboxForm() @{ let sent = state('Nothing sent yet.'); <form class="form" onSubmit={(event) => { event.preventDefault(); const data = new FormData(event.target as HTMLFormElement); sent = `Sent email=${String(data.get('email'))} newsletter=${String(data.get('newsletter'))}`; }} > <textbox.root class="field" name="email" value="ada@example.com"> <textbox.label class="label">Email</textbox.label> <textbox.input class="input" /> </textbox.root> <checkbox.root class="row" required name="newsletter" value="weekly"> <checkbox.field /> <checkbox.trigger class="box"> <checkbox.indicator class="tick">✓</checkbox.indicator> </checkbox.trigger> <checkbox.label class="label">Send me the weekly digest</checkbox.label> <checkbox.description class="hint">Required. One email each Monday.</checkbox.description> </checkbox.root> <button class="save" type="submit">Save</button> <p class="note">{sent}</p> </form>;}
.form { display: grid; gap: 0.8em; justify-items: start; width: min(22rem, 100%); font-family: 'Shantell Sans', system-ui, sans-serif; font-size: var(--step--1); line-height: 1.5; color: var(--ink);}.field { display: grid; gap: 0.3em; width: 100%;}.input { box-sizing: border-box; width: 100%; padding: 0.45em 0.7em; border: 1px solid var(--ink); border-radius: 6px; background: var(--paper); color: var(--ink); font: inherit; font-family: 'Shantell Sans', system-ui, sans-serif;}.input:focus-visible { outline: 2px solid var(--ink); outline-offset: 4px;}.row { display: grid; grid-template-columns: auto 1fr; gap: 0.2em 0.75em; align-items: center;}.hint { grid-column: 2; color: var(--muted-ink); font-family: 'Shantell Sans', system-ui, sans-serif;}.save { padding: 0.45em 0.9em; border: 1px solid var(--ink); border-radius: 6px; background: var(--paper); color: var(--ink); font: inherit; font-family: 'Shantell Sans', system-ui, sans-serif; cursor: pointer; transition: background 140ms ease;}.save:hover { background: color-mix(in srgb, var(--purple) 16%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.save:active { background: color-mix(in srgb, var(--purple) 26%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.save:focus-visible { outline: 2px solid var(--ink); outline-offset: 4px;}.note { margin: 0; color: var(--muted-ink); font-family: var(--font-mono);}.box { display: grid; place-items: center; box-sizing: border-box; width: 1.75em; height: 1.75em; padding: 0; border: 1px solid var(--ink); border-radius: 6px; background: var(--paper); color: var(--ink); cursor: pointer; transition: background 140ms ease;}.box:hover { background: color-mix(in srgb, var(--purple) 16%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.box[ui-checked],.box[ui-mixed] { background: color-mix(in srgb, var(--purple) 26%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.box:focus-visible { outline: 2px solid var(--ink); outline-offset: 4px;}.tick { font-family: 'Shantell Sans', system-ui, sans-serif; font-size: 1.15em; line-height: 1;}.label { font-family: 'Shantell Sans', system-ui, sans-serif; cursor: pointer;}@media (prefers-reduced-motion: reduce) { .box, .save { transition: none; }}
Press Save while the box is off, and the form refuses to send. Tick it and press Save again. The line under Save shows what the form sent.
A ticked box sends its name with its value. Without a value it sends "on", like a native
checkbox. An unticked box and a mixed box send nothing.
Help and error text
checkbox.description is a hint. checkbox.error says what went wrong. The trigger lists both in
aria-describedby, the error first, wherever you place them.
<checkbox.root required>
<checkbox.trigger>
<checkbox.indicator>✓</checkbox.indicator>
</checkbox.trigger>
<checkbox.label>I accept the terms</checkbox.label>
<checkbox.description>Read them before you tick this.</checkbox.description>
<checkbox.error>Please accept the terms to continue.</checkbox.error>
</checkbox.root>Keyboard
| Key | What it does |
|---|---|
| Tab | Move focus to the trigger, or past it. |
| Space | Toggle the box. |
| Enter | Nothing. A checkbox is not a submit button, so the family cancels Enter. |
Styling
| Attribute | On | When |
|---|---|---|
ui-checked |
root, trigger |
The value is true. |
ui-mixed |
root, trigger |
The value is 'mixed'. |
ui-disabled |
root, trigger |
disabled is set. |
The family ships no CSS. The box has no size or border until you give it one.
API reference
checkbox.root
| Prop | Type | What it does |
|---|---|---|
checked |
boolean | 'mixed' |
The starting value. The default is false. |
disabled |
boolean |
Nobody can toggle the box. The trigger button is disabled. |
required |
boolean |
The form will not submit while the box is off. Needs checkbox.field. |
name |
string |
The name checkbox.field submits under. |
value |
string |
What checkbox.field submits. The default is "on". |
onChange |
(checked: boolean | 'mixed') => void |
Called with the new value after each toggle. |
The root renders a <div> with aria-disabled set to true or false. Every other prop goes on
that <div>.
checkbox.trigger
A <button type="button" role="checkbox">. It has no props of its own, and every button prop goes
on the button. Your own onClick and onKeydown run after the family's.
| Attribute | Value |
|---|---|
aria-checked |
true, false or mixed. |
aria-invalid |
true while a checkbox.error is rendered, else false. |
aria-describedby |
The ids of checkbox.error and checkbox.description, error first. |
disabled |
Present while the root is disabled. |
checkbox.indicator
A <span aria-hidden="true">. Its children show while the value is true or 'mixed'. Screen
readers skip it, because the trigger already reports the state.
checkbox.label
A <label> whose for points at the trigger.
checkbox.description
A <div>. The trigger lists it in aria-describedby, after any error.
checkbox.error
A <div>. Rendering it marks the trigger invalid. The trigger lists it first in
aria-describedby.
checkbox.field
A hidden <input type="checkbox"> with tabindex="-1" and aria-hidden="true". It has no props
of its own. It reads name, value, required and disabled from the root, and sets
indeterminate while the value is mixed.