Swap

Animated content swaps for a region: tabs, pagination, filtered lists. The ink covers the old content, your code changes the DOM underneath, the new content is revealed. The height follows, and focus lands on the new content.

Live example

Page 1. Less, but better.

Usage

import { createRenderer, createSwapFx } from 'webgl-ui'

const renderer = createRenderer()
const results = document.querySelector<HTMLElement>('#results')!
const swap = createSwapFx(results, { renderer })

async function showPage(n: number) {
  const html = await fetchPage(n)            // anything: fetch, template, framework render
  await swap.run(() => { results.innerHTML = html })
}

The region's background is the cover color (or set --fx-cover). The update may be sync or async; while it runs, the region stays covered.

Options

OptionTypeDefaultDescription
rendererRenderer | () => RendererrequiredThe shared renderer.
effectEffectDefinkEffectAny cover effect.
durationnumber600Reveal in ms; covering takes 60%.
viewTransitionbooleanfalseRun the update inside document.startViewTransition() where it exists: the browser morphs the region's box and cross-fades the rest of the page. Otherwise the library animates the height itself, in every engine.

Returns

MemberDescription
run(update)Covers, runs update, reveals. Calls are queued; the promise resolves after the reveal.
destroy()Cancels a running animation; later calls just run the update.

Behavior and accessibility