Chasse.

10 / 13 ยท View Transitions API

Morph

The title morphs from one page into the next. This is what the API does best: elements with the same name on both pages move and resize into each other.

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
const name = (element: Element | null) => element?.setAttribute('style', 'view-transition-name: title')

// The title morphs between pages. The same view-transition-name goes on the old title before
// the old page is captured, and on the new one before the new page is. From the gallery that is
// the clicked card's title; going back it is the card for the page you left.
export const vtMorph = native('morph', {
  before: ({ trigger, current }) => name(trigger?.querySelector('h2') ?? current.querySelector('h1')),
  after: ({ next, from }) => name(next?.querySelector(`a[href="${from.pathname}"] h2`) ?? next?.querySelector('h1') ?? null),
  done: () => document.querySelectorAll('[style*="view-transition-name"]').forEach((element) => element.removeAttribute('style')),
})
The CSS
html[data-vt]::view-transition-old(root),
html[data-vt]::view-transition-new(root) {
  mix-blend-mode: normal;
}

/* Elements with the same view-transition-name on both pages move and resize into each other.
   The script gives the title that name on the old page and on the new one. */
html[data-vt="morph"]::view-transition-old(root),
html[data-vt="morph"]::view-transition-new(root) {
  animation-duration: 0.5s;
}
html[data-vt="morph"]::view-transition-group(title) {
  animation-duration: 0.8s;
  animation-timing-function: cubic-bezier(0.65, 0, 0.35, 1);
}
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)
    },
  }
}