Page transitions in SvelteKit, and letting each route pick its layout
View transitions between routes with onNavigate, a nav marker that slides to the new page, and a typed way for each route to tell the root layout how to arrange it.

My theme toggle already used a view transition. This post adds them between pages: the header stays still, the content fades into the new page, and the green bar under the current nav link slides to the new one. Then a small system for each route to choose its own layout, which the transition handles for free.
Starting the transition
SvelteKit routes in the browser, without reloading the page, so the browser can't know when a "page change" happens. SvelteKit tells us with onNavigate:
onNavigate((navigation) => {
if (!canTransition()) return;
return new Promise<void>((resolve) => {
document.startViewTransition({
update: async () => {
resolve();
await navigation.complete;
},
types: ['route']
});
});
});The order of events is the tricky part:
onNavigateruns just before SvelteKit swaps the page. It returns a promise, and SvelteKit waits for it.startViewTransitiontakes a picture of the old page, then callsupdate().update()resolves the promise, so SvelteKit swaps the page, and waits fornavigation.complete.- The browser takes the "after" picture and animates between the two.
canTransition() checks the browser supports transition types and the visitor hasn't asked for reduced motion. My CSS safety net for reduced motion can't help here: * doesn't match the ::view-transition-* pseudo-elements.
Naming things, but only sometimes
By default the whole page cross-fades. view-transition-name gives an element its own pair of pictures, so it can animate differently:
:root:active-view-transition-type(route) {
& .site-header {
view-transition-name: site-header;
}
& main {
view-transition-name: page;
}
}A name must be unique on the page. My first version said & header, and it worked until a Devlog entry got a <header> of its own for its title. Two elements had the name site-header, and the browser aborted the transition with "Unexpected duplicate view-transition-name". A class on the site header fixed it.
The names only exist during route transitions. My theme toggle's circular reveal animates the root picture, and a named element is cut out of the root. With a permanent name, the header would sit outside the circle and change theme on its own.
Then the content: the old page fades out fast, the new one fades in and rises a little.
&::view-transition-old(page) {
animation: route-out 150ms ease-in both;
}
&::view-transition-new(page) {
animation: route-in 300ms var(--ease-out-3) both;
}The sliding marker
The current nav link used to have a thick underline. An underline is part of the text, so it can't move between links. So I made it an element:
<a href={resolve(link.href)} aria-current={current}>
{link.label}
{#if current}
<span class="marker" aria-hidden="true"></span>
{/if}
</a>current is 'page' on a section's own page, 'true' on a page inside it (a post is inside Blog), and undefined everywhere else, which Svelte leaves out of the HTML.
There's only one marker on each page. During a route transition it's named nav-marker, so the browser pairs the old one with the new one and moves it from the old link's position to the new one's.
Each route picks its composition
The blog and the Devlog are long text, and long lines are hard to read. I wanted a narrow column there, but <main> belongs to the root layout, and a page's styles can't reach it. So the page sends the layout a piece of data instead.
The type goes in src/app.d.ts, so it's known everywhere:
interface PageData {
composition?: 'default' | 'reading';
}A route sets it in its +layout.ts, which covers the route and everything below it:
export const load: LayoutLoad = () => ({ composition: 'reading' }) satisfies App.PageData;satisfies checks the value against the type and keeps 'reading' as that exact string. Without it, TypeScript would widen it to string.
The root layout reads it and the CSS does the rest:
<main data-composition={page.data.composition ?? 'default'}>{@render children()}</main>main[data-composition='reading'] {
max-inline-size: var(--size-content-3);
}Going from the home page (1120px wide) to the blog (60 characters wide) needed nothing extra in the transition: the content box jumps to its new size and the new content fades in. The one catch was the old picture. By default it's stretched to the new size, so a wide page got squeezed as it faded out. Giving it its own size, centered where it was, fixed it:
&::view-transition-old(page) {
inline-size: auto;
block-size: auto;
inset-inline-start: 50%;
translate: -50% 0;
}Update: "where it was" wasn't quite true. The old picture sits where the new content starts, and going Back, SvelteKit scrolls the list to where you were, so the old article showed from its middle as it faded out. The layout now measures the content's top before and after the change and moves the old picture by the difference: translate: -50% var(--route-shift, 0px). It didn't show until cards started turning into their pages.
A bug this found
While testing, the home page's main was 876px wide instead of 1120px. In my body's grid, margin-inline: auto stops an item from stretching, so main shrank to fit its content. Every page was as wide as its text. inline-size: 100% next to max-inline-size fixed it, and now a composition really decides the width.