v0.0.2
Demos (demo panel, opens in a new tab)

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

Basic usage: view
BLADE

Give the text with label or inside the component; add checked to start it on.

blade
<zb:switch name="sw_notifications" label="Order notifications" checked />
<zb:switch name="sw_newsletter">Email newsletter</zb:switch>
PropValues
labelText next to the switch
checkedStarts it on

Description

Description: view
BLADE

description adds a short, muted explanation below the label.

blade
<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." />
PropValues
descriptionDescription below the label

Sizes

Sizes: view
BLADE

Choose sm or md (default) with size; sm suits tight places such as table rows.

blade
<zb:switch name="size_sm" size="sm" label="Small" checked />
<zb:switch name="size_md" label="Medium" checked />
PropValues
sizesm | md

Disabled

Disabled: view
BLADE

disabled locks the switch and dims it.

blade
<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 />
PropValues
disabledCannot be changed and looks dimmed

Value and Livewire

BLADE

Bind it to a boolean property with wire:model; for switches bound to one array, give each its own value.

blade
<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
PropValues
valueValue submitted when on (1)
wire:modelBoolean or array property

Props

The values the component takes. What are props and slots?

PropTypeDefaultDescription
label—nullText next to the switch; otherwise the slot content
description—nullDescription below the label
name—nullField name; otherwise the wire:model value
checkedbooleanfalseStarts it on
size—mdsm · md
valuenumber1Value submitted when on (attribute)
input-class—nullClasses 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

Events, hooks and details
  • 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
label and description sit 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 with aria-describedby. Your own aria-describedby is kept.
What you provide
Give a switch without a label (in a table cell, for example) an aria-label; it goes on the checkbox. disabled also goes there as the real attribute.
Zubazar UI · ZubazarDesigned for people.