Detailing SvelteKit FLIP cross-route animation
This chunk scopes a full SvelteKit example implementing FLIP animations morphing a grid card into a detail header across routes, with a shared store capturing bounding boxes on click and applying inverse transforms after navigation. It includes concrete file structures, store implementation, event handling, and CSS strategies to elegantly handle cross-route transitions without blocking navigation or breaking SSR. Next, the agent will generate the full minimal project code and usage instructions.
Below is a minimal SvelteKit project that demonstrates a cross‑route FLIP animation.
When you click a card on the grid (route /), its bounding box is stored. On the
detail route (/detail/[id]) the header element is measured, inverted to the
old box, and then animated back to its natural place.
Files are shown with their paths; drop them into a fresh npm create svelte@latest project.
src/lib/flip.ts
import { get, writable } from 'svelte/store';
type FlipState = {
key: string;
rect: DOMRectReadOnly;
scrollY: number;
};
const store = writable<FlipState | null>(null);
/**
* Called *before* navigation. Stores the element’s bounding box.
*/
export function startFlip(key: string, node: HTMLElement) {
if (typeof window === 'undefined') return;
const rect = node.getBoundingClientRect();
store.set({
key,
rect,
scrollY: window.scrollY
});
}
/**
* Called on the target element *after* the next page has rendered.
* It calculates the delta between the first and last boxes, applies
* the inverted transform, and then plays back to identity.
*/
export function runFlip(key: string, node: HTMLElement, duration = 380) {
if (typeof window === 'undefined') return;
const state = get(store);
if (!state || state.key !== key) return;
const first = state.rect;
const last = node.getBoundingClientRect();
const dx = first.left - last.left;
const dy = first.top - last.top + (state.scrollY - window.scrollY);
const sx = first.width / last.width;
const sy = first.height / last.height;
// Invert
node.style.transformOrigin = 'top left';
node.style.transform = `translate(${dx}px, ${dy}px) scale(${sx}, ${sy})`;
node.style.transition = 'transform 0s';
// Force reflow to ensure the inverted transform is applied
node.getBoundingClientRect();
// Play
requestAnimationFrame(() => {
node.style.transition = `transform ${duration}ms cubic-bezier(.2,.8,.2,1)`;
node.style.transform = '';
});
// Cleanup
const done = () => {
node.style.transition = '';
node.style.transform = '';
node.removeEventListener('transitionend', done);
store.set(null);
};
node.addEventListener('transitionend', done);
}
src/routes/+page.svelte (Route A)
<script lang="ts">
import { startFlip } from '$lib/flip';
const cards = [
{ id: 'alpha', title: 'Alpha Project', blurb: 'Fastest deployment pipeline' },
{ id: 'beta', title: 'Beta Project', blurb: 'Security-first everything' },
{ id: 'gamma', title: 'Gamma Project', blurb: 'Compute at the edge' },
{ id: 'delta', title: 'Delta Project', blurb: 'Serverless that scales' }
];
function handleClick(event: MouseEvent, id: string) {
const el = event.currentTarget as HTMLElement;
startFlip(id, el);
// allow normal SvelteKit navigation to proceed
}
</script>
<svelte:head>
<title>Route A · Grid</title>
</svelte:head>
<main>
<h1 class="page-title">Projects</h1>
<div class="grid">
{#each cards as card}
<a
class="card"
href={`/detail/${card.id}`}
sveltekit:prefetch
on:click={(e) => handleClick(e, card.id)}
>
<div class="card-body">
<h2>{card.title}</h2>
<p>{card.blurb}</p>
<span class="cta">Open →</span>
</div>
</a>
{/each}
</div>
</main>
<style>
main {
padding: 2rem clamp(1.5rem, 3vw, 4rem);
}
.page-title {
font-size: clamp(2rem, 4vw, 3rem);
margin-bottom: 1.5rem;
}
.grid {
display: grid;
gap: 1rem;
grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
}
.card {
position: relative;
display: block;
padding: 1.1rem;
border-radius: 16px;
text-decoration: none;
color: inherit;
background: color-mix(in srgb, #fff 80%, #e0e7ff);
box-shadow: 0 12px 28px -18px rgba(0, 0, 0, 0.35);
transition: transform 180ms ease, box-shadow 180ms ease, background 180ms ease;
}
.card:hover {
transform: translateY(-4px);
box-shadow: 0 16px 32px -18px rgba(0, 0, 0, 0.45);
background: color-mix(in srgb, #fff 90%, #c7d2fe);
}
.card-body h2 {
margin: 0 0 0.25rem;
font-size: 1.1rem;
}
.card-body p {
margin: 0 0 0.5rem;
color: #4b5563;
font-size: 0.95rem;
}
.cta {
font-weight: 600;
color: #4338ca;
}
</style>
src/routes/detail/[id]/+page.ts
import type { PageLoad } from './$types';
export const load: PageLoad = ({ params }) => {
const titles: Record<string, string> = {
alpha: 'Alpha Project',
beta: 'Beta Project',
gamma: 'Gamma Project',
delta: 'Delta Project'
};
return {
id: params.id,
title: titles[params.id] ?? params.id,
description:
'This is the detail page. The header morphes from the clicked card using a FLIP animation.'
};
};
src/routes/detail/[id]/+page.svelte (Route B)
<script lang="ts">
import { onMount } from 'svelte';
import { runFlip } from '$lib/flip';
export let data;
let hero: HTMLElement;
onMount(() => {
runFlip(data.id, hero);
});
</script>
<svelte:head>
<title>Route B · {data.title}</title>
</svelte:head>
<main>
<a class="back" href="/">← Back</a>
<header class="hero" bind:this={hero}>
<div class="eyebrow">Project</div>
<h1>{data.title}</h1>
<p>{data.description}</p>
</header>
<section class="content">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse dictum justo non
tellus tincidunt, id posuere tortor bibendum. Sed at egestas erat, id lacinia est.
</p>
<p>
More filler content to show layout. Scroll, resize, and click different cards to see the
FLIP morph from the grid to this header.
</p>
</section>
</main>
<style>
main {
padding: 2rem clamp(1.5rem, 3vw, 4rem);
max-width: 960px;
margin: 0 auto;
}
.back {
display: inline-flex;
align-items: center;
gap: 0.35rem;
color: #4338ca;
text-decoration: none;
font-weight: 600;
margin-bottom: 1rem;
}
.hero {
background: color-mix(in srgb, #fff 80%, #c7d2fe);
border-radius: 18px;
padding: 1.6rem;
box-shadow: 0 18px 42px -22px rgba(0, 0, 0, 0.4);
}
.eyebrow {
font-size: 0.8rem;
text-transform: uppercase;
letter-spacing: 0.08em;
color: #6b7280;
margin-bottom: 0.5rem;
}
h1 {
margin: 0 0 0.5rem;
font-size: clamp(1.8rem, 3.2vw, 2.6rem);
}
p {
margin: 0 0 1rem;
color: #4b5563;
line-height: 1.6;
}
.content {
margin-top: 2rem;
}
</style>
src/routes/+layout.svelte (optional, global styles)
<script>
// simple global font + background
</script>
<svelte:head>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&display=swap" rel="stylesheet" />
</svelte:head>
<slot />
<style>
:global(body) {
margin: 0;
font-family: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
background: #f8fafc;
color: #111827;
}
:global(a) {
color: inherit;
}
:global(*),
:global(*::before),
:global(*::after) {
box-sizing: border-box;
}
</style>
How it works (FLIP across routes)
- First – On the list page, the
on:click handler calls startFlip(id, node), storing the bounding box and scroll position in a store.
- Last – On the detail page, once the header is in the DOM,
runFlip(id, hero) reads the stored box and measures the header’s box.
- Invert – It sets a transform that moves/scales the header to where the card was (
translate + scale from the delta).
- Play – It immediately transitions the transform back to identity, giving the morphing effect.
Because the data is kept in a Svelte store, it survives the route change and allows you to animate elements between entirely different pages.
Run with:
npm install
npm run dev
and click any card to see it morph into the detail header.