11 / 13 ยท View Transitions API
Slide
The browser snapshots both pages and slides them. All of the motion is a few lines of CSS.
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 page slides in from the right, or from the left going back. All of the motion is in CSS.
export const vtSlide = native('slide') html[data-vt]::view-transition-old(root),
html[data-vt]::view-transition-new(root) {
mix-blend-mode: normal;
}
html[data-vt="slide"]::view-transition-old(root) {
animation: vt-slide-out 0.7s cubic-bezier(0.65, 0, 0.35, 1) both;
}
html[data-vt="slide"]::view-transition-new(root) {
animation: vt-slide-in 0.7s cubic-bezier(0.65, 0, 0.35, 1) both;
z-index: 2;
}
html[data-vt="slide"][data-vt-direction="back"]::view-transition-old(root) {
animation-name: vt-slide-out-back;
}
html[data-vt="slide"][data-vt-direction="back"]::view-transition-new(root) {
animation-name: vt-slide-in-back;
}
@keyframes vt-slide-out {
to { transform: translateX(-25%); opacity: 0.5; }
}
@keyframes vt-slide-in {
from { transform: translateX(100%); }
}
@keyframes vt-slide-out-back {
to { transform: translateX(25%); opacity: 0.5; }
}
@keyframes vt-slide-in-back {
from { transform: translateX(-100%); }
} 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)
},
}
}