View Transitions
Inertia supports the View Transitions API, allowing you to animate page transitions.
The View Transitions API is a relatively new browser feature. Inertia gracefully falls back to standard page transitions in browsers that don't support the API.
Enabling Transitions
You may enable view transitions for a visit by setting the viewTransition option to true. By default, this will apply a cross-fade transition between pages.
1import { router } from "@inertiajs/vue3";23router.visit("/another-page", { viewTransition: true });1import { router } from "@inertiajs/vue3";23router.visit("/another-page", { viewTransition: true });
1import { router } from "@inertiajs/react";23router.visit("/another-page", { viewTransition: true });1import { router } from "@inertiajs/react";23router.visit("/another-page", { viewTransition: true });
1import { router } from "@inertiajs/svelte";23router.visit("/another-page", { viewTransition: true });1import { router } from "@inertiajs/svelte";23router.visit("/another-page", { viewTransition: true });
Transition Callbacks
You may also pass a callback to the viewTransition option, which will receive the standard ViewTransition instance provided by the browser. This allows you to hook into the various promises provided by the API.
1import { router } from "@inertiajs/vue3";23router.visit("/another-page", {4 viewTransition: (transition) => {5 transition.ready.then(() => console.log("Transition ready"));6 transition.updateCallbackDone.then(() => console.log("DOM updated"));7 transition.finished.then(() => console.log("Transition finished"));8 },9});1import { router } from "@inertiajs/vue3";23router.visit("/another-page", {4 viewTransition: (transition) => {5 transition.ready.then(() => console.log("Transition ready"));6 transition.updateCallbackDone.then(() => console.log("DOM updated"));7 transition.finished.then(() => console.log("Transition finished"));8 },9});
1import { router } from "@inertiajs/react";23router.visit("/another-page", {4 viewTransition: (transition) => {5 transition.ready.then(() => console.log("Transition ready"));6 transition.updateCallbackDone.then(() => console.log("DOM updated"));7 transition.finished.then(() => console.log("Transition finished"));8 },9});1import { router } from "@inertiajs/react";23router.visit("/another-page", {4 viewTransition: (transition) => {5 transition.ready.then(() => console.log("Transition ready"));6 transition.updateCallbackDone.then(() => console.log("DOM updated"));7 transition.finished.then(() => console.log("Transition finished"));8 },9});
1import { router } from "@inertiajs/svelte";23router.visit("/another-page", {4 viewTransition: (transition) => {5 transition.ready.then(() => console.log("Transition ready"));6 transition.updateCallbackDone.then(() => console.log("DOM updated"));7 transition.finished.then(() => console.log("Transition finished"));8 },9});1import { router } from "@inertiajs/svelte";23router.visit("/another-page", {4 viewTransition: (transition) => {5 transition.ready.then(() => console.log("Transition ready"));6 transition.updateCallbackDone.then(() => console.log("DOM updated"));7 transition.finished.then(() => console.log("Transition finished"));8 },9});
Links
The viewTransition option is also available on the Link component.
1import { Link } from '@inertiajs/vue3'23<Link href="/another-page" view-transition>Navigate</Link>1import { Link } from '@inertiajs/vue3'23<Link href="/another-page" view-transition>Navigate</Link>
1import { Link } from "@inertiajs/react";23<Link href="/another-page" viewTransition>4 Navigate5</Link>;1import { Link } from "@inertiajs/react";23<Link href="/another-page" viewTransition>4 Navigate5</Link>;
1import { Link } from '@inertiajs/svelte'23<Link href="/another-page" viewTransition>Navigate</Link>1import { Link } from '@inertiajs/svelte'23<Link href="/another-page" viewTransition>Navigate</Link>
You may also pass a callback to access the ViewTransition instance.
1import { Link } from '@inertiajs/vue3'23<Link4 href="/another-page"5 :view-transition="6 (transition) => transition.finished.then(() => console.log('Done'))7 "8>9 Navigate10</Link>1import { Link } from '@inertiajs/vue3'23<Link4 href="/another-page"5 :view-transition="6 (transition) => transition.finished.then(() => console.log('Done'))7 "8>9 Navigate10</Link>
1import { Link } from "@inertiajs/react";23<Link4 href="/another-page"5 viewTransition={(transition) =>6 transition.finished.then(() => console.log("Done"))7 }8>9 Navigate10</Link>;1import { Link } from "@inertiajs/react";23<Link4 href="/another-page"5 viewTransition={(transition) =>6 transition.finished.then(() => console.log("Done"))7 }8>9 Navigate10</Link>;
1import { Link } from '@inertiajs/svelte'23<Link4 href="/another-page"5 viewTransition={(transition) => transition.finished.then(() => console.log('Done'))}6>7 Navigate8</Link>1import { Link } from '@inertiajs/svelte'23<Link4 href="/another-page"5 viewTransition={(transition) => transition.finished.then(() => console.log('Done'))}6>7 Navigate8</Link>
Global Configuration
You may enable view transitions globally for all visits by configuring the visitOptions callback when initializing your Inertia app.
1import { createInertiaApp } from "@inertiajs/vue3";23createInertiaApp({4 // ...5 defaults: {6 visitOptions: (href, options) => {7 return { viewTransition: true };8 },9 },10});1import { createInertiaApp } from "@inertiajs/vue3";23createInertiaApp({4 // ...5 defaults: {6 visitOptions: (href, options) => {7 return { viewTransition: true };8 },9 },10});
1import { createInertiaApp } from "@inertiajs/react";23createInertiaApp({4 // ...5 defaults: {6 visitOptions: (href, options) => {7 return { viewTransition: true };8 },9 },10});1import { createInertiaApp } from "@inertiajs/react";23createInertiaApp({4 // ...5 defaults: {6 visitOptions: (href, options) => {7 return { viewTransition: true };8 },9 },10});
1import { createInertiaApp } from "@inertiajs/svelte";23createInertiaApp({4 // ...5 defaults: {6 visitOptions: (href, options) => {7 return { viewTransition: true };8 },9 },10});1import { createInertiaApp } from "@inertiajs/svelte";23createInertiaApp({4 // ...5 defaults: {6 visitOptions: (href, options) => {7 return { viewTransition: true };8 },9 },10});
Customizing Transitions
You may customize the transition animations using CSS. The View Transitions API uses several pseudo-elements that you can target with CSS to create custom animations. The following examples are taken from the Chrome documentation.
1@keyframes fade-in {2 from {3 opacity: 0;4 }5}6@keyframes fade-out {7 to {8 opacity: 0;9 }10}11@keyframes slide-from-right {12 from {13 transform: translateX(30px);14 }15}16@keyframes slide-to-left {17 to {18 transform: translateX(-30px);19 }20}21::view-transition-old(root) {22 animation:23 90ms cubic-bezier(0.4, 0, 1, 1) both fade-out,24 300ms cubic-bezier(0.4, 0, 0.2, 1) both slide-to-left;25}26::view-transition-new(root) {27 animation:28 210ms cubic-bezier(0, 0, 0.2, 1) 90ms both fade-in,29 300ms cubic-bezier(0.4, 0, 0.2, 1) both slide-from-right;30}1@keyframes fade-in {2 from {3 opacity: 0;4 }5}6@keyframes fade-out {7 to {8 opacity: 0;9 }10}11@keyframes slide-from-right {12 from {13 transform: translateX(30px);14 }15}16@keyframes slide-to-left {17 to {18 transform: translateX(-30px);19 }20}21::view-transition-old(root) {22 animation:23 90ms cubic-bezier(0.4, 0, 1, 1) both fade-out,24 300ms cubic-bezier(0.4, 0, 0.2, 1) both slide-to-left;25}26::view-transition-new(root) {27 animation:28 210ms cubic-bezier(0, 0, 0.2, 1) 90ms both fade-in,29 300ms cubic-bezier(0.4, 0, 0.2, 1) both slide-from-right;30}
You may also animate individual elements between pages by assigning them a unique view-transition-name. For example, you may animate an avatar from a large size on a profile page to a small size on a dashboard.
1<!-- Profile.vue -->2<template>3 <img src="/avatar.jpg" alt="User" class="avatar-large" />4</template>56<style>7.avatar-large {8 view-transition-name: user-avatar;9 width: auto;10 height: 200px;11}12</style>1314<!-- Dashboard.vue -->15<template>16 <img src="/avatar.jpg" alt="User" class="avatar-small" />17</template>1819<style>20.avatar-small {21 view-transition-name: user-avatar;22 width: auto;23 height: 40px;24}25</style>1<!-- Profile.vue -->2<template>3 <img src="/avatar.jpg" alt="User" class="avatar-large" />4</template>56<style>7.avatar-large {8 view-transition-name: user-avatar;9 width: auto;10 height: 200px;11}12</style>1314<!-- Dashboard.vue -->15<template>16 <img src="/avatar.jpg" alt="User" class="avatar-small" />17</template>1819<style>20.avatar-small {21 view-transition-name: user-avatar;22 width: auto;23 height: 40px;24}25</style>
1// Profile.jsx2export default function Profile() {3 return <img src="/avatar.jpg" alt="User" className="avatar-large" />4}56// CSS7.avatar-large {8 view-transition-name: user-avatar;9 width: auto;10 height: 200px;11}1213// Dashboard.jsx14export default function Dashboard() {15 return <img src="/avatar.jpg" alt="User" className="avatar-small" />16}1718// CSS19.avatar-small {20 view-transition-name: user-avatar;21 width: auto;22 height: 40px;23}1// Profile.jsx2export default function Profile() {3 return <img src="/avatar.jpg" alt="User" className="avatar-large" />4}56// CSS7.avatar-large {8 view-transition-name: user-avatar;9 width: auto;10 height: 200px;11}1213// Dashboard.jsx14export default function Dashboard() {15 return <img src="/avatar.jpg" alt="User" className="avatar-small" />16}1718// CSS19.avatar-small {20 view-transition-name: user-avatar;21 width: auto;22 height: 40px;23}
1<!-- Profile.svelte -->2<img src="/avatar.jpg" alt="User" class="avatar-large" />34<style>5.avatar-large {6 view-transition-name: user-avatar;7 width: auto;8 height: 200px;9}10</style>1112<!-- Dashboard.svelte -->13<img src="/avatar.jpg" alt="User" class="avatar-small" />1415<style>16.avatar-small {17 view-transition-name: user-avatar;18 width: auto;19 height: 40px;20}21</style>1<!-- Profile.svelte -->2<img src="/avatar.jpg" alt="User" class="avatar-large" />34<style>5.avatar-large {6 view-transition-name: user-avatar;7 width: auto;8 height: 200px;9}10</style>1112<!-- Dashboard.svelte -->13<img src="/avatar.jpg" alt="User" class="avatar-small" />1415<style>16.avatar-small {17 view-transition-name: user-avatar;18 width: auto;19 height: 40px;20}21</style>
You may customize view transitions to your liking using any CSS animations you wish. For more information, please consult the View Transitions API documentation.