Chasse.

12 / 13 ยท View Transitions API

Wipe

A top-to-bottom reveal made by animating the new page snapshot's clip-path.

Chasse holds the swap while the browser captures the old page, then hands both snapshots to CSS. Browsers without the API get a quick fade.

The transition
// The new page is revealed top to bottom by animating its snapshot's clip-path in CSS.
export const vtWipe = native('wipe')
The CSS
html[data-vt]::view-transition-old(root),
html[data-vt]::view-transition-new(root) {
  mix-blend-mode: normal;
}

html[data-vt="wipe"]::view-transition-old(root) {
  animation: none;
}
html[data-vt="wipe"]::view-transition-new(root) {
  animation: vt-wipe 0.9s cubic-bezier(0.65, 0, 0.35, 1) both;
  z-index: 2;
}
@keyframes vt-wipe {
  from { clip-path: inset(0 0 100% 0); }
  to { clip-path: inset(0 0 0 0); }
}
The helper every View Transitions sample shares
interface NativeOptions {
  /** Runs before the old page is captured: name elements that should morph, set variables for the CSS. */
  before?: (visit: Visit) => void
  /** Runs once the new page is in, just before it is captured. */
  after?: (visit: Visit) => void
  /** Runs when the transition is over or was cancelled. */
  done?: () => void
}

// out-in, with the swap held by the browser's View Transitions API. `leave` starts the
// transition: the browser captures the old page and keeps showing it while Chasse fetches and
// swaps the pages. `enter` lets the browser capture the new page, then CSS animates the two.
export const native = (name: string, { before, after, done }: NativeOptions = {}): TransitionHooks => {
  let swapped: (() => void) | null = null
  let transition: ViewTransition | null = null

  const reset = () => {
    const root = document.documentElement
    delete root.dataset.vt
    delete root.dataset.vtDirection
    done?.()
    swapped = null
    transition = null
  }

  return {
    mode: 'out-in',
    leave: (visit) => {
      // Browsers without the API get a quick fade instead.
      if (!('startViewTransition' in document)) return visit.current.animate({ opacity: [1, 0] }, { duration: 250, fill: 'forwards' })

      before?.(visit)
      document.documentElement.dataset.vt = name
      document.documentElement.dataset.vtDirection = visit.direction
      const ready = new Promise<void>((resolve) => (swapped = resolve))
      transition = document.startViewTransition(() => ready)
      transition.finished.catch(() => undefined).finally(reset)

      // A visit replaced while it loads must not leave the page frozen, waiting for a swap.
      visit.signal.addEventListener('abort', () => {
        swapped?.()
        transition?.skipTransition()
      }, { once: true })
    },
    enter: (visit) => {
      if (!('startViewTransition' in document)) return visit.next?.animate({ opacity: [0, 1] }, { duration: 250 })
      if (!transition) return
      after?.(visit)
      swapped?.()
      return transition.finished.catch(() => undefined)
    },
  }
}