Back to Blog
Next.js SaaS Boilerplate

Page Transitions and Animations in Next.js

Creating smooth next js page transitions & animation effects turns a static website into a fluid application. Users expect apps to feel native and responsive. A jarring page load breaks immersion immediately. Next.js, combined with tools like Framer Motion, allows you to orchestrate complex visual flows. This guide covers the technical implementation of layout transitions,...

Nabed Khan

Nabed Khan

Nov 30, 2025
6 min read
Page Transitions and Animations in Next.js

Creating smooth next js page transitions & animation effects turns a static website into a fluid application. Users expect apps to feel native and responsive. A jarring page load breaks immersion immediately. Next.js, combined with tools like Framer Motion, allows you to orchestrate complex visual flows.

This guide covers the technical implementation of layout transitions, exit animations, and performance best practices.

Why Are Animations Critical for Modern Web Apps?

Animations bridge the cognitive gap between different states of an application, reducing the user’s perceived load time. They provide visual cues about navigation hierarchy and improve the overall “feel” of the product. A well-animated interface keeps users engaged and lowers bounce rates significantly.

When a user clicks a link, and the screen snaps instantly to a new layout, it feels “digital” and harsh. In the physical world, things move and transition.

I recall a project where we added a simple 0.3s fade-in to a dashboard. User complaints about “slow data loading” dropped by 40%. The data loaded at the same speed, but the perception changed.

Animations serve three core functions:

  1. Orientation: Telling the user where they came from and where they are going.
  2. Feedback: Acknowledging an interaction immediately.
  3. Masking: Hiding data fetching latency behind a smooth visual curtain.

If you are building a Next.js SaaS template, integrated transitions are often the difference between looking like a prototype and a finished product.

How Do Next.js Page Transitions Work in the App Router?

The App Router requires using template.tsx files instead of layout.tsx to trigger animations on navigation. While layouts persist state and do not re-render, templates create a new instance for every route change. This unique behavior allows animation libraries to detect the mounting and unmounting phases required for transitions.

This is the most common stumbling block for developers moving from the Pages router.

In the Pages Router (old way), you often had to rely on _app.js logic. In the App Router (new way), we have file-based routing distinctions:

  • layout.tsx: Wraps pages. Preserves state. Does NOT re-render on navigation between sibling routes. Great for navigation bars.
  • template.tsx: Wraps pages. Re-renders on navigation. Required for enter/exit animations.

If you place your animation logic in a Layout, the initial load will animate, but clicking links will do nothing. The component stays mounted, so the “enter” animation never triggers again.

What Is the Best Library for Next.js Animations?

Framer Motion is the industry standard for React and Next.js animations due to its declarative syntax and robust component lifecycle handling. It manages complex “exit” animations automatically via the AnimatePresence component, which keeps elements in the DOM until their exit animation completes.

While you can use raw CSS or other libraries, Framer Motion aligns perfectly with the React component model.

Alternative options exist. Some developers prefer GSAP for timeline-heavy marketing sites, while others use the native View Transitions API. However, when comparing Next.js alternatives or styling libraries, Framer Motion remains the most compatible with the App Router’s server components architecture (when using “use client” directives).

Quick Comparison Table

LibraryBest ForLearning CurveExit Animation Support
Framer MotionComplex UI, Page TransitionsModerateExcellent
GSAPHigh-end Marketing SitesSteepGood
CSS TransitionsSimple hover statesLowPoor (requires JS hacks)
View TransitionsNative browser feelingLowGrowing Support

How Do I Implement a Basic Page Transition?

Install Framer Motion, creates a template.tsx file in your implementation directory, and wrap the children prop with a motion component. Define initial, animate, and exit variants to control opacity and positioning. Ensure the component is marked with ‘use client’ to enable browser-side animation logic.

Here is the practical implementation. First, install the library:

Bash

npm install framer-motion

Next, create your app/template.tsx. This file sits alongside your page.tsx.

TypeScript

'use client'

import { motion } from 'framer-motion'

export default function Template({ children }: { children: React.ReactNode }) {
  return (
    
      {children}
    
  )
}

This code creates a “slide up and fade in” effect every time a user navigates to a new URL. It is simple, effective, and adds significant polish.

How Do I Handle Exit Animations?

Exit animations require the AnimatePresence component wrapping your routes, usually implemented in a global context or specific layout wrapper. You must use the mode="wait" prop to ensure the current page finishes leaving the screen before the new page enters, preventing layout thrashing or content overlap.

Handling the “exit” is harder than the “enter.” By default, React removes components instantly.

To smooth this out, you need a slightly more advanced setup, often involving a FrozenRouter context if you are using the new App Router, or strictly using template.tsx with careful key management.

If you are dealing with complex data dependencies during these transitions, ensure your Next.js build is optimized. If the Javascript bundle is too heavy, the animation might stutter (jank) right as the exit begins.

Can I Use the View Transitions API?

Yes, Next.js creates experimental support for the native View Transitions API, or you can use third-party hooks to trigger it. This API allows the browser to snapshot the old state and cross-fade to the new state automatically, performing heavy lifting off the main JavaScript thread.

The View Transitions API is the future. It works differently than Framer Motion. Instead of animating DOM elements, the browser takes a “screenshot” of the old page and the new page, then blends them.

This is particularly useful for simpler Next.js applications where you want a native mobile app feel without heavy library overhead.

Best Practices for Performance and Accessibility

Always respect the user’s prefers-reduced-motion media query to disable animations for those with vestibular disorders. Avoid animating expensive CSS properties like width or height; stick to transform and opacity. Lazy load heavy animations to prevent blocking the main thread during the initial paint.

Animations should never hurt usability.

  1. Respect Preferences:CSS@media (prefers-reduced-motion) { * { animation: none !important; transition: none !important; } }
  2. Authentication Flows: When redirecting users via Next.js authentication, keep animations minimal. Users want to get into their account fast. A spinner is better than a flashy transition here.
  3. API Delays: If your page relies on Next.js API routes to fetch data, use a Skeleton loader that transitions into the real content. Do not animate a blank white box.

Final Thoughts

Mastering next js page transitions & animation requires a shift in thinking. You stop looking at pages as static documents and start treating them as connected views.

Start small. Implement a simple opacity fade in your template.tsx. Once you are comfortable, explore AnimatePresence for exit animations or layout animations for shared elements. A well-crafted transition communicates quality and care to your users, distinguishing your application from the crowd. For a deeper history on the framework itself, check out Next.js.