Layout animations
Motion makes it simple to animate an element's size and position between different layouts. With animateLayout and the data-layout prop, you can perform layout animations, and by using data-layout-id you can create seamless "magic motion" effects between two separate elements.
Layout animations can animate previously unanimatable CSS values, like switching justify-content between flex-start and flex-end.
Install
animateLayout is in early access. You need to be a Motion+ member to install.
First, add the motion-plus package to your project using your private token.
Once installed, animateLayout can be imported via motion-plus/animate-layout.
animateLayout requires motion@12.29.2 or above.
Once out of alpha, animateLayout will be imported from the main "motion" package.
Usage
Import animateLayout from "motion-plus/animate-layout".
Wrap any layout change with animateLayout:
Any elements tagged with a data-layout will animate to their new size and position.
Configure transitions
Pass a Motion transition to animateLayout to configure the animation.
Scope layout animations
We can scope layout animations to a specific part of a page by passing a selector or element(s) as the first argument:
Shared element transitions
It's possible to animate from one element to another different element by removing an existing element and then adding a new element to the DOM with matching data-layout-id attributes:
Crossfade
You can also crossfade between two elements with matching data-layout-id attributes by leaving the initial element in the DOM.
In the React version of layout animations, we can also crossfade back to the original element by removing the new element using <AnimatePresence>. As there is not yet an <AnimatePresence> equivalent for vanilla JS, this is not yet possible.
Configure shared transitions
We can override the default layout transition using the .shared() method.
Pass it the data-layout-id of the matching pair we want to animate and a Motion transition:
Controls
animateLayout is an async function that returns AnimationPlaybackControls like animate.
Therefore, we can play, pause, set time, or power the animation with a scroll animation.


