1. Components
  2. switch

Switch

An on/off control for settings that apply right away.

Demo

<div class="flex flex-wrap items-center gap-4">
    <x-ui.switch name="wifi" checked aria-label="Wi-Fi" />
    <x-ui.switch name="bluetooth" aria-label="Bluetooth" />
    <x-ui.switch name="airplane" checked disabled aria-label="Airplane mode" />
</div>

Installation

Shell
php artisan flexi:add switch

The switch is a native checkbox with role="switch" : it submits with a form, works with wire:model and needs no JavaScript.

Use a switch for a setting that takes effect at once, and a checkbox for a choice confirmed by a submit button.

With a label

label wraps the switch and its text in one <label> : the whole line toggles it, no id needed. description adds a second line, linked with aria-describedby .

<div class="flex flex-col gap-4">
    <x-ui.switch name="dark_theme" label="Use dark theme" />
    <x-ui.switch name="digest" checked label="Weekly digest" description="A summary of your projects every Monday." />
    <x-ui.switch name="beta" disabled label="Beta features" description="Not available on your plan." />
</div>

Settings list

reverse puts the text first and the switch at the end of the line.

<div class="w-full max-w-md divide-y divide-border rounded-ui border border-border bg-card">
    <div class="p-4">
        <x-ui.switch reverse name="email_notifications" checked label="Email notifications"
            description="Receive updates about your account." />
    </div>
    <div class="p-4">
        <x-ui.switch reverse name="push_notifications" label="Push notifications"
            description="Get notified on your devices." />
    </div>
    <div class="p-4">
        <x-ui.switch reverse name="marketing_emails" label="Marketing emails"
            description="News, offers and product announcements." />
    </div>
</div>

Sizes

<div class="flex flex-col gap-4">
    <x-ui.switch size="xs" name="size_xs" checked label="Extra small" />
    <x-ui.switch size="sm" name="size_sm" checked label="Small" />
    <x-ui.switch name="size_md" checked label="Medium" />
    <x-ui.switch size="lg" name="size_lg" checked label="Large" />
</div>

Colors

track colors the switch when it is on. It takes title , muted or any theme color: success , warning , destructive …

<div class="flex flex-wrap items-center gap-4">
    <x-ui.switch name="color_primary" checked aria-label="Primary" />
    <x-ui.switch name="color_success" checked track="success" aria-label="Success" />
    <x-ui.switch name="color_warning" checked track="warning" aria-label="Warning" />
    <x-ui.switch name="color_destructive" checked track="destructive" aria-label="Destructive" />
    <x-ui.switch name="color_title" checked track="title" knob="bg" aria-label="Title" />
</div>

Outlined

<div class="flex flex-wrap items-center gap-4">
    <x-ui.switch outlined name="outlined_off" aria-label="Outlined, off" />
    <x-ui.switch outlined name="outlined_on" checked aria-label="Outlined, on" />
    <x-ui.switch outlined name="outlined_quiet" bg="bg" track="transparent" knob="muted" knob-checked="primary"
        aria-label="Quiet outlined, off" />
    <x-ui.switch outlined name="outlined_quiet_on" checked bg="bg" track="transparent" knob="muted" knob-checked="primary"
        aria-label="Quiet outlined, on" />
</div>

With Livewire

<x-ui.switch wire:model.live="notifications" label="Notifications" />

Attributes you pass, wire:model included, go to the input.

References

Switch props

Prop Description
name , value , id (string) Passed to the input.
checked (boolean) On at first.
disabled (boolean) Can't be toggled.
label (string) Text next to the switch. The slot can replace it.
description (string) Second line under the label.
reverse (boolean) Text first, switch at the end of a full-width line.
size (xs|sm|md|lg) Default: md .
outlined (boolean) Adds a ring around the switch.
bg (string) Color when off. Default: border-strong (muted when outlined).
track (string) Color when on. Default: primary .
knob (string) Knob color. Default: white .
knob-checked (string) Knob color when on. Default: --switch-knob-checked-color (the theme's --primary-foreground ) on the theme track, so it stays visible on a light primary; the knob color otherwise.

Color props take bg , surface , muted , title , neutral , white , transparent or a theme color name such as success .

Styling

Classes go to the input. A bg-* class replaces the off color, a text-* color replaces the on color, and these custom properties tune the knob:

Property Description
--knob-bg Knob color.
--knob-bg-checked Knob color when on.
--switch-knob-checked-color Theme token: knob color when on, on the default track. Every theme sets it to var(--primary-foreground) .
--knob-size , --knob-width , --knob-height Knob size.
--knob-radius Knob radius. Default: 9999px .
--toggle-radius Switch radius. Default: 9999px .
<x-ui.switch name="square" class="bg-muted text-success [--toggle-radius:6px] [--knob-radius:4px]" aria-label="Square switch" />

Accessibility

  • The input has role="switch" : screen readers announce "on" and "off".
  • Without label , give the switch an aria-label or point a <label for> at its id .
  • Space toggles it; the focus ring shows only for keyboard focus.
  • The knob stops sliding when the system asks for reduced motion.