Swap
A button that cross-fades between an on and off slot with a keyboard-accessible role="switch" toggle.
Preview
Controls
Device view renders in an isolated frame so real responsive breakpoints apply. Switch to Full for overflowing popovers.
Installation
php artisan ui:add swap
Behavior layer (Alpine)
This component installs a self-registering Alpine module to
resources/js/ui/swap.js. Import the behavior layer once
from your bundle so it loads (it hooks alpine:init):
import './swap.js';
Registry contract
Install confidence
php artisan ui:add swap
writes only the generated targets below. The CLI validates each file hash before writing and
prompts before replacing local changes unless --force is used.
- Version
- 1.0.0
- License
- open
- Stability
- stable
- Contract
- v2
- Foundation
- ≥ 1.0.0
| Type | Generated target |
|---|---|
| blade | resources/views/components/ui/swap.blade.php |
| js | resources/js/ui/swap.js |
Registry dependencies
None.
Package dependencies
composer: jml/brok:^1.0
npm: alpinejs
Guidance
Immediate reversible stateChange an immediate reversible system state.
Use when
- Use for immediate state changes that are obvious and reversible. Do not use for choices that need explanation, confirmation, or a later Save step.
Avoid when
- Avoid using swap as a default simply because it is available in the library. Choose it only when its interaction cost, visibility, and behavior match the job to be done.
- Task length
- micro
- Interaction cost
- low
- Interruptiveness
- none
- Input precision
- constrained
- Mobile / touch risk
- low
- Accessibility risk
- medium
- Needs background context
- No
- Comparison needed
- No
Preferred alternatives
- 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
API
manifest knowledge + registry-derived coverageProps
| Prop | Type / values | Default | Description |
|---|---|---|---|
| active | bool | false | Declared by @props in the registry Blade source. |
| name | mixed|null | null | Declared by @props in the registry Blade source. |
Slots
Components use the default Blade slot unless a documented subcomponent is installed by the registry item.
Data slots
swap
Capabilities
Behavior
- Uses server-rendered Blade and semantic HTML.
- Installs a JavaScript behavior module when the registry item includes resources/js/ui files.
- Declares registry capability flags: a11y, interactive, responsive, rtl, darkMode, localized.
Usage
<div class="flex items-center gap-4">
<x-ui.swap aria-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>
Accessibility & localization
data-slot
Keyboard focus-visible
Localized labels
- Accessibility target: wcag-2.2-aa-target.
- Uses semantic HTML and a stable
data-slotattribute for styling/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 above.
- Labels go through
__()and the component uses logical properties (ms-*,text-start,start-*) so it mirrors correctly — flip the preview to RTL to confirm.
Source
The exact, editable file ui:add writes
into your app — copy it or own the whole thing. Previews
render this same code; there are no preview-only components.
Changelog
Current version: v1.0.0. See the site changelog for the full release history.