Back to Blog
Framer Motion and Next.js: The Ultimate Guide for Modern Web Apps – cover image comparing React animation libraries

Framer Motion and Next.js: The Ultimate Guide for Modern Web Apps

·
Karan

Key Takeaways

  • Next.js provides server-side rendering and automatic code splitting, making your React apps fast and SEO-friendly.

  • Framer Motion simplifies adding intuitive, declarative animations and gestures to your React components.

  • Combining the two provides unparalleled performance with interactive, smooth, and engaging user experiences.

Next.js and Framer Motion combined are the industry-standard stack for building high-performance, animated React applications. Next.js provides the robust, server-side rendered foundation, while Framer Motion handles complex, fluid, and hardware-accelerated animations across the DOM. According to recent developer surveys, over 3.5 million developers download Framer Motion weekly to power these interactions. Let's dive into what each of these technologies is and how they can elevate your projects.

What is Next.js?

Next.js is a popular framework for building React applications. It makes creating web apps easier and faster. With Next.js, you get many built-in features that help you develop high-quality applications. For example, it offers server-side rendering, which means your pages can load faster and be better for search engines. This is great for users because they don’t have to wait long for a page to appear.

Another cool feature of Next.js is automatic code splitting. This means that only the code needed for a specific page is loaded, making your app quicker and more efficient. Plus, Next.js supports static site generation, which is perfect for creating fast, SEO-friendly websites. Overall, using Next.js allows developers to focus on building great user experiences without worrying about the nitty-gritty of configuration.

What is Framer Motion?

Framer Motion is an animation library specifically designed for React. It provides an easy way to create beautiful animations and transitions. With Framer Motion, you can make elements move, fade, and transform in ways that catch a user’s eye.

The library is built to be simple and intuitive. For instance, you can create animations with just a few lines of code. This makes it perfect for developers who want to add flair to their applications without spending hours coding complex animations. Framer Motion also supports gesture-based animations, so users can interact with your site in fun and engaging ways.

Why Use Framer Motion with Next.js?

Combining Framer Motion with Next.js is like having the best of both worlds. Here are some of the advantages:

Core Benefits Summary

  • Improved Performance: Next.js is optimized for performance, and when you add Framer Motion, your animations can run smoothly without slowing down your app. This is crucial for keeping users happy and engaged.
  • Enhanced User Experience: Animations can make your application feel more interactive and alive. With Framer Motion, you can create transitions that guide users through your app, making it easier for them to understand how to navigate.
  • SEO Benefits: Because Next.js supports server-side rendering, your animated pages can still be indexed by search engines. This means you can have great animations without sacrificing your SEO.
  • Ease of Use: Both Next.js and Framer Motion are designed to be developer-friendly. You can get started quickly and build complex animations without a steep learning curve.

In summary, using Framer Motion with Next.js gives you a powerful combination that can significantly elevate your application's interactivity while maintaining excellent Core Web Vitals.

Setting Up Your Next.js Project

Now that you understand the basics of Next.js and Framer Motion, it’s time to get your hands dirty! In this section, we’ll walk through the steps to set up a new Next.js project and integrate Framer Motion.

Creating a New Next.js Project

The first step is to create your Next.js project. We’ll use a tool called create-next-app, which simplifies the setup process:

npx create-next-app@latest my-next-app

Replace my-next-app with whatever name you want for your project. This command will create a new directory with all the files you need.

cd my-next-app
npm run dev

Now, open your web browser and go to http://localhost:3000. You should see your new Next.js app running.

Installing Framer Motion

With your Next.js project set up, it’s time to install Framer Motion:

npm install motion/react

This command will download and install Framer Motion into your project. Now you can start adding animations to your components!

Basic Animations with Framer Motion

Now that you have your Next.js project set up and Framer Motion installed, it’s time to dive into the fun part: creating animations! Let’s explore how to make your app come alive.

Fade-In Effect

To create a fade-in effect, you can use the motion component from Framer Motion. Here’s a simple example:

"use client";
import { motion } from "motion/react";
export default function FadeInDemo() {
return (
<div className="flex flex-col items-center justify-center p-8 w-full">
<div className="h-32 w-full max-w-sm flex items-center justify-center bg-neutral-900 border border-neutral-800 rounded-xl overflow-hidden relative">
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ duration: 1.5 }}
className="bg-cyan-500/20 text-cyan-400 px-6 py-3 rounded-lg border border-cyan-500/30 font-medium"
>
Hello, World!
</motion.div>
</div>
</div>
);
}

Here's exactly what that code creates in the browser:

Hello, World!

In this code, the initial property sets the starting state, and the animate property defines the end state. The transition property controls how long the animation takes.

Slide-In Effect

Next, let’s create a slide-in effect. This can make elements appear to slide from the left side of the screen:

"use client";
import { motion } from "motion/react";
export default function SlideInDemo() {
return (
<div className="flex flex-col items-center justify-center p-8 w-full">
<div className="h-32 w-full max-w-sm flex items-center justify-center bg-neutral-900 border border-neutral-800 rounded-xl overflow-hidden relative">
<motion.div
initial={{ x: -150, opacity: 0 }}
animate={{ x: 0, opacity: 1 }}
transition={{ duration: 0.6, type: "spring", bounce: 0.4 }}
className="bg-emerald-500/20 text-emerald-400 px-6 py-3 rounded-lg border border-emerald-500/30 font-medium"
>
Welcome to My Next.js App!
</motion.div>
</div>
</div>
);
}

Here is a live preview of the slide-in element we just created:

Welcome to My Next.js App!

With just a few lines of code, you can create engaging animations that enhance user experience.

Animating Buttons

Animating buttons can make them more interactive. Here’s an example of how to animate a button with a hover effect:

import { motion } from "motion/react";
const AnimatedButton = () => {
return (
<motion.button
whileHover={{ scale: 1.1 }}
whileTap={{ scale: 0.9 }}
transition={{ duration: 0.2 }}
>
Click Me!
</motion.button>
);
};
export default AnimatedButton;

Here is a live preview of a more advanced, fully interactive Magnetic Button animation. Hover over the button to see it follow your cursor magnetically:

Animating Images

Here’s how to add a simple fade-in effect to an image:

import { motion } from "motion/react";
const AnimatedImage = () => {
return (
<motion.img
src="/path/to/image.jpg"
alt="A beautiful scenery"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ duration: 1 }}
/>
);
};
export default AnimatedImage;

Advanced Animation Techniques

Let’s explore some advanced techniques with Framer Motion, like gesture-based animations and shared layout transitions.

Gestures and Interactivity

You can make elements draggable by using the drag property. Here’s an example of a draggable box:

"use client";
import { motion } from "motion/react";
import { useRef } from "react";
export default function DraggableDemo() {
const constraintsRef = useRef(null);
return (
<div className="flex flex-col items-center justify-center p-4 sm:p-8 w-full">
<div
ref={constraintsRef}
className="w-full max-w-sm h-64 bg-neutral-900 border border-neutral-800 rounded-xl overflow-hidden flex items-center justify-center relative touch-none"
>
<motion.div
drag
dragConstraints={constraintsRef}
whileDrag={{ scale: 1.1, cursor: "grabbing" }}
whileHover={{ scale: 1.05 }}
className="w-24 h-24 bg-indigo-500 rounded-2xl cursor-grab flex items-center justify-center text-white font-medium shadow-xl shadow-indigo-500/20 active:cursor-grabbing border-2 border-indigo-400/50"
>
Drag Me!
</motion.div>
</div>
</div>
);
}
export default DraggableBox;

Here's an interactive example of that code. Try dragging the box around within its constraints:

Drag Me!

Try dragging the box around within the container.

Conclusion

In this article, we've explored the exciting world of animations using Framer Motion with Next.js. We’ve seen how both tools work together to create dynamic and engaging web applications. Combining these two technologies gives you the ability to create fast, responsive, and visually stunning applications that captivate your audience. Dive in, get creative, and see how you can make your web applications stand out with animations!

Wanna skip the grunt work?

Just checkout these PREMIUM MOTION COMPONENTS built with Next.js and Tailwind that you can integrate in under a minute.

Further Resources

As you continue your journey with Framer Motion and Next.js, having access to the right resources can make a big difference. Here are some valuable links to official documentation, tutorials, and community resources that will help you deepen your understanding and enhance your skills:

  • Framer Motion Documentation: The official documentation is the best place to start. It provides detailed explanations of all the features, examples, and API references. You can find it here: Framer Motion Docs
  • Next.js Documentation: For everything you need to know about Next.js, including setup, features, and best practices, visit the official Next.js documentation: Next.js Docs.
  • Community Tutorials: Many creators have uploaded tutorials covering both Framer Motion and Next.js. Search for "Framer Motion Next.js tutorial" to find helpful videos that walk you through building projects step-by-step.
  • Twitter and Discord: Follow the official Framer Motion and Next.js accounts for updates and community tips. You can also join Discord servers related to these technologies to connect with other developers.

FAQ

Does Framer Motion work with Next.js?

Yes, it works perfectly with Next.js. Next.js explicitly supports React components and libraries, and Framer Motion handles server-side rendering flawlessly when configured correctly.

Written by Karan

ogBlocks is an Animated React UI Component library built with Motion and Tailwind CSS

Framer Motion and Next.js: The Ultimate Guide for Modern Web Apps | OGBlocks Blog | ogBlocks