Create Stunning Animations In React Using Framer Motion
Integrate framer motion to spice up your React App
Objectives Of This Article
In this article, we will learn the following:
- What is Framer Motion
- When to use it
- The basics of Framer Motion
- More advanced topics:
useCycle,AnimatePresenceand Variants.
Introduction
One of the best ways to spice up your web app and enhance user experience is to use animations. There are several libraries to bring animations into your React app. Arguably, one of the best is Framer Motion.
This library’s syntax is intuitive and easy to use. According to LogRocket, Framer Motion uses spring animations by default, which are popular since they are smoother and so look more natural.
Let’s get started!
Getting Started
First, we need to initialize our project repository:

When that’s done, let’s move on to installing the required libraries for this project.
Installing dependencies
In this article, we’ll use the following libraries:
framer-motion: The core library for bringing animations into our project.
To install this library, run the following terminal command:

When that’s done, we will learn the basics of Framer Motion.
Framer Motion: The Basics
Setup
In this section, we’ll cover the foundations of the Framer library. First, let’s import the motion module from the framer-motion library into our App.js file like so:

It’s time to code! Before getting started, find your return block in App.js:

Delete all the code between the div opening and closing tags. In the end, the return block should look like this:

As a first step, let’s work on basic animations with Framer using motion components coupled with the animate prop.
Motion components
Motion components are the core of Framer. The team has built a separate motion component for every HTML element. Consequently, you do not need to write extra boilerplate code to animate your elements.
For example, if you wanted to animate a p tag, you can replace it with its associated motion component like so:

This yields the following output:

As you can see, this is an ordinary paragraph element. The only difference is that it can use animations. To animate a div element, you would write motion.div.
Furthermore, note that we also use an animate prop in our motion.p component. What does the animate prop do?
To find it, go to App.js and find the line of code that we just wrote:

Now edit it like so:

- Line 1: Here, we are telling Framer to upscale the size of the text by two times.
This will be the result:

As you can see, when the motion.p mounted itself to the DOM, it played an animation while increasing its text size.
This means that the animate attribute essentially plays an animation while changing the properties of its component. It accepts key-value pairs that we want to animate.
Let’s step things up a bit. We can change the x coordinate of the motion.p, while simultaneously increasing its text size, like so:

- Line 1: Change the scale as well as the
xcoordinate during the animation.
This will be the output:

Voila! Our code works. As you can see, writing animations with Framer is really easy.
Let’s learn about the initial prop.
The initial prop
The initial attribute specifies the component’s starting or initial point. In other words, it tells the Framer element where to animate from. On the other hand, the animate prop tells the Framer element where to animate to.
To demonstrate its functionality, write the following code in App.js :

- Line
1: The initialxcoordinate of the component is100. During the animation process, change thexcoordinate to10. - Line
3: Closing tags of themotion.pelement.
In summary, this piece of code makes our piece of text travel from right to left.
Run the code. This will be the result:

Great! Our code works. Let’s experiment with the initial prop even more.
This piece of code will give our text a nice zoom-out effect:

- Line
1: The initial size of the text should be ten times its original dimensions. During the animation, revert the scale back to1.
This will be the output of the code:

Amazing! Our code works.
In the next section, we will customize our animations even more via the transition prop.
The transition prop
Now that we have made animations, it would be better if we had more granular control over them. What about changing its duration, changing its type, or even letting the animation loop forever? This is where the transition attribute comes in.
In App.js, write the following code:

- Line
1: Rotate the text by 180 degrees.
This will be the result:

As you can see, the animation was really swift. Let’s make it slower by changing the duration to two seconds.
First, backtrack to the code we just wrote:

Now change it, like so:

- Line
2: Change the duration of the animation to two seconds. By default, the duration is 0.3.
This will be the result:

Great! Our code works. As you can see, our animation is slower.
Let’s give our text a nice and slow zoom effect:

- Line
1: Upscale the text size by two times during the animation. - Line
2: Extend the duration of the animation to two seconds.

Great! As you can see, we got the desired output.
Let’s change the type of animation. For now, we will give our animation a spring-like effect.
In App.js, write the following code:

- Line 1: Rotate the text by 180 degrees.
- Line 2: Change the animation type to
spring.
This will be the result:

As you can see, our animation has a spring-like feel to it. This means that we were successful!
We can even alter the bounciness of this effect, like so:

- Line 2: Increase the bounciness of the animation to
0.8. The default value is0.25. If thebounceproperty is set to 1, then the animation is extremely bouncy, if it is0, then the animation will have no bounce.
This will be the output of the code:

Great! Our code works. Our animation has a nice spring effect. I suggest you play with the transition prop further by customizing other attributes, which you can find in the official documentation.
Let’s move on to creating animations for various gestures.
Handling gestures
In this article, we will cover two gestures:
- Hovering over the element which will be handled by the
whileHoverproperty. - Tapping or clicking the component which will be handled by the
whileTapattribute.
Let’s first use the whileHover prop. In App.js , write the following code:

- Line
1: Create amotion.buttoncomponent. Furthermore, we are telling Framer to upscale this element by 1.2 times when the user hovers over the button.
This will be the result if you hover over the button:

Great! Our code works.
For our next step, we use the whileTap prop. In App.js, write the following code:

- Line
1: When the user clicks or taps on this button, downscale its size by half.
This will be the result if you click on the button:

As you can see, when we clicked on the button, the component’s dimensions shrank. When we stop clicking the element, then the component restores itself to its original state.
Now that we’ve learned about the fundamentals of Framer Motion in React, it’s time to dive into the more complex topics in Framer Motion.
In the end, App.js should look like this:



























