Switch
A switch that turns a setting on or off with one click, for on/off settings that take effect at once.
zb:switch zubazar/ui · free (MIT)
Basic usage
Give the text with label or inside the component; add checked to start it on.
<zb:switch name="sw_notifications" label="Order notifications" checked />
<zb:switch name="sw_newsletter">Email newsletter</zb:switch>
| Prop | Values |
|---|---|
label | Text next to the switch |
checked | Starts it on |
Description
description adds a short, muted explanation below the label.
<zb:switch name="desc_sms" label="SMS notification" description="An SMS is sent to the customer when an order ships." checked />
<zb:switch name="desc_2fa" label="Two-step verification" description="Team members confirm every sign-in with an SMS code." />
| Prop | Values |
|---|---|
description | Description below the label |
Sizes
Choose sm or md (default) with size; sm suits tight places such as table rows.
<zb:switch name="size_sm" size="sm" label="Small" checked />
<zb:switch name="size_md" label="Medium" checked />
| Prop | Values |
|---|---|
size | sm | md |
Disabled
disabled locks the switch and dims it.
<zb:switch name="disabled_on" label="Automatic backup (enabled by an administrator)" checked disabled />
<zb:switch name="disabled_off" label="Bulk SMS (package required)" disabled />
| Prop | Values |
|---|---|
disabled | Cannot be changed and looks dimmed |
Value and Livewire
Bind it to a boolean property with wire:model; for switches bound to one array, give each its own value.
<zb:switch wire:model.live="notificationsOn" label="Order notifications" />
@foreach (['sms' => 'SMS', 'email' => 'Email'] as $channel => $name)
<zb:switch wire:model="channels" :value="$channel" :label="$name" />
@endforeach
| Prop | Values |
|---|---|
value | Value submitted when on (1) |
wire:model | Boolean or array property |
Props
The values the component takes. What are props and slots?
| Prop | Type | Default | Description |
|---|---|---|---|
label | — | null | Text next to the switch; otherwise the slot content |
description | — | null | Description below the label |
name | — | null | Field name; otherwise the wire:model value |
checked | boolean | false | Starts it on |
size | — | md | sm · md |
value | number | 1 | Value submitted when on (attribute) |
input-class | — | null | Classes of the hidden checkbox itself |
Slots: Default slot: the label text when label is not given. class, style, x-show and wire:key go on the whole switch; other attributes such as disabled, wire:model, aria-* and data-* are added to the checkbox. A single switch shows its validation error below its label; switches in a list (name[] or bound to an array) only turn red.
Developer notes
- When on, the value sent is
1; when off, a classic form sends nothing.
Accessibility
- ARIA and roles
- The switch is a visually hidden real checkbox with
role="switch", so screen readers announce it as on or off. - Keyboard and focus
- Space toggles it (native checkbox behaviour). Keyboard focus shows an outline around the track.
- Label
labelanddescriptionsit inside the same<label>, so both are read as the switch name. The id is built from the field name.- Error
- With an error the checkbox gets
aria-invalid="true"and the message is linked witharia-describedby. Your ownaria-describedbyis kept. - What you provide
- Give a switch without a label (in a table cell, for example) an
aria-label; it goes on the checkbox.disabledalso goes there as the real attribute.