Swap
A button that cross-fades between an 'on' and 'off' named slot (e.g. two icons) with an accessible role="switch" toggle.
Preview
<div class="flex items-center gap-4">
<x-ui.swap label="{{ __('Toggle theme') }}">
<x-slot:on>
{{-- Sun icon (active / light mode) --}}
<svg xmlns="http://www.w3.org/2000/svg" class="size-6 text-foreground" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
<path d="M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M4.93 19.07l1.41-1.41M17.66 6.34l1.41-1.41"/>
<circle cx="12" cy="12" r="4"/>
</svg>
</x-slot:on>
<x-slot:off>
{{-- Moon icon (inactive / dark mode) --}}
<svg xmlns="http://www.w3.org/2000/svg" class="size-6 text-muted-foreground" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
<path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/>
</svg>
</x-slot:off>
</x-ui.swap>
<span class="text-sm text-muted-foreground">{{ __('Toggle light / dark') }}</span>
</div>
Installation
php artisan ui:add swap
Note
This component ships an Alpine behavior module at
resources/js/ui/swap.js. Import it once from your bundle so it registers on alpine:init:
import './swap.js';
Registry contract
php artisan ui:add swap
writes only the files below. The CLI validates each file hash before writing and asks before it replaces a local change, unless you pass --force.
-
resources/views/components/ui/swap.blade.php -
resources/js/ui/swap.js
- Registry dependencies
- None — installs on its own.
- Packages
-
composer: jml/brok:^0.2npm: alpinejs
Use with AI
A brief for your coding agent: install command, usage, props, guidance and the rules. Copy it, or open a prompt about this component in an assistant.
# Brok UI: Swap (`swap`)
A button that cross-fades between an 'on' and 'off' named slot (e.g. two icons) with an accessible role="switch" toggle.
Brok UI is a Laravel Blade component registry. Installed components are plain Blade files the app owns.
## Install
```bash
php artisan ui:add swap
```
## Usage
```blade
<div class="flex items-center gap-4">
<x-ui.swap label="{{ __('Toggle theme') }}">
<x-slot:on>
{{-- Sun icon (active / light mode) --}}
<svg xmlns="http://www.w3.org/2000/svg" class="size-6 text-foreground" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
<path d="M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M4.93 19.07l1.41-1.41M17.66 6.34l1.41-1.41"/>
<circle cx="12" cy="12" r="4"/>
</svg>
</x-slot:on>
<x-slot:off>
{{-- Moon icon (inactive / dark mode) --}}
<svg xmlns="http://www.w3.org/2000/svg" class="size-6 text-muted-foreground" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
<path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/>
</svg>
</x-slot:off>
</x-ui.swap>
<span class="text-sm text-muted-foreground">{{ __('Toggle light / dark') }}</span>
</div>
```
## Props
- `active` (bool, default `false`) — Initial active state; modelable via x-model or wire:model.
- `name` (mixed|null, default `null`) — When set, mirrors the active state into a hidden input for plain form submission.
- `label` (mixed|null, default `null`) — Accessible name for the switch; falls back to a generic label unless aria-label or aria-labelledby is supplied.
## Use when
- Use for immediate state changes that are obvious and reversible.
- Toggling between two icons or short pieces of content, such as sun/moon or play/pause, with a cross-fade.
- Needing a single-button role="switch" that can bind with x-model or wire:model.
## Avoid when
- Do not use for choices that need explanation, confirmation, or a later Save step.
- Toggling a labelled setting with visible on/off text; use switch instead.
- Submitting a boolean form field with no visual swap needed; use checkbox or switch.
## Anti-patterns
- Using a switch when Save is required
- Using a switch for irreversible actions
## Rules
- Use the `<brok:swap>` tag (or `<x-ui.swap>`) in Blade; do not rewrite the component.
- Prefer the documented props and variants over utility-class overrides; when a utility must win, use the `!` important modifier.
- Use semantic design tokens (`bg-primary`, `text-muted-foreground`), never raw colour utilities.
- Keep the `data-slot` attributes; they are the styling and test hooks.
## Links
- Docs: https://brokui.dev/docs/components/swap
- Registry JSON (files, props, contract): https://brokui.dev/r/open/swap.json
Working in Claude Code, Cursor or Codex? Give the agent the whole registry through the MCP server or the Brok UI skill.
API
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| active | bool | false | Initial active state; modelable via x-model or wire:model. |
| name | mixed | null | null | When set, mirrors the active state into a hidden input for plain form submission. |
| label | mixed | null | null | Accessible name for the switch; falls back to a generic label unless aria-label or aria-labelledby is supplied. |
Slots
default—on— Content shown when active; defaults to a sun icon.off— Content shown when inactive; defaults to a moon icon.
Data slots
Stable hooks for CSS overrides and browser tests.
Behavior
- Clicking the button, or pressing Enter or Space, toggles the active state and cross-fades between the on and off slots.
- The root is modelable, so x-model (Alpine) or wire:model (Livewire) can bind directly to it without extra wiring.
- With a name prop, a hidden input mirrors the active state so the toggle submits in a plain form.
- Installs a JavaScript behavior module when the registry item includes resources/js/ui files.
- Declares registry capability flags: a11y, interactive, responsive, rtl, darkMode, localized.
Guidance
Change an immediate reversible system state.
Use when
- Use for immediate state changes that are obvious and reversible.
- Toggling between two icons or short pieces of content, such as sun/moon or play/pause, with a cross-fade.
- Needing a single-button role="switch" that can bind with x-model or wire:model.
Avoid when
- Do not use for choices that need explanation, confirmation, or a later Save step.
- Toggling a labelled setting with visible on/off text; use switch instead.
- Submitting a boolean form field with no visual swap needed; use checkbox or switch.
Use instead
- Checkbox for a value saved later
- Button plus confirmation for consequential actions
Anti-patterns
- Using a switch when Save is required
- Using a switch for irreversible actions
- Anatomy
- Theming hooks
Accessibility
- Keyboard
- Enter Space
- Focus
managed
- The root exposes role="switch" with aria-checked reflecting state, and requires a label prop, aria-label, or aria-labelledby to have an accessible name.
- Both the on and off slots are aria-hidden decoration; the accessible name comes only from the label.
- Semantic HTML and a stable
data-slotattribute for styling and scripting hooks. - Focus-visible rings use the
ringtoken, so keyboard focus is always visible. - Disabled and invalid states are conveyed to assistive tech, not by color alone.
- Targets WCAG 2.2 AA; verify contrast in light, dark, admin and customer surfaces in the preview.
- Labels go through
__()and layout uses logical properties (ms-*,text-start), so it mirrors underdir="rtl"— flip the preview to RTL to confirm. - Dark mode uses the same semantic tokens under the
darkclass; high contrast follows forced-color system tokens.
Livewire
Add a stable wire:key when Livewire can reorder this interactive component.
<div wire:key="swap-{{ $record->id }}">
<div class="flex items-center gap-4">
<x-ui.swap label="{{ __('Toggle theme') }}">
<x-slot:on>
{{-- Sun icon (active / light mode) --}}
<svg xmlns="http://www.w3.org/2000/svg" class="size-6 text-foreground" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
<path d="M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M4.93 19.07l1.41-1.41M17.66 6.34l1.41-1.41"/>
<circle cx="12" cy="12" r="4"/>
</svg>
</x-slot:on>
<x-slot:off>
{{-- Moon icon (inactive / dark mode) --}}
<svg xmlns="http://www.w3.org/2000/svg" class="size-6 text-muted-foreground" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
<path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/>
</svg>
</x-slot:off>
</x-ui.swap>
<span class="text-sm text-muted-foreground">{{ __('Toggle light / dark') }}</span>
</div>
</div>
Validation
Validation support: native. Keep the error message connected with aria-describedby.
<form wire:submit="save" class="space-y-2">
<brok:swap
wire:model="value"
:aria-invalid="$errors->has('value') ? 'true' : 'false'"
aria-describedby="value-error"
/>
@error('value')
<p id="value-error" role="alert">{{ $message }}</p>
@enderror
<brok:button type="submit" wire:loading.attr="disabled">
<span wire:loading.remove>Save</span>
<span wire:loading>Saving…</span>
</brok:button>
</form>
Source
The exact, editable files ui:add writes
into your app. Previews render this same code; there are no preview-only components.
@props([
'active' => false,
'name' => null,
// Accessible name — role="switch" requires one, and both slots are
// aria-hidden decoration. Pass a meaningful label in context; falls back to
// a generic name. Skipped when the consumer supplies
// aria-label/aria-labelledby themselves.
'label' => null,
])
@php
$needsName = $label === null && ! $attributes->has('aria-label') && ! $attributes->has('aria-labelledby');
@endphp
{{--
Swap — toggles between an "on" and "off" named slot with a cross-fade
transition. Works as a keyboard-accessible role="switch" button.
Named slots: $on (shown when active), $off (shown when inactive).
Prop `name`: when set, a hidden input mirrors the active state for forms.
--}}
<button
type="button"
x-data="uiSwap({ active: @js((bool) $active) })"
{{-- Modelable: `x-model` (Alpine) and `wire:model` (Livewire binds through
the same x-model contract) attach to the root. Inert without either. --}}
x-modelable="active"
data-slot="swap"
role="switch"
@if ($label !== null) aria-label="{{ $label }}" @elseif ($needsName) aria-label="{{ __('Toggle') }}" @endif
:aria-checked="active.toString()"
@click="toggle()"
@keydown.enter.prevent="toggle()"
@keydown.space.prevent="toggle()"
tabindex="0"
{{ $attributes->merge(['class' => 'relative inline-grid cursor-pointer select-none place-items-center focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background disabled:pointer-events-none disabled:opacity-50']) }}
>
{{-- "on" state --}}
<span
x-show="active"
x-transition:enter="transition duration-200 motion-reduce:transition-none"
x-transition:enter-start="opacity-0 scale-75"
x-transition:enter-end="opacity-100 scale-100"
x-transition:leave="transition duration-150 motion-reduce:transition-none"
x-transition:leave-start="opacity-100 scale-100"
x-transition:leave-end="opacity-0 scale-75"
class="col-start-1 row-start-1"
aria-hidden="true"
>@isset($on){{ $on }}@else<svg xmlns="http://www.w3.org/2000/svg" class="size-6" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M4.93 19.07l1.41-1.41M17.66 6.34l1.41-1.41"/><circle cx="12" cy="12" r="4"/></svg>@endisset</span>
{{-- "off" state --}}
<span
x-show="!active"
x-transition:enter="transition duration-200 motion-reduce:transition-none"
x-transition:enter-start="opacity-0 scale-75"
x-transition:enter-end="opacity-100 scale-100"
x-transition:leave="transition duration-150 motion-reduce:transition-none"
x-transition:leave-start="opacity-100 scale-100"
x-transition:leave-end="opacity-0 scale-75"
class="col-start-1 row-start-1"
aria-hidden="true"
>@isset($off){{ $off }}@else<svg xmlns="http://www.w3.org/2000/svg" class="size-6" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/></svg>@endisset</span>
@if ($name)
<input type="hidden" :name="@js($name)" :value="active ? '1' : '0'" />
@endif
</button>
/**
* Swap behavior — toggles between an "on" and "off" visual state.
*
* Self-registers on `alpine:init` so import order does not matter — import
* this file once from your bundle (e.g. resources/js/ui/index.js).
*/
document.addEventListener('alpine:init', () => {
window.Alpine.data('uiSwap', (config = {}) => ({
active: config.active ?? false,
toggle() {
this.active = !this.active;
},
}));
});
Ownership & lifecycle
Owner, release state, review evidence and adoption for this item.
- Owner
- Platform UI (@JoshJML)
- Current version
-
1.2.2 - Status
- Stable
- License
-
open - Accessibility reviewed
- No review date recorded
- Last breaking change
- No date recorded
- Deprecation
- Not deprecated
- Contract
-
v6 - Foundation
-
≥ 1.0.0