Forms

Switch

An on/off switch with its label, for a setting that takes effect right away: notifications, dark mode, a feature flag. It is a checkbox underneath, with role="switch". While a Livewire request it started is running, it shows a spinner and ignores clicks.

A summary every Monday.

Notify: false
Blade

A pill-shaped track with a round knob, followed by the label and the description. Clicking it, its label, or pressing Space slides the knob and sets $notify to true. While that request runs, a small spinner shows in the knob and the switch cannot be flipped again; it appears only if the request takes longer than 150 ms.

State labels #

"On" or "Off" next to the switch, following its state, with "Dark mode" as its accessible name.

Dark: false
Blade

An action on change #

Each change calls toggleAdmin; the switch is blocked with a spinner until it answers.

Ana Souza is admin: no
Blade

Turning on all at once #

A "Mark all" switch on top: turning it on turns on every enabled switch in the same form, and it shows on when all of them are on. group limits it to switches with the same group, anywhere on the page.

Blade

A group bound to an array #

With group, tk:switch.all reaches only the switches of that group, anywhere on the page. Switches bound to one array add their value to it.

Channels: email
Blade

Colors and sizes #

color fills the track while on and size scales the switch and its label.

Blade

Icons in the knob #

icon-on and icon-off put an icon in the knob that follows its state.

Blade

Alignment and disabled #

align places the switch before the label, right after it, or at the ends of the row; disabled blocks it.

Blade

Customization #

Classes go to the <input>; prefixed attributes reach the track (control:), the knob (icon:) and the state texts (label-checked:, label-unchecked:).

Blade

Reference

tk:switch
tk:switch.all

tk:switch

Prop Type Default Description
checked bool | array false Turns the switch on at the first render. An array turns it on when it contains the switch's value.
labelOn string null Text shown next to the switch while it is on.
labelOff string null Text shown while it is off.
iconOn string null An icon in the knob while on.
iconOff string null An icon in the knob while off.
align string left Where the switch sits next to the label: left, right, justify-left or justify-right, as on tk:checkbox.
loading bool auto Shows a spinner and blocks the switch while its request runs. On with wire:model.live or wire:change.
loadingDelay int | string 150 Milliseconds (or 300ms, 1s) before the spinner shows.
loadingMinDuration int | string 700 Milliseconds the spinner stays at least, once shown, so it does not flash.
group string null Puts the switch in a set controlled by the tk:switch.all with the same group.

Also accepts the field props (label, description, help, value, size, color...). wire:model and x-model bind a bool, or an array of values when several switches share the property.

Slots

Slot Description
default The label, in place of label.

Prefixes

Prefix Goes to
icon-on:, icon-off: The icons in the knob.
label-checked:, label-unchecked: The labelOn and labelOff texts.
control: The track.
icon: The knob.
loading: The spinner shown while its request runs.
state: The labelOn and labelOff texts beside the track.
state-group: The element holding the track and the state texts, present with labelOn or labelOff.

tk:switch.all

Prop Type Default Description
label string | slot Mark all The label, translated.
group string null Controls the switches with this group. Without it, those with no group in the same form, or else in the same Livewire component, or else on the page.

Also accepts the field props and the iconOn, iconOff, labelOn, labelOff, loadingDelay and loadingMinDuration props of tk:switch.

Customization

Classes and attributes on the tag go to the <input>. The parts around the field take the prefixes of the field props; the switch itself takes (listed in the Reference above).

blade

Accessibility

A native checkbox with role="switch", so screen readers announce "on" and "off". Without a label it is named "Switch" (translated); give it a label or an aria-label. The state labels are hidden from screen readers, so the name does not change with the state. While blocked by a request it has aria-busy and aria-disabled.

Key Action
Space Flips the switch.

Behavior notes

  • Old input from a failed submit restores the state.
  • tk:switch.all skips disabled switches and dispatches change on each switch it flips, so wire:model on them updates. It shows on when all of them are on and follows them after a Livewire update, a form reset or an x-model value set from code.