Chasse.

13 / 13 ยท View Transitions API

Circle

A circle from where you clicked, with the origin handed to the CSS as custom properties.

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 opens from where you clicked. The origin goes to the CSS as custom properties.
export const vtCircle = native('circle', {
  before: ({ origin }) => {
    const x = origin?.x ?? innerWidth / 2
    const y = origin?.y ?? innerHeight / 2
    const style = document.documentElement.style
    style.setProperty('--vt-x', `${x}px`)
    style.setProperty('--vt-y', `${y}px`)
    style.setProperty('--vt-r', `${Math.hypot(Math.max(x, innerWidth - x), Math.max(y, innerHeight - y))}px`)
  },
  done: () => {
    const style = document.documentElement.style
    for (const name of ['--vt-x', '--vt-y', '--vt-r']) style.removeProperty(name)
  },
})
The CSS
html[data-vt]::view-transition-old(root),
html[data-vt]::view-transition-new(root) {
  mix-blend-mode: normal;
}

/* --vt-x, --vt-y and --vt-r are set from where you clicked, before the old page is captured. */
html[data-vt="circle"]::view-transition-old(root) {
  animation: none;
}
html[data-vt="circle"]::view-transition-new(root) {
  animation: vt-circle 1s cubic-bezier(0.65, 0, 0.35, 1) both;
  z-index: 2;
}
@keyframes vt-circle {
  from { clip-path: circle(0px at var(--vt-x) var(--vt-y)); }
  to { clip-path: circle(var(--vt-r) at var(--vt-x) var(--vt-y)); }
}
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)
    },
  }
}