Swap toggles the visibility of two elements with a smooth transition. Perfect for toggle buttons, theme switchers, and animated icons.
Usage Example
Swap with transition effects and state binding.
await import('/components/actions/swap.js');
await import('/components/actions/button.js');
const { signal, tags, $ } = Lightview;
const { div, p, Swap, Button } = tags;
const isActive = signal(false);
// Controlled swap
const swap = Swap({
effect: 'flip',
class: 'text-6xl',
active: () => isActive.value,
onChange: (checked) => { isActive.value = checked; }
},
Swap.On({}, '🥳'),
Swap.Off({}, '😭')
);
// External control
const demo = div({ style: 'display: flex; flex-direction: column; gap: 1rem; align-items: center' },
swap,
p({ class: 'text-sm' },
() => isActive.value ? 'Party time! 🎉' : 'So sad... 💔'
),
Button({
color: 'primary',
size: 'sm',
onclick: () => { isActive.value = !isActive.value; }
}, 'Toggle from button')
);
$('#example').content(demo);
Reactive Toggle
Controlled swap with signal binding
await import('/components/actions/swap.js');
await import('/components/actions/button.js');
const { signal, tags, $ } = Lightview;
const { div, p, Swap, Button } = tags;
const isActive = signal(false);
// Controlled swap
const swap = Swap({
effect: 'flip',
class: 'text-6xl',
active: () => isActive.value,
onChange: (checked) => { isActive.value = checked; }
},
Swap.On({}, '🥳'),
Swap.Off({}, '😭')
);
// External control
const demo = div({ style: 'display: flex; flex-direction: column; gap: 1rem; align-items: center' },
swap,
p({ class: 'text-sm' },
() => isActive.value ? 'Party time! 🎉' : 'So sad... 💔'
),
Button({
color: 'primary',
size: 'sm',
onclick: () => { isActive.value = !isActive.value; }
}, 'Toggle from button')
);
$('#example').content(demo);
Props
| Prop | Type | Default | Description |
|---|---|---|---|
active |
boolean | function | false | Control swap state (reactive) |
effect |
string | - | 'rotate' | 'flip' |
onChange |
function | - | Callback when state changes: (checked) => void |
Sub-components
| Component | Description |
|---|---|
Swap.On |
Content visible when active/checked |
Swap.Off |
Content visible when inactive/unchecked |
Swap Gallery
Live examples using <lv-swap> custom elements.
Effects
Rotate
Flip