Swap
Swap toggles the visibility of two elements with a smooth transition. Perfect for toggle buttons, theme switchers, and animated icons.
Basic Example
Swap with rotate animation
const { default: Swap } = await import('/components/actions/swap.js');
const { tags, $ } = Lightview;
const { div, span } = tags;
// Emoji swap with rotate effect
const emojiSwap = Swap({ effect: 'rotate', class: 'text-4xl' },
Swap.On({}, 'π'),
Swap.Off({}, 'π')
);
// Play/Pause swap
const playPause = Swap({ effect: 'rotate', class: 'btn btn-circle' },
Swap.On({}, 'βΈοΈ'),
Swap.Off({}, 'βΆοΈ')
);
$('#demo').insert(
div({ class: 'flex gap-8 items-center' },
emojiSwap,
playPause
)
);
Reactive Toggle
Controlled swap with signal binding
const { default: Swap } = await import('/components/actions/swap.js');
const { default: Button } = await import('/components/actions/button.js');
const { signal, tags, $ } = Lightview;
const { div, span, p } = 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({ class: 'flex flex-col gap-4 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')
);
$('#demo').insert(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 |
Effects
Rotate
Flip
Hamburger Menu Icon
Theme Toggle (Sun/Moon)
For more examples, see the
DaisyUI Swap documentation.