Checkbox
One independent yes/no. Not one of a set — that is radio.md.
Form control. It takes input in a form, so it is drawn on the Every input type block, beside every other control, as it starts and with an error where it has one.
Anatomy
☑ I agree to the terms
At most one email a month. ← help, or error
| Part | Token | Why |
|---|---|---|
| Box | border-strong on surface | 18px (size-4.5), the same mark size the alert uses. |
| Checked | accent + on-accent | The one place accent means "on" rather than "link". |
| Error | border-danger | Plus aria-invalid. |
| Disabled | surface-subtle, fg-subtle fill | Checked-and-disabled stays visibly checked — a locked-on setting must still read as on. |
The native input, styled
appearance-none on the real <input type="checkbox">, not a hidden input beside
a decorated <span>.
The hidden-input trick loses the browser's own focus behaviour, Windows
high-contrast rendering and forced-colors support — and every one of those
failures is invisible in a normal browser on a normal machine. Nobody finds
them until a user does. forced-colors:appearance-auto hands the control back to
the OS palette, where the fill is stripped and the native mark is the only thing
left.
The input and its tick occupy one grid cell, stacked, rather than the tick being absolutely positioned. Absolute positioning against a label that wraps to two lines drifts; a grid cell cannot.
Indeterminate
indeterminate is the "select all" state, where some children are checked.
It is a DOM property. No attribute sets it. The component emits
x-ds-indeterminate, a core directive that sets it, and clears it in its
cleanup. So a Livewire morph follows the server both ways: a morph that drops
the attribute (no longer mixed) runs the cleanup, one that adds it sets the
property again. Until 30B it was x-init="$el.indeterminate = true", which a
morph could set but never clear. A host without Alpine sets it in one line of
its own.
The important part is what the styling keys off: the :indeterminate
pseudo-class — the property itself — and not the prop. So in a host where
nothing sets the property, the box renders unchecked, which is exactly what a
screen reader announces. The feature is absent rather than lying, and a developer
testing it sees that immediately.
Styling from the prop instead would draw a mixed mark on a control reporting "unchecked", and that divergence is invisible until an assistive technology reads it. With no script, then, a mixed box shows unchecked: what it posts, and what a screen reader says.
Label
label is required, and it is a real <label for>, so its whole width is a
click target. On a phone that is most of the touch target — the 18px box on its
own is well under the 44px minimum, and the label is what makes up the
difference.
label and help take a slot as well as a string, for a label that holds a
link. The box's cell is one label line tall (h-[1lh] at text-body), so the
box sits on the label's first line; until 30B the cell stretched to the label
and help together and centred the box against both.
help is replaced by error, never stacked with it. Two lines of guidance under
one control is one too many; the same rule as input.md.
Hidden label
hideLabel keeps the <label for> as the accessible name and hides its text
(sr-only). It exists for a select column in a table, where the row already
names the thing and a visible "Select Invoice 42" would repeat it.
Hiding the text must not shrink the target to the 18px box. The label element
stays, transparent, as a 44px square centred on the box (-inset-[13px]
around 18px), painted behind it with -z-10 inside an isolate cell. Paint
order is hit-test order, so:
- a press on the box lands on the input, and its hover state still fires;
- a press on the margin lands on the label and toggles the input through
for; isolatekeeps the negative z-index inside the cell, so the square never sinks behind the row it sits in.
The cell loses its pt-0.5 in this mode: that padding aligns the box with the
first line of a visible label, and with no line to meet it would push the box
off the centre of its own target.
help and error still render visibly when given. A hidden label is not a
hidden message.
checkbox-group
A fieldset and legend, as radio-group: the legend is the question,
said as the group is entered. help sits under the legend, error under the
boxes (help replaced, never stacked), both named by the fieldset's
aria-describedby; a fieldset takes no aria-invalid.
all draws a box above the others (no name: it never posts) that sets every
enabled box and shows checked, unchecked or mixed as they change. The sync is
the core directive x-ds-check-all on the fieldset; the group marks each box
in its slot data-ds-check-item (on the type="checkbox" every ds::checkbox
writes, as chip-group reaches its chips). The all box is drawn hidden and
shown by the directive: without the script it would do nothing, so it is not
there. The boxes the directive sets dispatch nothing; the all box's own
change bubbles on after they are set, so a listener on the form sees them as
they are left. A form reset puts the boxes back natively, and the all box
is worked out again after it.
The directive is usable on its own (a table's tbody, where a fieldset cannot
go), with data-ds-check-all="key" and data-ds-check-item="key" pairing sets.
Accessibility
aria-describedbywires help or error to the input.errorsetsaria-invalid="true".- Disabled uses the real
disabledattribute — a checkbox is a form control, and a disabled one should genuinely not submit. - The focus ring is on the box,
focus-visibleonly.
Do not
- Do not use one for a two-way choice. "Send as HTML / Send as plain text" is a radio pair; a checkbox makes the reader work out what unchecked means.
- Do not use a checkbox as a switch. A switch applies immediately; a checkbox
is submitted. If it takes effect on click, it needs
role="switch"and a different component. - Do not omit the label and rely on a table column header. The header is not
programmatically associated, and the touch target collapses to 18px. Use
hideLabelwith a per-row label instead. - Do not use
hideLabeloutside a table row that already names the thing. - Do not put the error on the box in a group of checkboxes — it belongs to the group. See radio.md.
From the package repo: edit this page on GitHub.