Animating React Components with Framer Motion
Introduction
Framer Motion is an animation library for React. With its simple and intuitive syntax, we can easily apply animations and transitions to React components without breaking changes and rewriting. In the following paragraphs, I am going to introduce the basics of how to use Framer Motion.
Quick Start
Installation
npm install framer-motion
Importing
import { motion } from "framer-motion"
Animating Elements
To make an element into a motion element, we can simply add motion.
in the front of whatever DOM element it is, for example, a div element. This returns us a new motion component which will still be a div when it’s rendered to the DOM, however, it now comes with some extra attributes that we can use to animate the element.
animate
attribute. In this attribute, we need to pass an object which represents the different properties of this element that we want to animate.
{ fontSize: "50px", y: "-50vh" }
.
Initial Animation State
initial
attribute. Initial attribute allows us to set an initial state of an element to control how or where the animation starts and that adds flexibility to this animation since we can now control the start point and the end point.
{ rotateZ: 180 }
in initial attribute if we want to rotate an element which is up-side-down initially.
Transition Options
transition
attribute provides us a way to control how an animation works from the beginning to the end(initial → animation), such as its duration, delay and easing functions.
{ duration: 1, type: "spring" }
and delay displaying an element and let it fade-in very slowly by passing an object { delay: 1, duration: 5 }
.
Hover Animation
whileHover
attribute. The rules are the same as we use animate attribute.
{ scale: 1.1 }
in whileHover
attribute.
Variants
when
. It detects when the parent animation should occur in relation to any children elements which are animating as well.
when: "beforeChildren"
→ Parent animation will complete before any children animations occur.
staggerChildren
. It staggers the animation of each children elements by x amount so that all children elements animate one after another for a nice little effect.
Keyframes
For repeating an animation, instead of just increasing the amount of keyframes we can use a transition property called yoyo
. Yoyo property allows us to repeat an animation as many times as we want, even infinity.
Animating SVGs
By using pathLength
property, we can animate the SVG paths as though they are being drawn on the screen. PathLength is the actual path of the SVG and the path starts with pathLength 0 and ends with 1. With the transition properties duration and ease, we have more control of the animation.
Wrapping Up
In this article, we have seen how easy it is to animate React components with Framer Motion. However, there are a lot of features which are not covered in the article - gestures, dragging and much more. If you are interested in learning more about Framer Motion, I encourage you to check out Framer Motion Docs for more information.
Reference