checkbox

Read them before you tick this.

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>;}

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

checkbox anatomy
Anatomy perspectiveFlat
Layers
✓Send me the weekly digestGood things, once a week.
checkbox.root

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>;}

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.

Required. One email each Monday.

Nothing sent yet.

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>;}

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.