NEXT.JS X FRAMER MOTION

Page Transitions in Next.js with Framer Motion

Using AnimateSharedLayout in Next.js

Seth Corker
Nov 16 · 5 min read
Image for post
Image for post

What we’re building

See the demo

Why?

Why Next.js?

Why Framer Motion?

What we need to start animating

About the project

Page transition tutorial

Identify the elements that stay the same

We can’t transition between all of these elements because they aren’t all on both pages. The title and image are the same so these are the most obvious candidates. The button looks like it could work but the text content differs, it’s possible with a bit of work to create something that looks passable but I’ll skip the layout transition for this element.

Adding the AnimateSharedLayout to _app

Our _app looks like this. I'm using the Bulma CSS library and SASS along with FontAwesome icons. The Component prop in the context of MyApp is the whatever page we are currently on. In Next.js, this acts as a layout which is used across our pages. It's especially useful for our purposes because we want to be able to animate seamlessly between all the pages in our app.

We’ve added AnimateSharedLayout but nothing has changed, let's turn our title and image elements into motion components.

Using layoutId to setup the transitions

Our title will go from this:

To this:

We’ll do the same with the figure element that wraps our image.

Let’s replace the matching elements on our features.js page too. When the page transitions from / to /features, framer will treat the elements on one page with the elements on the second page as the same if they share an identical layoutId.

Try it out, and we’ll see the title and image moves smoothly across the screen.

Completing the look with motion

For elements that are unique to a page, we’ll fade them in.

Here’s an example of the subtitle on the index page:

This is a straightforward fade in animation that starts with an opacity of zero and animates to an opacity of 1 after a delay of 200ms.

But what happens when the user first visits a page? Can we animate all elements in? With Framer Motion, yes we can. We can add the animate prop to have an intro animation that will play when the user opens the page and this won't affect the transition to the next page.

Here’s our animation, a fade in with a slight scale up effect:

Let’s see the final product:

The final transitions between pages

Where to now?

The web is a platform that can do so much more than static webpages. As a platform for rich user experience, we have the opportunity to craft more imaginative user interfaces that really react to the user’s actions and exceed expectations. Animation through page transitions is one of the tools we have available to enrich experiences, and they’re easy with Next.js and Framer Motion.

Resources

If you enjoyed this and would like to learn more about Framer Motion or web development in React, check out my Benevolent Bytes blog. I also have a YouTube channel, Coding with Seth, where I make video tutorials to help you craft better web experiences

Benevolent Bytes

Thoughts, stories and ideas about development.

Seth Corker

Written by

Sharing ideas, facts, and opinions about programming. 📚 Learn more React and JavaScript. 🗺 Learn web development tips and tricks 🛸 Discover something new

Benevolent Bytes

Thoughts, stories and ideas about development. Web development and beyond.

Seth Corker

Written by

Sharing ideas, facts, and opinions about programming. 📚 Learn more React and JavaScript. 🗺 Learn web development tips and tricks 🛸 Discover something new

Benevolent Bytes

Thoughts, stories and ideas about development. Web development and beyond.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store