


























Framer Motion has changed a lot since I last wrote about it. So much so that I recently got a bit lost trying to build a specific layout animation and my own blog post that actually looked into this specific type of animation was far from helpful 😅. Despite the updated section I added back in November, it still felt like I was not touching upon several points on this subject and that some of them were incomplete.
On top of the API changes and the many new features that the Framer team added to the package around layout animations, I noticed that there are lots of little tricks that can make your layout animations go from feeling clumsy to absolutely ✨ perfect ✨. However, these are a bit hidden or lack some practical examples to fully understand them.
Thus, I felt it was time to write a dedicated deep dive into all the different types of layout animations. My objective is for this article to be the place you go to whenever you need a refresher on layout animations or get stuck. Additionally, I'll give you some of my own tips and tricks that I use to work around some of the glitches that layout animations can trigger and examples on how to combine them with other tools from the library such as AnimatePresence to achieve absolutely delightful effects in your projects!
Before we dive into the new features and complex examples of layout animations, let's look back at the fundamentals to reacquaint ourselves with how they work.
In Framer Motion, you can animate a motion component between distinct layouts by setting the layout prop to true. This will result in what we call a layout animation.
We can't animate a motion component between layouts using a combination of initial and animate props as we would do for other kinds of Framer Motion animations. For that, we need to use the layout prop.
In the example below, you'll find a first showcase of a layout animation:
You can change the position of the motion component, the square, along the x axis.
You can enable or disable the layout prop for that motion component
We can see that each time we change the layout, i.e. a rerender occurs, the layout prop allows for the component to transition smoothly from its previous layout to the newly selected one. However, without it there is no transition: the square will move abruptly.
Layout animations "smooth things up", and add a certain level of physicality to some user interactions where usually things would transition abruptly. One example where they can shine is when adding/removing elements from a list. I tend to leverage layout animations a lot for use cases like this one, especially combined with other Framer Motion features such as AnimatePresence.
The playground below showcases one of my own NotificationList component that leverages layout animations:
each notification is wrapped in a motion component with the layout prop set to true.
the overall list is wrapped in AnimatePresence thus allowing each item in a list to have an exit animation.
clicking on any of the notifications on the list will remove them and, thanks to layout animations, the stack will gracefully readjust itself.
When performing a layout animation that affects the size of a component, some distortions may appear during the transition for some properties like borderRadius or boxShadow. These distortions will occur even if these properties are not part of the animation.
Luckily, there's an easy workaround to fix those: set these properties as inline styles as showcased below:
Expand card
Set distorted properties inline
10
.box[data-expanded="true"] {
19
data-expanded={expanded}
We just saw that setting the layout prop to true gives us the ability to animate a component between layouts by transitioning any properties related to its size or position. I recently discovered that there are more values that the layout prop can take:
layout="position": we only smoothly transition the position-related properties. Size-related properties will transition abruptly.
layout="size": we only smoothly transition the size-related properties. Position-related properties will transition abruptly.
To illustrate this, I built the widget below that showcases how the transition of a motion component is altered based on the value of the layout prop:
Why would we need to use these other layout properties? What's the practical use? you may ask. Sometimes, as a result of a layout animation, the content of a component that resizes can end up "squished" or "stretched". If you see this happening when working on a layout animation, chances are that it can be fixed by simply setting the layout prop to position.
Below you'll find an example of such a use case:
Removing items in this horizontal list will affect the size of each component. By default, you will notice the components getting slightly squished when an item is removed.
Wrapping the content in a motion component and setting layout to position by toggling the switch will fix all the distortions you may observe on the content of the motion block. Each component will resize gracefully with a more natural transition.
Example of practical use case for layout="position"
2
<Label variant="success">
7
justifyContent: 'start',
Drag-to-reorder items in a list where each item then smoothly moves to its final position is perhaps the best in class use case when it comes to layout animations. It's actually the first use case I thought about when I discovered layout animations in the first place a year ago.
Lucky us, the developers at Framer gave us a ready-to-use set of components to handle that specific use case with ease 🎉. They provided 2 components that we're going to use in follow-up examples:
Reorder.Group where we pass our list of items, the direction of the reorder (horizontal or vertical), and the onReorder callback which will return the latest order of the list
Reorder.Item where we pass the value of an item in the list
Simple examples of drag-to-reorder list using Reorder
2
const [items, setItems] = React.useState(['Item 1', 'Item 2', 'Item 3']);
15
{items.map((item) => (
17
<Reorder.Item key={item} value={item}>
With just a few lines of code, we can get a ready-to-use list with a drag-to-reorder effect! And that's not all of it:
Each Reorder.Item is a motion component
Each Reorder.Item component in the list is able, out-of-the-box, to perform layout animations
Thus it's very easy to add a lot more animations on top of this component to build a truly delightful user experience. There are, however, two little catches that I only discovered when I started working with the Reorder components 👇
In the playground below, you will find a more advanced example that leverages Reorder.Group and Reorder.Item along with some other aspects of layout animations that we saw earlier:
Build new Three.js experiences ✨
Add new components to Design System 🌈
Check items off the list when you're done!
layout="position" is used on the content of each item to avoid distortions when they are selected and a layout animation is performed
Custom React styled-components use Reorder components through polymorphism
8
<Card.Body as={motion.div} layout="position">
10
id={`checkbox-${item.id}`}
11
aria-label="Mark as done"
12
checked={item.checked}
13
onChange={() => completeItem(item.id)}
15
<Text>{item.text}</Text>
Inline styles are used for the borderRadius of the item to avoid distortions when the item resizes
position: relative has been added as inline style to the Reorder.Item to fix overlap issues that occur while dragging elements of the list over one another
AnimatePresence is used to allow for exit animations when elements are removed from the list
5
exit={{ opacity: 0, transition: { duration: 0.2 } }}
12
width: item.checked ? '70%' : '100%',
The list and its sibling elements are wrapped in a LayoutGroup to perform smooth layout animations when the task list updates and changes the overall layout
2
<Reorder.Group axis="y" values={items} onReorder={setItems}>
9
<span>Check items off the list when you're done!</span>
Want to run this example yourself and hack on top of it? You can find the full implementation of this example on my blog's Github repository.
You now know pretty much everything there is to know about Framer Motion layout animations 🎉. Whether it's for some basic use cases, such as the Notification List we've seen in the first part, adding little details like the shared layout animations from the tabs components, to building reorderable lists with complex transitions: layout animations have no more secrets to you.
I hope this blog post can serve you as a guide/helper to make your own animations look absolutely perfect ✨, especially when working on the nitty-gritty details of your transitions. It may sound overkill to spend so much time reading and working around the issues we showcased in this blog post, but trust me, it's worth it!
Want to go further?
I'd suggest taking a look at some of the complex examples provided in the Framer Motion documentation. The team came up with very good examples such as this drag to reorder tabs component which contains every concept used in the task list example that I introduced in this blog post. After that, I'd try to see where you could sprinkle some layout animation magic on your own projects 🪄. There's no better way of learning than building things by yourself!
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。