Examples
Simple examples for animation, gestures, components, transforms and more.
Animation
The animate
prop can accept an object of values. When one of them changes, the motion
component will automatically animate to the new state.
The animation used can be configured using the transition
prop.
Keyframes
Set a value as an array and Motion will animate through each of these values in turn.
By default, each keyframe will be spaced evenly throughout the animation, but the exact timing and easing can be configured via the transition
property.
Variants
Variants are pre-defined visual states that a component can be in. By giving a component and its children variants
with matching names, whole React trees can be animated by changing a single prop.
By using variants, a parent can easily orchestrate the animations of its children with special transition
props like staggerChildren
.
Variants can also be dynamic functions that return different props based on data passed to each component's custom
prop.
Gesture animations
Motion provides whileHover
, whileTap
, whileDrag
and whileFocus
helper props, that will temporarily animate a component to a visual state while a gesture is active.
Like animate
, these can either be set as an object of properties (each with their own transition
prop), or the name of a variant.
Motion will also automatically handle the interplay of the two gestures, so if a component is being pressed while a hover gestures starts/stops, the whileTap
gesture will take priority.
Drag
A component can be made draggable with the addition of the drag
prop. Lock it to either axis by setting drag
to "x"
or "y"
.
The component can be constrained to a specific range, defined either in pixels, or by providing a ref
to another DOM element.
These constraints are elastic, and the strength of this elasticity can be configured with the dragElastic
prop.
MotionValues
Motion uses MotionValue
s to track the state and velocity of every animating value.
By making MotionValue
s yourself, you can create declarative chains of values that map from one into the other.
Scroll-triggered animations
Motion also provides a whileInView
prop that defines a visual state to animate to while a component is in the viewport.
Scroll-linked animations
The useScroll
hook provides four read-only MotionValue
s, two that return the viewport's x/y scroll position in pixels, and two that return it as progress value between 0
and 1
.
You can use these MotionValue
s to declaratively drive features like progress indicators or parallax effects.
Exit animations
In React, it's usually difficult to animate components once they've been removed from the DOM.
By wrapping motion
components with AnimatePresence
, they gain the use of an exit
property that can define either a set of values or a variant name to animate to before being removed.
Layout animations
To automatically animate the layout of a motion
component when its size or position changes, give it a layout
prop.
It doesn't matter what causes the layout change, whether it's a parent flexbox direction, width, top/right etc, the animation itself will be performed with transforms for maximum performance.
Shared layout animations
When a new motion
component is added, it can be automatically animated from another one by giving them both the same layoutId
prop.
Line drawing
Line drawing animations are made simple with the special pathLength
, pathSpacing
and pathOffset
properties available on many SVG elements.
Path morphing
With the help of external libraries like Flubber, it's possible to animate between very different SVG shapes.
React Router 6 page transitions
It's possible to mix React Router 6 with AnimatePresence
to make these fade-in/fade-out page transitions:
Or we can animate a privacy screen to hide the change of page:
Animating gradients
Framer Motion can animate properties that CSS can't, like gradients. For instance here we're animating mask-image
with a repeating-linear-gradient
to create this image-reveal effect.