Checkbox

One independent yes/no. Not one of a set — that is radio. Several boxes that answer one question go in a checkbox-group.

Checkbox

indeterminate and a group's all box are the two things here that need Alpine and the core script. Everything else on this page works in a plain Blade view with no JavaScript at all.

Use it

<x-ds::checkbox name="terms" label="I agree to the terms" />

Props

PropTypeDefaultWhat it does
labelstring | slotrequiredThe clickable label. A label slot holds markup, such as a link.
helpstring | slot—Guidance under the label. A help slot holds markup.
errorstring—Validation message.
checkedboolfalse
indeterminateboolfalseThe "some children are checked" state.
hideLabelboolfalseKeeps label as the accessible name but does not show it. Only inside a table whose row already names the thing.
disabledboolfalse

name, value, wire:model and anything else pass through to the input.

Pass label (and help) as a slot when it holds markup. Strings keep working. The box sits on the label's first line however far the label wraps.

<x-ds::checkbox name="terms">
    <x-slot:label>I agree to the <a href="{{ route('terms') }}" class="underline">terms</a></x-slot:label>
</x-ds::checkbox>

A link inside a label is its own target: pressing it follows the link and does not tick the box.

The label is the touch target

label is required and renders a real <label for>, so its whole width is clickable. On a phone that is most of the target — the 18px box on its own is well under the 44px minimum.

Hidden label

hideLabel is for one place: a select column in a table whose row already names the thing. "Select Invoice 42" written out beside "Invoice 42" says it twice, so the label stays in the DOM as the control's accessible name, and is not shown.

<x-ds::table>
    <thead>
        <tr>
            <th scope="col" class="w-12 px-4 py-3">
                <x-ds::checkbox label="Select all" :hideLabel="true" wire:model.live="selectAll" />
            </th>
            <th scope="col" class="px-4 py-3 text-left">Invoice</th>
        </tr>
    </thead>
    @foreach ($invoices as $invoice)
        <x-ds::table-row>
            <x-ds::table-cell>
                <x-ds::checkbox :label="'Select '.$invoice->number" :hideLabel="true"
                                wire:model.live="selected" :value="$invoice->id" />
            </x-ds::table-cell>
            <x-ds::table-cell>{{ $invoice->number }}</x-ds::table-cell>
        </x-ds::table-row>
    @endforeach
</x-ds::table>

Write the label as the action and the thing: "Select Invoice 42", not "Select" on every row. A screen reader moving down the column hears the label alone, so forty rows of "Select, checkbox" is forty identical controls.

With the visible label gone, so is most of the touch target. The hidden label therefore still takes presses, as a 44px square centred on the box, behind it: a press on the box reaches the input, a press beside it reaches the label. Keep the cell at least that wide (w-12) so the square does not overlap the next column.

Everywhere else, show the label. A checkbox in a form with no visible label is a question with no words.

Examples

With Laravel validation:

<x-ds::checkbox
    name="terms"
    label="I agree to the terms"
    :checked="old('terms')"
    :error="$errors->first('terms')"
/>

A "select all" that reflects a partial selection:

<x-ds::checkbox
    name="all"
    label="Select all"
    :checked="$allSelected"
    :indeterminate="$someSelected && ! $allSelected"
/>

indeterminate is a DOM property with no attribute form, so the component renders x-ds-indeterminate, and the core script sets the property from it — which needs Alpine and an x-data scope above it (a Livewire component is one). A Livewire morph that drops the attribute clears the mixed state, and one that adds it sets it, so the box follows the server. With no script the box shows unchecked, which is what it posts and exactly what a screen reader announces: the feature is absent rather than lying about the state.

checkbox-group

Several boxes that answer one question: a fieldset whose legend is the question, with the group's help and error said as it is entered.

<x-ds::checkbox-group label="It can" all="Everything" :error="$errors->first('scopes')">
    @foreach ($scopes as $value => $label)
        <x-ds::checkbox name="scopes[]" :value="$value" :label="$label" :checked="in_array($value, old('scopes', []))" />
    @endforeach
</x-ds::checkbox-group>
PropTypeDefaultWhat it does
labelstringrequiredThe question. Becomes the legend.
hideLabelboolfalseKeep label as the accessible name only.
helpstring—Guidance under the legend. Replaced by error, never stacked with it.
errorstring—The group's validation message ("Choose at least one"), under the boxes, tied to the fieldset by aria-describedby.
allstring—The label of a box above the others that sets them all and shows checked, unchecked or mixed as they change.

The all box has no name, so it never posts; each box posts as it is checked. It needs the core script, so it is drawn hidden and shown when the script runs: with no script there is no all box, and the boxes still work and post. A form reset puts the boxes back natively and the all box is worked out again.

The same sync anywhere: x-ds-check-all

The group's sync is a directive you can put on any container, such as a table's tbody, where a fieldset cannot go. Mark the box that sets the others data-ds-check-all="key" and the boxes it sets data-ds-check-item="key"; the key pairs them, so one container holds several sets (a column per role):

<tbody x-data x-ds-check-all>
    <tr><td><x-ds::checkbox label="Editors can do everything" :hideLabel="true" data-ds-check-all="editor" /></td></tr>
    <tr><td><x-ds::checkbox name="editor[]" value="edit" label="Editors can edit" :hideLabel="true" data-ds-check-item="editor" /></td></tr>
</tbody>

The boxes it sets dispatch nothing; the all box's own change bubbles on to the form, after the boxes are set, so a save bar compares them as they are left.

Accessibility

  • The real <input type="checkbox"> is styled with appearance-none, not replaced by a decorated <span>. That keeps the browser's focus behaviour and Windows high-contrast rendering, both of which the substitution trick loses invisibly.
  • error sets aria-invalid and wires aria-describedby.
  • disabled uses the real attribute, so the field genuinely does not submit.

Don't

  • Don't 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.
  • Don't use it as a switch. A switch applies immediately; a checkbox is submitted. If it takes effect on click it needs a different control.
  • Don't rely on a table column header as the label. The header is not programmatically associated, and the touch target collapses to 18px. In a select column, give each box its own label and hideLabel — see above.
  • Don't use hideLabel outside a table row. Something visible has to say what the box is.
  • Don't attach a group-level error to one checkbox. See radio.

More in specs/checkbox.md.


From the package repo: edit this page on GitHub.

Was this page helpful?