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.