I'm now zeroing in on the core of the project: creating a SvelteKit site with advanced transitions using svelte-motion. Initial analysis is complete; I have a solid understanding of the project's goal. This will necessitate detailed design considerations for optimal implementation.
Outlining Project Structure
I'm deep into project structuring now, clarifying the scope of "advanced" transitions, it will involve layout animations, page transitions, and potentially scroll-triggered effects. My current plan involves a global layout for navigation and transitions, with components for specific animated areas. The first round of components will include a hero section with staggered text and a gallery with shared layout animations for expanding cards. Tailwind is looking good for styles, but I'll only bring it in if the vanilla CSS starts to slow down the workflow.
Implementing Transition Effects
I'm now focusing on implementing the actual transition effects. I've broken down the implementation into clear steps, from setting up SvelteKit and installing dependencies to creating a TransitionWrapper and tackling key features like the shared layout effect and staggered entrances. I have a detailed plan for features like scroll progress effects to add to the site's interactivity and user experience. Routing will be straightforward, with just a few pages for the page transitions demonstration.
Architecting Transition Logic
I'm presently building the Transition.svelte component, focusing on the AnimatePresence setup with exitBeforeEnter. To make this work smoothly with SvelteKit's routing, I've had to key the <slot /> in the +layout.svelte file. I'm taking care to keep the example concise, wary of over-complicating it with excess svelte-motion features. It's a key part of the design, and I'm refining the component's internal structure to match that.
Building the Core Animations
I'm now in the middle of creating the more advanced component, which will be a shared layout grid. I'm focusing on the click-to-select interaction, which changes the selectedId state and renders an overlay. I'm also preparing to use Motion layoutId and AnimatePresence in the grid and overlay, and will be updating the +layout.svelte and +page.svelte files.
Detailing Implementation Steps
I'm deep into the coding now, emphasizing clarity for the user. I'll use Markdown code blocks for file structures and snippets, including file paths to ease reproduction. I'm prioritizing clear explanations, especially when dealing with concepts like layoutId - making sure the user grasps the reasoning behind each implementation choice. I'll also add instructions to run the project. Since the example will be runnable right out-of-the-box, I'll keep the styles clean and simple, using the <style> block for styling.
This guide will walk you through creating a SvelteKit site featuring two "Advanced" animation techniques using svelte-motion:
Page Transitions: Smoothly animating between routes.
Shared Layout Animations (Magic Motion): Elements that morph from one position/size to another (e.g., a card expanding into a full-screen view).
Prerequisites
Node.js installed.
Ideally, a basic understanding of CSS/HTML.
Step 1: Setup Project
Initialize a new SvelteKit project and install the physics/animation library.
bash
npm create svelte@latest my-motion-site
# Select "Skeleton project" and "TypeScript" (optional but recommended)
cd my-motion-site
npm install
npm install svelte-motion
Step 2: Create the Page Transition Wrapper
To animate between pages, we need a component that detects route changes and triggers an animation. We will use AnimatePresence to handle components leaving the DOM.
Create src/lib/components/PageTransition.svelte:
svelte
<script>
import { Motion, AnimatePresence } from 'svelte-motion';
import { page } from '$app/stores';
// We access the current route key (pathname) to trigger changes
$: key = $page.url.pathname;
const variants = {
initial: { opacity: 0, x: -20 },
enter: { opacity: 1, x: 0, transition: { duration: 0.4, ease: "easeOut" } },
exit: { opacity: 0, x: 20, transition: { duration: 0.3, ease: "easeIn" } }
};
</script>
<!--
exitBeforeEnter: Ensures the old page fades out completely
before the new one fades in.
-->
<AnimatePresence list={[{ key, ...variants }]}>
<Motion
let:motion
initial="initial"
animate="enter"
exit="exit"
{variants}
>
<div use:motion>
<slot />
</div>
</Motion>
</AnimatePresence>
Step 3: Global Layout Setup
Apply the transition wrapper to your global layout so it affects every page.
Update src/routes/+layout.svelte:
svelte
<script>
import PageTransition from '$lib/components/PageTransition.svelte';
import '../app.css'; // Ensure you have basic CSS reset here
</script>
<nav>
<a href="/">Gallery</a>
<a href="/about">About</a>
</nav>
<main>
<PageTransition>
<slot />
</PageTransition>
</main>
<style>
:global(body) {
margin: 0;
font-family: sans-serif;
background: #111;
color: white;
overflow-x: hidden;
}
nav {
padding: 20px;
display: flex;
gap: 20px;
border-bottom: 1px solid #333;
position: relative;
z-index: 10;
}
nav a {
color: white;
text-decoration: none;
font-weight: bold;
}
main {
padding: 20px;
max-width: 800px;
margin: 0 auto;
}
</style>
This is the "Wow" factor. We will create a grid of items. When clicked, an item will "pop out" using the layoutId prop. This tells svelte-motion that two different DOM elements are actually the "same" thing visually.
Create src/lib/components/ExpandingGrid.svelte:
svelte
Step 5: Build the Routes
1. The Home Page (Gallery)
Update src/routes/+page.svelte:
svelte
<script>
import ExpandingGrid from '$lib/components/ExpandingGrid.svelte';
import { Motion } from 'svelte-motion';
</script>
<Motion
let:motion
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
>
<h1 use:motion>Motion Gallery</h1>
</Motion>
<p>Click an item to see Shared Layout Animations.</p>
<ExpandingGrid />
Page Transitions: In +layout.svelte, the PageTransition component listens to the URL. When it changes, AnimatePresence detects the key change. It holds the old component in the DOM, runs the exit animation on it, removes it, and then runs the enter animation on the new component.
Shared Layout (layoutId): In ExpandingGrid.svelte, we have a small card and a large card (modal). Both share the same layoutId (e.g., "neon"). When Svelte removes the small card from the DOM and adds the large card on the same tick (controlled by selectedId state), svelte-motion calculates the position difference and smoothly animates the scale and transform to bridge the gap.
Running the project
bash
npm run dev
Open your browser.
Click between "Gallery" and "About" to see the smooth slide/fade transition.
On the Gallery page, click a colored card. Watch it physically morph into the center modal. Click the black backdrop to morph it back.