Animate Elements When Their Layout Changes with Framer Motion layoutId

Will Johnson
InstructorWill Johnson
Share this video with your friends

Social Share Links

Send Tweet
Published 3 years ago
Updated 2 years ago

Use the layoutId prop to automatically animate layout changes across, and between, multiple components.

Will Johnson: [0:02] Take notice what happens when we start adding items to our list. As you can see, it does fade in, but the item that was at the top of the list kind of jerks down. It's not smooth. It isn't fluid, and also the same thing when we remove it. It just jerks. That's not the experience that we're going for in our animation.

[0:24] Luckily, Framer Motion makes this incredibly easy, especially now with Framer Motion 5. All we have to do is go to our list item and then add a layoutId prop. We'll set it to item.id. Basically, we want Framer Motion to keep track of the layout of the id of each item.

[0:52] Now, whenever the items that we're tracking layout changes, that means it needs to move somewhere or something like that, Framer Motion will automatically animate it in place because now all these items are considered to share the same layout.

[1:12] Let me show you what that looks like. We'll add the first one, of course. That one animates in. We'll add the second one. Notice how it slides down and makes room. It just looks a lot better. The same thing happens on the exit animation. It fades out and then slides up. It's a lot smoother and a lot less jerky.

[1:34] To recap, all we did was add the layoutId prop and we wanted Framer Motion to track the layout of the item.id, which is the items that we're adding to the list.

egghead
egghead
~ 12 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today