<x-widget.switch>
Switch Blade component for Laravel and Livewire
An on/off switch that submits like a checkbox and is announced as a switch. Three sizes, an optional tick and cross, before or after its text, or as a card. Works with Livewire wire:model.
php artisan bladewell:add switch
- Also adds
- Icon
- Works with
- Laravel 12 and 13, Livewire 3 and 4, Tailwind CSS 4.1+, PHP 8.3+
Usage
Livewire
In a Livewire component, bind a bool with wire:model (deferred) or wire:model.live: public bool $updates = false. No name is needed. With .live each flip reaches the server at once, so a settings page can save it in updatedUpdates() with no Save button. The switch shows the property after every render, so turning it on or off in PHP flips it.
<div class="space-y-4">
<x-widget.switch label="Email me updates" wire:model.live="updates" />
<x-widget.switch label="Dark mode" description="Follows your system until you choose." wire:model="darkMode" />
</div>
On the server
The switch can't stop a request that skips it. Here's what to check on your server.
Security
-
It submits like a checkbox: switched off, it sends nothing. Read it with
$request->boolean(), or give itunchecked-value="0", and validate it withboolean. -
A switch that changes who can see or do something (a public profile, an admin flag) needs a policy check as well as validation, and shouldn't be fillable from any other form.
Examples
Switch
A checkbox with role="switch", so screen readers announce on and off. It submits like a checkbox.
Show code Hide code
<div class="divide-line flex max-w-md flex-col divide-y">
<x-widget.switch name="alerts" label="Withdrawal alerts" description="Email me when a withdrawal completes." checked class="py-3" />
<x-widget.switch name="two_factor" label="Two-factor authentication" class="py-3" />
<x-widget.switch name="beta" label="Beta features" disabled class="py-3" />
</div>
Switch variants
size is sm, md (default) or lg. icons puts a tick or cross in the knob, so on and off don't rely on colour alone. placement="start" puts the switch before the text, like a checkbox. variant="card" makes the whole row a tile that lights up while on.
Show code Hide code
<div class="flex flex-col gap-8">
<div class="flex flex-col gap-4">
<x-widget.switch name="compact" label="Small" size="sm" checked icons placement="start" />
<x-widget.switch name="default_size" label="Medium" checked icons placement="start" />
<x-widget.switch name="large" label="Large" size="lg" icons placement="start" />
</div>
<div class="grid gap-3 sm:grid-cols-2">
<x-widget.switch name="addons[]" value="backup" label="Daily backups" description="Kept for 30 days" variant="card" checked />
<x-widget.switch name="addons[]" value="support" label="Priority support" description="Replies within 2 hours" variant="card" />
</div>
</div>
Props
Other attributes, such as autocomplete or data-*, are passed through to the element. class styles the component's outer wrapper.
<x-widget.switch>
| Prop | Default | Description |
|---|---|---|
| name |
null
|
What it submits as; its error and old input are found under it. Optional with wire:model, which then names it. |
| id |
null
|
Defaults to one made from the name (or the wire:model property). |
| label |
null
|
Beside the switch, and its name for screen readers. |
| description |
null
|
Smaller text under the label. |
| value |
'1'
|
What it submits when on. |
| checked |
false
|
On to start with. Old input wins after a failed submit; with wire:model, the bound property. |
| unchecked-value |
null
|
Also submits this when off (e.g. 0), so the field is always in the request. |
| error |
null
|
An error message of your own; otherwise the validation error for the name, from the session or Livewire. |
| info |
null
|
A hint under the field. |
| bag |
'default'
|
Which error bag to read the error from. |
| disabled |
false
|
Greyed out: it can't be changed. |
| readonly |
false
|
Shown, and submitted, but it can't be changed. With disabled, disabled wins. |
| size |
'md'
|
sm, md or lg. |
| placement |
'end'
|
end (label first, switch after it, for settings rows) or start (switch first, like a checkbox). |
| variant |
'default'
|
default, or card: a bordered box that highlights when on. |
| icons |
false
|
A tick when on and a cross when off, so the state doesn't rest on colour alone. |
Accessibility
All 2 examples above are checked with axe-core against the WCAG 2.2 A and AA rules, in the light theme and the dark one, both as the page draws and with each popover, dialog, toast and tooltip opened, on every change. A change that fails can't be merged. Where axe can't decide, such as contrast on SVG text, the test measures the colours itself instead of letting it pass.
Automated checks can't judge everything: how it sounds in a screen reader, and how it feels to use from the keyboard, still need a person. Check those on your own pages too.
Source
What bladewell:add switch writes to your app with the default namespaces. Prefer to copy by hand? Take these files, plus the ones from
the components it also adds and Field, and the theme and base CSS.
resources/views/components/widget/switch/index.blade.php Show
app/View/Widget/ElementIds.php Show
app/View/Widget/FormField.php Show