Checkbox and radio
A checkbox (zb:checkbox) and a radio button (zb:radio) in the brand color.
zb:checkbox · zb:radio zubazar/ui · free (MIT)
Checkbox
Pass the label with label, and checked to have the box checked initially.
<div class="flex flex-col gap-3">
<zb:checkbox name="email_notification" label="Email notification" checked />
<zb:checkbox name="sms_notification" label="SMS notification" />
</div>
| Prop | Values |
|---|---|
label | Text next to the box |
checked | true | false |
Radio button
Give the buttons in one group the same name and each a different value; only one of the group can be selected.
<div class="flex flex-wrap gap-6">
<zb:radio name="customer_type" value="individual" label="Individual" checked />
<zb:radio name="customer_type" value="corporate" label="Corporate" />
</div>
| Prop | Values |
|---|---|
name | Group name |
value | Value sent when selected |
checked | true | false |
With Livewire
Bind a single box to a bool property with wire:model, and several boxes bound to the same property to an array.
<zb:checkbox wire:model="active" label="Active customer" />
<zb:checkbox-group label="Shipping days" name="days">
<zb:checkbox wire:model="days" value="monday" label="Monday" />
<zb:checkbox wire:model="days" value="wednesday" label="Wednesday" />
<zb:checkbox wire:model="days" value="friday" label="Friday" />
</zb:checkbox-group>
<zb:radio wire:model="priority" value="normal" label="Normal" />
<zb:radio wire:model="priority" value="urgent" label="Urgent" />
Groups
Gather several boxes under one title with zb:checkbox-group or zb:radio-group; inline lays them side by side.
<div class="grid gap-8 sm:grid-cols-2">
<zb:checkbox-group label="Categories" name="categories" description="Where the product is listed." required>
<zb:checkbox name="categories[]" value="audio" label="Audio and headphones" checked />
<zb:checkbox name="categories[]" value="wearables" label="Wearable technology" />
<zb:checkbox name="categories[]" value="home" label="Smart home" />
</zb:checkbox-group>
<zb:radio-group label="Shipping" name="shipping" inline>
<zb:radio name="shipping" value="standard" label="Standard" checked />
<zb:radio name="shipping" value="express" label="Express" />
<zb:radio name="shipping" value="store" label="Store pickup" />
</zb:radio-group>
</div>
| Prop | Values |
|---|---|
label | The group's title (legend) |
name | The key of the error message |
description | The text under the title |
inline | Lays the boxes side by side |
required | Adds an asterisk to the title |
Props
The values the component takes. What are props and slots?
| Prop | Type | Default | Description |
|---|---|---|---|
checkbox: label | — | null | Text next to it; the slot is used when not given |
checkbox: name | — | null | Field name; defaults to the wire:model value |
checkbox: checked | boolean | false | Initially checked |
checkbox: value (attribute) | number | 1 | Value sent while checked; the id becomes name-value |
radio: label | — | null | Text next to it; the slot is used when not given |
radio: name | — | null | Group name; defaults to the wire:model value |
radio: value | — | null | Value sent when selected; the id becomes name-value |
radio: checked | boolean | false | Initially selected |
checkbox / radio: input-class | — | null | Classes of the box or button itself. class, style, x-show and wire:key go on the outer element; name, wire:model, aria-*, data-* and events on the box |
checkbox-group / radio-group: label | — | null | The group's title (fieldset legend) |
checkbox-group / radio-group: name | — | null | The message key; shown once under the boxes (name and name.*) |
checkbox-group / radio-group: description | — | null | The text under the title |
checkbox-group / radio-group: inline | boolean | false | Lays the boxes side by side |
checkbox-group / radio-group: required | boolean | false | Adds an asterisk to the title and "(required)" for screen readers |
Slots: default (label content when label is not given)
Advanced
Rich label
Without label, the slot becomes the label; use it for formatted content such as a link. Wrap the content in a single <span> so the gap between it and the box is not broken.
<zb:checkbox name="privacy_consent">
<span>I have read and accept the <a href="#" class="font-medium text-primary-ink hover:underline">privacy notice</a>.</span>
</zb:checkbox>
Disabled
The disabled attribute goes straight to the box; a disabled element is drawn faded and cannot be clicked. On radio buttons the label turns gray as well.
<div class="flex flex-wrap gap-10">
<div class="flex flex-col gap-3">
<zb:checkbox name="auto_backup" label="Automatic backup" checked disabled />
<zb:checkbox name="bulk_sms" label="Bulk SMS (no permission)" disabled />
</div>
<div class="flex flex-col gap-3">
<zb:radio name="delivery_method" value="shipping" label="Standard shipping" checked />
<zb:radio name="delivery_method" value="same_day" label="Same-day delivery (not available in this area)" disabled />
</div>
</div>
| Prop | Values |
|---|---|
disabled | Attribute |
Developer notes
- The array holds the
valuevalues of the checked boxes; for radio buttons the group name comes fromwire:model. - A single checkbox shows its validation error inside its label, below the text (the root stays the
<label>). Boxes in a list and radio buttons only turn red; the group component shows the list's message once. - The group's validation message (
categoriesorcategories.*) appears once under the boxes; the boxes get a red border.
Accessibility
- Keyboard
- Both are real
<input>elements. Space toggles a checkbox, and arrow keys move between radios that share a name. This is native browser behaviour. - Label
- The label is tied to the box with
<label for>, so clicking the label toggles it too. The id is built from the name and the value, so boxes in one list get different ids. - Errors and groups
- With an error the box gets
aria-invalid="true"; a single checkbox also links its message witharia-describedby.zb:checkbox-groupandzb:radio-grouprender a<fieldset>with a<legend>. The group description and message are linked to the fieldset witharia-describedby, andrequiredadds a hidden "(required)" to the legend. - Focus and disabled
- Keyboard focus shows an outline around the box (
focus-visible).disabledgoes to the input as the real attribute. - What you provide
- Set the
indeterminateproperty ("some selected") yourself in JavaScript; the component only styles it. Give a box without a label (a table row selector, for example) anaria-label. Give radios the samenameorwire:modelso they form one group.