{"id":21718,"date":"2022-10-26T07:14:16","date_gmt":"2022-10-26T14:14:16","guid":{"rendered":"https:\/\/coderpad.io\/?p=21718"},"modified":"2023-06-05T13:57:30","modified_gmt":"2023-06-05T20:57:30","slug":"how-to-animate-a-react-application-with-framer-motion","status":"publish","type":"post","link":"https:\/\/coderpad.io\/blog\/development\/how-to-animate-a-react-application-with-framer-motion\/","title":{"rendered":"How to Animate A React Application With Framer Motion"},"content":{"rendered":"\n<p>Animations on web pages involve moving elements on the screen to improve the visual experience of users. These animations are visually appealing and tend to draw users&#8217; attention to buttons, images, and other important information. Overall, it contributes to establishing a solid connection between users and the content on the screen.<\/p>\n\n\n\n<p>In the past, web pages were very plain and lacked interactivity because they were only intended to display information, and there was no capability to create appealing visual experiences. Things have changed dramatically in recent years, thanks to the availability of libraries and packages that make it easier to animate and add visual experiences to your web pages.<\/p>\n\n\n\n<p>This article will teach you how to use the Framer Motion library to animate a React application. Why should you use Framer Motion? And how it works by animating a mini-project.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Framer Motion?<\/h2>\n\n\n\n<p>Framer Motion is an open-source production-ready motion library for React. It allows you to add all forms of animations and transitions directly to Document Object Model (DOM) elements defined in your React components. Its syntax is easy to understand, and with a few lines of code, you will be able to implement awesome animations.<\/p>\n\n\n\n<p>Framer Motion uses an awesome API that gives you access to the motion component. You can plug this component into your React DOM elements giving you access to additional props for animation. For example, the <code>div<\/code> element will become <code>motion.div<\/code>.<\/p>\n\n\n\n<p>The Framer Motion also has beginner-friendly documentation with lots of practical examples and illustrations showing how to perform simple to complex forms of animations and transitions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How Framer Motion works<\/h3>\n\n\n\n<p>Framer Motion uses the <code>motion<\/code> component, which you can attach to any DOM element to implement an animation or transition effect.&nbsp;<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">motion.div<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span> An animated text <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">motion.div<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Nothing changes when you attach the <code>motion<\/code> component to your element. Instead, it supercharges that element with animation capabilities, giving you access to props, keys, and values you can use to add any animation. For example, you can access the <code>animate<\/code> prop, which takes in a dynamic value \u2014 an object which contains the different properties and values you want to animate.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">motion.div<\/span> <span class=\"hljs-attr\">animate<\/span>=<span class=\"hljs-string\">{{x:<\/span> <span class=\"hljs-attr\">-10<\/span>, <span class=\"hljs-attr\">y:<\/span> <span class=\"hljs-attr\">10<\/span>}}&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span> An animated text <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">motion.div<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The above animation will cause the <code>div<\/code> element to slide <code>10px<\/code> to the left and then <code>10px<\/code> to the bottom when it loads from the original position specified in style.&nbsp;<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\u2139\ufe0f When units are not specified, calculations are done in pixels. You can also specify your preferred units by attaching them. For example: <code>animate={{x:\"-10rem\"}}<\/code>.<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">Getting started with Framer Motion in React<\/h2>\n\n\n\n<p>Framer Motion is a popular library with over <a href=\"https:\/\/www.npmjs.com\/package\/framer-motion\" target=\"_blank\" rel=\"noopener\">1.6 million weekly downloads<\/a>. You can install the library into your React project using the command below in your terminal:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs shcb-wrap-lines\">$ npm i framer-motion<\/code><\/span><\/pre>\n\n\n<p>Once you have installed it, you can verify if it has been installed in your <code>package.json<\/code> file&#8217;s <code>dependencies<\/code> object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JSON \/ JSON with Comments\" data-shcb-language-slug=\"json\"><span><code class=\"hljs language-json shcb-wrap-lines\"><span class=\"hljs-string\">\"dependencies\"<\/span>: {\n    <span class=\"hljs-comment\">\/\/ ...<\/span>\n    <span class=\"hljs-attr\">\"framer-motion\"<\/span>: <span class=\"hljs-string\">\"^7.5.3\"<\/span>,\n    <span class=\"hljs-attr\">\"react\"<\/span>: <span class=\"hljs-string\">\"^18.2.0\"<\/span>,\n    <span class=\"hljs-comment\">\/\/ ...<\/span>\n},<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JSON \/ JSON with Comments<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">json<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Once you have installed the Framer Motion library, you can import the <code>motion<\/code> component into a component you wish to use within your project, and everything will work fine.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">import<\/span> { motion } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'framer-motion'<\/span>;\n\n<span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">motion.h1<\/span> <span class=\"hljs-attr\">animate<\/span>=<span class=\"hljs-string\">{{x:<\/span> <span class=\"hljs-attr\">10<\/span>, <span class=\"hljs-attr\">y:<\/span> <span class=\"hljs-attr\">20<\/span>}}&gt;<\/span>\n    An animated text\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">motion.h1<\/span>&gt;<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Animating elements in React<\/h2>\n\n\n\n<p>The <code>motion<\/code> component can be applied to any DOM element within your React component. You can apply it to the container elements, headings, text elements, images, and other elements. This motion component gives you access to props like <code>animate<\/code>, which you can use to adjust the color, size, position, display, and add other animation effects to the DOM element.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">motion.div<\/span>\n    <span class=\"hljs-attr\">animate<\/span>=<span class=\"hljs-string\">{{<\/span>\n        <span class=\"hljs-attr\">x:<\/span> <span class=\"hljs-attr\">10<\/span>,\n        <span class=\"hljs-attr\">backgroundColor:<\/span> '#<span class=\"hljs-attr\">fff<\/span>',\n        <span class=\"hljs-attr\">boxShadow:<\/span> '<span class=\"hljs-attr\">10px<\/span> <span class=\"hljs-attr\">10px<\/span> <span class=\"hljs-attr\">0<\/span> <span class=\"hljs-attr\">rgba<\/span>(<span class=\"hljs-attr\">0<\/span>, <span class=\"hljs-attr\">0<\/span>, <span class=\"hljs-attr\">0<\/span>, <span class=\"hljs-attr\">0.2<\/span>)',\n    }}\n&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span>&gt;<\/span>Hi<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">img<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">{myImage}<\/span> <span class=\"hljs-attr\">alt<\/span>=<span class=\"hljs-string\">\"\"<\/span> \/&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">motion.div<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the example above, the background color changes to white. A box shadow is added to the <code>div<\/code> when it loads, and the elements contained in the <code>div<\/code> moves <code>10px<\/code> to the right.<\/p>\n\n\n\n<p>It is also important to know that you can apply any form of animation based on CSS attributes. For example, you use <code>x<\/code>, <code>y<\/code>, and <code>z<\/code> to move elements similar to <code>translateX<\/code>, <code>translateY<\/code>, and <code>translateZ<\/code>.&nbsp;<\/p>\n\n\n\n<p>You can also scale the size of an element using the <code>scale<\/code> attribute for both <code>x<\/code> and <code>y<\/code> or <code>scaleX<\/code> and <code>scaleY<\/code> if you wish to apply separate values. Example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">motion.div<\/span> <span class=\"hljs-attr\">animate<\/span>=<span class=\"hljs-string\">{{<\/span> <span class=\"hljs-attr\">scale:<\/span> <span class=\"hljs-attr\">0.8<\/span> }} &gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span>&gt;<\/span>Hi<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">motion.img<\/span> <span class=\"hljs-attr\">animate<\/span>=<span class=\"hljs-string\">{{<\/span> <span class=\"hljs-attr\">scaleX:<\/span> <span class=\"hljs-attr\">1.2<\/span> }} <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">{myImage}<\/span> <span class=\"hljs-attr\">alt<\/span>=<span class=\"hljs-string\">\"\"<\/span> \/&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">motion.div<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The same applies to transformation values like <code>skew<\/code> and <code>rotate<\/code>. It&#8217;s also important to mention that it supports all forms of value types, such as numbers, strings, and colors in Hex, RGB, and HSLA as strings. You can also calculate values using the <code>calc()<\/code> function.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">motion.img<\/span> <span class=\"hljs-attr\">animate<\/span>=<span class=\"hljs-string\">{{<\/span> <span class=\"hljs-attr\">x:<\/span> \"<span class=\"hljs-attr\">calc<\/span>(<span class=\"hljs-attr\">100vw<\/span> <span class=\"hljs-attr\">-<\/span> <span class=\"hljs-attr\">50<\/span>%)\" }} <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">{myImage}<\/span> <span class=\"hljs-attr\">alt<\/span>=<span class=\"hljs-string\">\"\"<\/span> \/&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Setting initial animation state<\/h3>\n\n\n\n<p>At this point, you have only used the <code>animate<\/code> prop, which makes you animate elements from the default CSS style to your specified values. But this is not always what you want; sometimes, you want to specify where your animation starts and ends. For example, you might want your text to slide from the left of your screen to the middle.<\/p>\n\n\n\n<p>You can specify initial animation values using the <code>initial<\/code> props, which work similarly to the <code>animate<\/code> prop and uses similar attributes and values.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">motion.div<\/span>\n    <span class=\"hljs-attr\">initial<\/span>=<span class=\"hljs-string\">{{<\/span> <span class=\"hljs-attr\">opacity:<\/span> <span class=\"hljs-attr\">0<\/span>, <span class=\"hljs-attr\">x:<\/span> '<span class=\"hljs-attr\">-100vh<\/span>' }}\n    <span class=\"hljs-attr\">animate<\/span>=<span class=\"hljs-string\">{{<\/span> <span class=\"hljs-attr\">opacity:<\/span> <span class=\"hljs-attr\">1<\/span>, <span class=\"hljs-attr\">x:<\/span> <span class=\"hljs-attr\">0<\/span> }}\n&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span>&gt;<\/span>Hi<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n        Lorem ipsum dolor sit amet consectetur adipisicing elit. Deserunt\n        perspiciatis voluptates nihil dolores eum architecto eligendi\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">motion.div<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">How to add transition options to animation<\/h3>\n\n\n\n<p>You will notice that these animations happen fast to the extent that you might not see them unless you pay close attention.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"110\" style=\"aspect-ratio: 1170 \/ 110;\" width=\"1170\" autoplay loop muted src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/10\/1.mp4\"><\/video><figcaption class=\"wp-element-caption\"><em>The page animates a block of <strong>Lorem ipsum<\/strong> text by indenting it and reverting it to its original position.<\/em><\/figcaption><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>You can fix this by adding transition effects using the <code>transition<\/code> prop. This <code>transition<\/code> prop defines the type of animation used when animating between two values and how these values animate from one state to another. <\/p>\n\n\n\n<p>For example, if you add a <code>duration<\/code> of <code>1<\/code>, the animation will run for 1 second.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">motion.div<\/span>\n    <span class=\"hljs-attr\">initial<\/span>=<span class=\"hljs-string\">{{<\/span> <span class=\"hljs-attr\">opacity:<\/span> <span class=\"hljs-attr\">0<\/span>, <span class=\"hljs-attr\">x:<\/span> '<span class=\"hljs-attr\">-100vh<\/span>' }}\n    <span class=\"hljs-attr\">animate<\/span>=<span class=\"hljs-string\">{{<\/span> <span class=\"hljs-attr\">opacity:<\/span> <span class=\"hljs-attr\">1<\/span>, <span class=\"hljs-attr\">x:<\/span> <span class=\"hljs-attr\">0<\/span> }}\n    <span class=\"hljs-attr\">transition<\/span>=<span class=\"hljs-string\">{{<\/span> <span class=\"hljs-attr\">duration:<\/span> <span class=\"hljs-attr\">1<\/span> }}\n&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span>&gt;<\/span>Hi<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n        Lorem ipsum dolor sit amet consectetur adipisicing elit. Deserunt\n        perspiciatis voluptates nihil dolores eum architecto eligendi\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">motion.div<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/10\/img_6357c51e2fb10.gif\" alt=\"\"\/><figcaption class=\"wp-element-caption\">The page animates a block of <strong>Lorem ipsum<\/strong> text for a second by indenting it and reverting it to its original position.<\/figcaption><\/figure>\n\n\n\n<p>There are three major transition types, and they all have their specific effect; they include <code>Tween<\/code>, <code>Spring<\/code>, or <code>Inertia<\/code>. <code>Tween<\/code> is the default type for all animation, so if you want a spring form of animation, you will need to specify the transition type as <code>Spring<\/code>.&nbsp;<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">motion.div<\/span>\n    <span class=\"hljs-attr\">initial<\/span>=<span class=\"hljs-string\">{{<\/span> <span class=\"hljs-attr\">opacity:<\/span> <span class=\"hljs-attr\">0<\/span>, <span class=\"hljs-attr\">x:<\/span> '<span class=\"hljs-attr\">-100vh<\/span>' }}\n    <span class=\"hljs-attr\">animate<\/span>=<span class=\"hljs-string\">{{<\/span> <span class=\"hljs-attr\">opacity:<\/span> <span class=\"hljs-attr\">1<\/span>, <span class=\"hljs-attr\">x:<\/span> <span class=\"hljs-attr\">0<\/span> }}\n    <span class=\"hljs-attr\">transition<\/span>=<span class=\"hljs-string\">{{<\/span> <span class=\"hljs-attr\">type:<\/span> '<span class=\"hljs-attr\">spring<\/span>', <span class=\"hljs-attr\">duration:<\/span> <span class=\"hljs-attr\">1<\/span> }}\n&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span>&gt;<\/span>Hi<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n        Lorem ipsum dolor sit amet consectetur adipisicing elit. Deserunt\n        perspiciatis voluptates nihil dolores eum architecto eligendi\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">motion.div<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Each type has some attributes peculiar to them. For example, when you use the transition type of <code>spring<\/code>, you have access to an attribute like <code>bounce<\/code> which specifies how elastic the spring effect is on the element.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">motion.div<\/span>\n    <span class=\"hljs-attr\">initial<\/span>=<span class=\"hljs-string\">{{<\/span> <span class=\"hljs-attr\">opacity:<\/span> <span class=\"hljs-attr\">0<\/span>, <span class=\"hljs-attr\">x:<\/span> '<span class=\"hljs-attr\">-100vh<\/span>' }}\n    <span class=\"hljs-attr\">animate<\/span>=<span class=\"hljs-string\">{{<\/span> <span class=\"hljs-attr\">opacity:<\/span> <span class=\"hljs-attr\">1<\/span>, <span class=\"hljs-attr\">x:<\/span> <span class=\"hljs-attr\">0<\/span> }}\n    <span class=\"hljs-attr\">transition<\/span>=<span class=\"hljs-string\">{{<\/span> <span class=\"hljs-attr\">type:<\/span> '<span class=\"hljs-attr\">spring<\/span>', <span class=\"hljs-attr\">bounce:<\/span> <span class=\"hljs-attr\">0.6<\/span> }}\n&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span>&gt;<\/span>Hi<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n        Lorem ipsum dolor sit amet consectetur adipisicing elit. Deserunt\n        perspiciatis voluptates nihil dolores eum architecto eligendi\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">motion.div<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/10\/img_6357c5208b3d2.gif\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Spring animation.<\/figcaption><\/figure>\n\n\n\n<p>There are other attributes like <code>delay<\/code>, which you can use to set a delay time before the animation renders.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How to use keyframes and repeat animation in Framer Motion<\/h3>\n\n\n\n<p>Sometimes, you may want your elements to animate through several different animation values, which may be from left to right, depending on what you want. In CSS, this is handled with <code>keyframes<\/code>, but for Framer Motion, you can do this with a square bracket holding as many values as you want.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">motion.button<\/span>\n    <span class=\"hljs-attr\">animate<\/span>=<span class=\"hljs-string\">{{<\/span> <span class=\"hljs-attr\">x:<\/span> &#91;<span class=\"hljs-attr\">0<\/span>, <span class=\"hljs-attr\">-20<\/span>, <span class=\"hljs-attr\">20<\/span>, <span class=\"hljs-attr\">-20<\/span>, <span class=\"hljs-attr\">20<\/span>, <span class=\"hljs-attr\">0<\/span>] }}\n    <span class=\"hljs-attr\">transition<\/span>=<span class=\"hljs-string\">{{<\/span> <span class=\"hljs-attr\">delay:<\/span> <span class=\"hljs-attr\">1<\/span> }}\n&gt;<\/span>\n    My Button\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">motion.button<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-video\"><video height=\"150\" style=\"aspect-ratio: 1116 \/ 150;\" width=\"1116\" autoplay loop muted src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/10\/1-1.mp4\"><\/video><figcaption class=\"wp-element-caption\">The page animates a button to the left after a second from the initial render.<\/figcaption><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Note that in the example above, the animation waited for 1 second before moving the button to the left <code>20px<\/code> and right <code>20px<\/code> twice before returning to its original position. This can be applied to any animation transform attribute such as <code>x,<\/code> <code>y<\/code>, <code>scale<\/code>, <code>rotate<\/code> and others.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">motion.button<\/span>\n    <span class=\"hljs-attr\">animate<\/span>=<span class=\"hljs-string\">{{<\/span> <span class=\"hljs-attr\">scale:<\/span> &#91;<span class=\"hljs-attr\">1<\/span>, <span class=\"hljs-attr\">1.2<\/span>, <span class=\"hljs-attr\">1<\/span>, <span class=\"hljs-attr\">1.2<\/span>, <span class=\"hljs-attr\">1<\/span>] }}\n    <span class=\"hljs-attr\">transition<\/span>=<span class=\"hljs-string\">{{<\/span> <span class=\"hljs-attr\">delay:<\/span> <span class=\"hljs-attr\">1<\/span> }}\n&gt;<\/span>\n    My Button\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">motion.button<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>You will notice that these animations stop after it has completed the number of times specified. The example above will start at 1, which is the default. It will then scale up and down twice to its default value. <\/p>\n\n\n\n<p>However, you may want the animation effect to repeat by using the <code>transition<\/code> property <code>yoyo<\/code> and set its value to <code>Infinity<\/code> or the number of times you wish your animation repeats. <\/p>\n\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\ud83d\udca1 The transition property <code>yoyo<\/code> lets you specify the number of times you want an animation to repeat.<\/p>\n<\/blockquote>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">motion.button<\/span>\n    <span class=\"hljs-attr\">animate<\/span>=<span class=\"hljs-string\">{{<\/span> <span class=\"hljs-attr\">scale:<\/span> <span class=\"hljs-attr\">1.2<\/span> }}\n    <span class=\"hljs-attr\">transition<\/span>=<span class=\"hljs-string\">{{<\/span> <span class=\"hljs-attr\">delay:<\/span> <span class=\"hljs-attr\">1<\/span>, <span class=\"hljs-attr\">yoyo:<\/span> <span class=\"hljs-attr\">Infinity<\/span> }}\n&gt;<\/span>\n    My Button\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">motion.button<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the code block above, you will notice you no longer need the square bracket. All you need is the value it should change to when it leaves the default, <code>1<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/10\/img_6357c5277e6c8.gif\" alt=\"\"\/><figcaption class=\"wp-element-caption\">The page animates a <strong>Generate Quote<\/strong> button once the button is hovered on.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Implement hover animation gestures<\/h2>\n\n\n\n<p>You often want to apply hover gestures to buttons, cards, and other elements to draw the user&#8217;s attention. You might want a button to scale up and down when a user hovers over the button instead of just scaling up and down without any action from the user. To do this, you can use the <code>whileHover<\/code> prop, which works similarly to previous props.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">motion.button<\/span>\n    <span class=\"hljs-attr\">whileHover<\/span>=<span class=\"hljs-string\">{{<\/span>\n        <span class=\"hljs-attr\">scale:<\/span> <span class=\"hljs-attr\">1.2<\/span>,\n        <span class=\"hljs-attr\">transition:<\/span> { <span class=\"hljs-attr\">yoyo:<\/span> <span class=\"hljs-attr\">Infinity<\/span> },\n    }}\n&gt;<\/span>\n    My Button\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">motion.button<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\u2139\ufe0f The transition property for <code>whileHover<\/code> prop is placed within the curly braces and not outside the <code>whileHover<\/code> prop.<\/p>\n<\/blockquote>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/10\/img_6357c52b64af9.gif\" alt=\"\"\/><figcaption class=\"wp-element-caption\">The page animation transitions once the <strong>Generate Quote <\/strong>button is hovered on.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Variants: A better way to use Framer Motion in React<\/h2>\n\n\n\n<p>At this point, you have learned the basics of Framer Motion, how it works and how you could use it to implement animations directly on your DOM elements. You must have started asking yourself why you should add all animation properties and values directly on each element because it tends to make your code clumsy and not easy to understand.<\/p>\n\n\n\n<p>This is where <strong>variants<\/strong> come in. You use variants to remove the props object and place them outside your component, so you only use the object names within your DOM element. This also helps you use the same animation effect for multiple elements within your component to avoid repetition.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">import<\/span> { Link } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react-router-dom'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> logo <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/..\/images\/logo.svg'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { motion } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'framer-motion'<\/span>;\n\n<span class=\"hljs-keyword\">const<\/span> headerVariants = {\n    <span class=\"hljs-attr\">initial<\/span>: {\n        <span class=\"hljs-attr\">y<\/span>: <span class=\"hljs-string\">'-100vh'<\/span>,\n    },\n    <span class=\"hljs-attr\">animate<\/span>: {\n        <span class=\"hljs-attr\">y<\/span>: <span class=\"hljs-number\">0<\/span>,\n        <span class=\"hljs-attr\">transition<\/span>: {\n            <span class=\"hljs-attr\">delay<\/span>: <span class=\"hljs-number\">0.5<\/span>,\n            <span class=\"hljs-attr\">duration<\/span>: <span class=\"hljs-number\">0.5<\/span>,\n            <span class=\"hljs-attr\">type<\/span>: <span class=\"hljs-string\">'spring'<\/span>,\n        },\n    },\n};\n\n<span class=\"hljs-keyword\">const<\/span> Header = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n    <span class=\"hljs-keyword\">return<\/span> (\n        <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"container\"<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">motion.div<\/span>\n                <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"header\"<\/span>\n                <span class=\"hljs-attr\">variants<\/span>=<span class=\"hljs-string\">{headerVariants}<\/span>\n                <span class=\"hljs-attr\">initial<\/span>=<span class=\"hljs-string\">\"initial\"<\/span>\n                <span class=\"hljs-attr\">animate<\/span>=<span class=\"hljs-string\">\"animate\"<\/span>\n            &gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Link<\/span> <span class=\"hljs-attr\">to<\/span>=<span class=\"hljs-string\">\"\/\"<\/span>&gt;<\/span>\n                    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">img<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">{logo}<\/span> <span class=\"hljs-attr\">alt<\/span>=<span class=\"hljs-string\">\"\"<\/span> \/&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">Link<\/span>&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span>&gt;<\/span>Quotes Circle<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">motion.div<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/span>\n    );\n};\n\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> Header;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the code block above, an object (variant) is created to hold all the animation properties and values. This object contained two child objects, one to hold the <code>initial<\/code> animation and the second to hold the final animation (<code>animate<\/code>). You can give them any name:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> headerVariants = {\n    <span class=\"hljs-attr\">initial<\/span>: {\n        <span class=\"hljs-attr\">y<\/span>: <span class=\"hljs-string\">'-100vh'<\/span>,\n    },\n    <span class=\"hljs-attr\">animate<\/span>: {\n        <span class=\"hljs-attr\">y<\/span>: <span class=\"hljs-number\">0<\/span>,\n        <span class=\"hljs-attr\">transition<\/span>: {\n            <span class=\"hljs-attr\">delay<\/span>: <span class=\"hljs-number\">0.5<\/span>,\n            <span class=\"hljs-attr\">duration<\/span>: <span class=\"hljs-number\">0.5<\/span>,\n            <span class=\"hljs-attr\">type<\/span>: <span class=\"hljs-string\">'spring'<\/span>,\n        },\n    },\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To make use of this variant in your React DOM elements, then you will use the <code>variants<\/code> prop and pass the variant name into it. Also, you will assign the <code>initial<\/code> and <code>animate<\/code> prop with the name you used when creating your variant, <strong>but this time as a string and not a dynamic value<\/strong>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">motion.div<\/span>\n    <span class=\"hljs-attr\">variants<\/span>=<span class=\"hljs-string\">{headerVariants}<\/span>\n    <span class=\"hljs-attr\">initial<\/span>=<span class=\"hljs-string\">\"initial\"<\/span>\n    <span class=\"hljs-attr\">animate<\/span>=<span class=\"hljs-string\">\"animate\"<\/span>\n&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Link<\/span> <span class=\"hljs-attr\">to<\/span>=<span class=\"hljs-string\">\"\/\"<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">img<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">{logo}<\/span> <span class=\"hljs-attr\">alt<\/span>=<span class=\"hljs-string\">\"\"<\/span> \/&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">Link<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span>&gt;<\/span>Quotes Circle<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">motion.div<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>You can have as many variants as possible, and if you use the same <code>initial<\/code> and <code>animate<\/code> value, you would not need to declare that for children elements in your component.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> headerVariants = {\n    <span class=\"hljs-attr\">initial<\/span>: {\n        <span class=\"hljs-attr\">y<\/span>: <span class=\"hljs-string\">'-100vh'<\/span>,\n    },\n    <span class=\"hljs-attr\">animate<\/span>: {\n        <span class=\"hljs-attr\">y<\/span>: <span class=\"hljs-number\">0<\/span>,\n        <span class=\"hljs-attr\">transition<\/span>: {\n            <span class=\"hljs-attr\">delay<\/span>: <span class=\"hljs-number\">0.5<\/span>,\n            <span class=\"hljs-attr\">duration<\/span>: <span class=\"hljs-number\">0.5<\/span>,\n            <span class=\"hljs-attr\">type<\/span>: <span class=\"hljs-string\">'spring'<\/span>,\n        },\n    },\n};\n\n<span class=\"hljs-keyword\">const<\/span> logoVariants = {\n    <span class=\"hljs-attr\">initial<\/span>: {\n        <span class=\"hljs-attr\">x<\/span>: <span class=\"hljs-string\">'-100vw'<\/span>,\n    },\n    <span class=\"hljs-attr\">animate<\/span>: {\n        <span class=\"hljs-attr\">x<\/span>: <span class=\"hljs-number\">0<\/span>,\n        <span class=\"hljs-attr\">transition<\/span>: {\n            <span class=\"hljs-attr\">delay<\/span>: <span class=\"hljs-number\">1<\/span>,\n        },\n    },\n};\n\n<span class=\"hljs-keyword\">const<\/span> Header = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n    <span class=\"hljs-keyword\">return<\/span> (\n        <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"container\"<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">motion.div<\/span>\n                <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"header\"<\/span>\n                <span class=\"hljs-attr\">variants<\/span>=<span class=\"hljs-string\">{headerVariants}<\/span>\n                <span class=\"hljs-attr\">initial<\/span>=<span class=\"hljs-string\">\"initial\"<\/span>\n                <span class=\"hljs-attr\">animate<\/span>=<span class=\"hljs-string\">\"animate\"<\/span>\n            &gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Link<\/span> <span class=\"hljs-attr\">to<\/span>=<span class=\"hljs-string\">\"\/\"<\/span>&gt;<\/span>\n                    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">motion.img<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">{logo}<\/span> <span class=\"hljs-attr\">variants<\/span>=<span class=\"hljs-string\">{logoVariants}<\/span> \/&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">Link<\/span>&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span>&gt;<\/span>Quotes Circle<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">motion.div<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/span>\n    );\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you look at the logo image element, you will notice the <code>variants<\/code> prop was only defined. There is no need to define the <code>initial<\/code> and <code>animate<\/code> prop because it has the same name as the previous one you already declared in the parent element.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to animate routes in React with Framer Motion<\/h2>\n\n\n\n<p>When building applications that have more than one page, you will want to add animation between each page. These animations may not be conspicuous but can add a nice experience rather than having content suddenly leave the screen.<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"634\" style=\"aspect-ratio: 1114 \/ 634;\" width=\"1114\" autoplay loop muted src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/10\/1-2.mp4\"><\/video><figcaption class=\"wp-element-caption\">The page animates a new component once the route changes.<\/figcaption><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>The GIF above almost looks like the routes are animated, but you will notice that it only animates in but not out. This means that the components disappear when they unmount, so you will want to add an animation instead.<\/p>\n\n\n\n<p>This is done with the <code>AnimatePresence<\/code> component, which notifies components when they will be unmounted and allows them to defer that unmounting until after the animation is complete. This animation is added with the exit prop to your component&#8217;s parent element.<\/p>\n\n\n\n<p>To implement this, you will first import <code>AnimatePresence<\/code> component to the <code>App.js<\/code> file where you configured your routes or if it&#8217;s in the <code>index.js<\/code> file. Once that is done, you will wrap the <code>Routes<\/code> component with the <code>AnimatePresence<\/code> component:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">import<\/span> { Routes, Route } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react-router-dom'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> Home <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/pages\/Home'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> QuotesPage <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/pages\/QuotesPage'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> Header <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/components\/Header'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { AnimatePresence } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'framer-motion'<\/span>;\n\n<span class=\"hljs-keyword\">const<\/span> App = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n\n    <span class=\"hljs-keyword\">return<\/span> (\n    <span class=\"xml\"><span class=\"hljs-tag\">&lt;&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Header<\/span> \/&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">AnimatePresence<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Routes<\/span> <span class=\"hljs-attr\">location<\/span>=<span class=\"hljs-string\">{location}<\/span> <span class=\"hljs-attr\">key<\/span>=<span class=\"hljs-string\">{location.key}<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Route<\/span> <span class=\"hljs-attr\">path<\/span>=<span class=\"hljs-string\">\"\/\"<\/span> <span class=\"hljs-attr\">element<\/span>=<span class=\"hljs-string\">{<\/span>&lt;<span class=\"hljs-attr\">Home<\/span> \/&gt;<\/span>} \/&gt;\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Route<\/span> <span class=\"hljs-attr\">path<\/span>=<span class=\"hljs-string\">\"\/quote\"<\/span> <span class=\"hljs-attr\">element<\/span>=<span class=\"hljs-string\">{<\/span>&lt;<span class=\"hljs-attr\">QuotesPage<\/span> \/&gt;<\/span>} \/&gt;\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">Routes<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">AnimatePresence<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/&gt;<\/span><\/span>\n    );\n};\n\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> App;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Then you will need to use <code>useLocation<\/code> router hook to know when the route changes. This will be attached to the <code>Route<\/code> component:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">import<\/span> { Routes, Route, useLocation } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react-router-dom'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> Home <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/pages\/Home'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> QuotesPage <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/pages\/QuotesPage'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> Header <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/components\/Header'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { AnimatePresence } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'framer-motion'<\/span>;\n\n<span class=\"hljs-keyword\">const<\/span> App = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n    <span class=\"hljs-keyword\">const<\/span> location = useLocation();\n\n    <span class=\"hljs-keyword\">return<\/span> (\n    <span class=\"xml\"><span class=\"hljs-tag\">&lt;&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Header<\/span> \/&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">AnimatePresence<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Routes<\/span> <span class=\"hljs-attr\">location<\/span>=<span class=\"hljs-string\">{location}<\/span> <span class=\"hljs-attr\">key<\/span>=<span class=\"hljs-string\">{location.key}<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Route<\/span> <span class=\"hljs-attr\">path<\/span>=<span class=\"hljs-string\">\"\/\"<\/span> <span class=\"hljs-attr\">element<\/span>=<span class=\"hljs-string\">{<\/span>&lt;<span class=\"hljs-attr\">Home<\/span> \/&gt;<\/span>} \/&gt;\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Route<\/span> <span class=\"hljs-attr\">path<\/span>=<span class=\"hljs-string\">\"\/quote\"<\/span> <span class=\"hljs-attr\">element<\/span>=<span class=\"hljs-string\">{<\/span>&lt;<span class=\"hljs-attr\">QuotesPage<\/span> \/&gt;<\/span>} \/&gt;\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">Routes<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">AnimatePresence<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/&gt;<\/span><\/span>\n    );\n};\n\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> App;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>At this point, you can now add exit animation to all your component&#8217;s parent elements, and the exit animation will work very fine. <\/p>\n\n\n\n<p>For example, if you want to add it to the <code>Home<\/code> component, which is a route as seen above. You can use <code>variants<\/code> or add the animation object directly to the <code>exit<\/code> prop.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-comment\">\/\/ ...<\/span>\n<span class=\"hljs-keyword\">import<\/span> { motion } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'framer-motion'<\/span>;\n\n<span class=\"hljs-keyword\">const<\/span> Home = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n    <span class=\"hljs-comment\">\/\/ ...<\/span>\n\n    <span class=\"hljs-keyword\">const<\/span> containerVariants = {\n        <span class=\"hljs-attr\">hidden<\/span>: {\n            <span class=\"hljs-attr\">opacity<\/span>: <span class=\"hljs-number\">0<\/span>,\n            <span class=\"hljs-attr\">x<\/span>: <span class=\"hljs-string\">'100vw'<\/span>,\n        },\n        <span class=\"hljs-attr\">visible<\/span>: {\n            <span class=\"hljs-attr\">opacity<\/span>: <span class=\"hljs-number\">1<\/span>,\n            <span class=\"hljs-attr\">x<\/span>: <span class=\"hljs-number\">0<\/span>,\n            <span class=\"hljs-attr\">transition<\/span>: {\n                <span class=\"hljs-attr\">type<\/span>: <span class=\"hljs-string\">'spring'<\/span>,\n                <span class=\"hljs-attr\">mass<\/span>: <span class=\"hljs-number\">0.4<\/span>,\n                <span class=\"hljs-attr\">damping<\/span>: <span class=\"hljs-number\">8<\/span>,\n                <span class=\"hljs-attr\">when<\/span>: <span class=\"hljs-string\">'beforeChildren'<\/span>,\n                <span class=\"hljs-attr\">staggerChildren<\/span>: <span class=\"hljs-number\">0.4<\/span>,\n            },\n        },\n        <span class=\"hljs-attr\">exit<\/span>: {\n            <span class=\"hljs-attr\">x<\/span>: <span class=\"hljs-string\">'-100vw'<\/span>,\n            <span class=\"hljs-attr\">transition<\/span>: {\n                <span class=\"hljs-attr\">ease<\/span>: <span class=\"hljs-string\">'easeInOut'<\/span>,\n            },\n        },\n    };\n\n    <span class=\"hljs-keyword\">return<\/span> (\n        <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">motion.div<\/span>\n            <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"container\"<\/span>\n            <span class=\"hljs-attr\">variants<\/span>=<span class=\"hljs-string\">{containerVariants}<\/span>\n            <span class=\"hljs-attr\">initial<\/span>=<span class=\"hljs-string\">\"hidden\"<\/span>\n            <span class=\"hljs-attr\">animate<\/span>=<span class=\"hljs-string\">\"visible\"<\/span>\n            <span class=\"hljs-attr\">exit<\/span>=<span class=\"hljs-string\">\"exit\"<\/span>\n        &gt;<\/span>\n            { \/\/ ... component elements }\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">motion.div<\/span>&gt;<\/span><\/span>\n    );\n};\n\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> Home;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>At this point, your routes will be animated when any component leaves the screen. It will animate out by sliding to the left as specified in the exit object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-23\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">exit: {\n    <span class=\"hljs-attr\">x<\/span>: <span class=\"hljs-string\">'-100vw'<\/span>,\n    <span class=\"hljs-attr\">transition<\/span>: {\n        <span class=\"hljs-attr\">ease<\/span>: <span class=\"hljs-string\">'easeInOut'<\/span>,\n    },\n},<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-23\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>But you will notice that the animation of the new component already happens when the previous component leaves the screen. You can fix this by adding an <code>exitBeforeEnter<\/code> prop to the <code>&lt;AnimatePresence<\/code>&gt; component you used to wrap the <code>App.js<\/code> component.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-24\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-comment\">\/\/ ... imports<\/span>\n<span class=\"hljs-keyword\">const<\/span> App = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n    <span class=\"hljs-keyword\">const<\/span> location = useLocation();\n    <span class=\"hljs-keyword\">return<\/span> (\n    <span class=\"xml\"><span class=\"hljs-tag\">&lt;&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Header<\/span> \/&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">AnimatePresence<\/span> <span class=\"hljs-attr\">exitBeforeEnter<\/span>&gt;<\/span>\n        { \/\/ ... routes }\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">AnimatePresence<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/&gt;<\/span><\/span>\n    );\n};\n\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> App;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-24\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-video\"><video height=\"634\" style=\"aspect-ratio: 1114 \/ 634;\" width=\"1114\" autoplay loop muted src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/10\/1-3.mp4\"><\/video><figcaption class=\"wp-element-caption\">Each page is animated once the route is changed.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">How to animate SVGs with Framer Motion<\/h2>\n\n\n\n<p>Framer Motion allows you to animate your Scalable Vector Graphics (SVGs) via their path (A way of drawing your SVGs). This is possible by attaching the motion component to the path element of your SVG and then adding your preferred animation alongside the <code>pathlength<\/code> property with a value of <code>0<\/code> and <code>1<\/code> for when the animation starts and when it ends.<\/p>\n\n\n\n<p>Suppose you have an SVG, you will notice that these SVGs usually have a path or paths defined with so many attributes and values:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-25\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> AnimateSVG = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n    <span class=\"hljs-keyword\">return<\/span> (\n        <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"container\"<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">svg<\/span>\n                <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"my-svg\"<\/span>\n                <span class=\"hljs-attr\">viewBox<\/span>=<span class=\"hljs-string\">\"0 0 256 256\"<\/span>\n                <span class=\"hljs-attr\">xmlns<\/span>=<span class=\"hljs-string\">\"http:\/\/www.w3.org\/2000\/svg\"<\/span>\n            &gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">rect<\/span> <span class=\"hljs-attr\">fill<\/span>=<span class=\"hljs-string\">\"none\"<\/span> <span class=\"hljs-attr\">height<\/span>=<span class=\"hljs-string\">\"256\"<\/span> <span class=\"hljs-attr\">width<\/span>=<span class=\"hljs-string\">\"256\"<\/span> \/&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">path<\/span>\n                    <span class=\"hljs-attr\">d<\/span>=<span class=\"hljs-string\">\"M108,144H40a8,8,0,0,1-8-8V72a8,8,0,0,1,8-8h60a8,8,0,0,1,8,8v88a40,40,0,0,1-40,40\"<\/span>\n                    <span class=\"hljs-attr\">fill<\/span>=<span class=\"hljs-string\">\"none\"<\/span>\n                    <span class=\"hljs-attr\">stroke<\/span>=<span class=\"hljs-string\">\"#000\"<\/span>\n                    <span class=\"hljs-attr\">stroke-linecap<\/span>=<span class=\"hljs-string\">\"round\"<\/span>\n                    <span class=\"hljs-attr\">stroke-linejoin<\/span>=<span class=\"hljs-string\">\"round\"<\/span>\n                    <span class=\"hljs-attr\">stroke-width<\/span>=<span class=\"hljs-string\">\"12\"<\/span>\n                \/&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">path<\/span>\n                    <span class=\"hljs-attr\">d<\/span>=<span class=\"hljs-string\">\"M224,144H156a8,8,0,0,1-8-8V72a8,8,0,0,1,8-8h60a8,8,0,0,1,8,8v88a40,40,0,0,1-40,40\"<\/span>\n                    <span class=\"hljs-attr\">fill<\/span>=<span class=\"hljs-string\">\"none\"<\/span>\n                    <span class=\"hljs-attr\">stroke<\/span>=<span class=\"hljs-string\">\"#000\"<\/span>\n                    <span class=\"hljs-attr\">stroke-linecap<\/span>=<span class=\"hljs-string\">\"round\"<\/span>\n                    <span class=\"hljs-attr\">stroke-linejoin<\/span>=<span class=\"hljs-string\">\"round\"<\/span>\n                    <span class=\"hljs-attr\">stroke-width<\/span>=<span class=\"hljs-string\">\"12\"<\/span>\n                \/&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">svg<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/span>\n    );\n};\n\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> AnimateSVG;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-25\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>You can attach the motion component to the path element and then use the <code>initial<\/code> and <code>animate<\/code> props to add animation to the path:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-26\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">import<\/span> { motion } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'framer-motion'<\/span>;\n\n<span class=\"hljs-keyword\">const<\/span> AnimateSVG = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n    <span class=\"hljs-keyword\">return<\/span> (\n        <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"container\"<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">svg<\/span>\n                <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"my-svg\"<\/span>\n                <span class=\"hljs-attr\">viewBox<\/span>=<span class=\"hljs-string\">\"0 0 256 256\"<\/span>\n                <span class=\"hljs-attr\">xmlns<\/span>=<span class=\"hljs-string\">\"http:\/\/www.w3.org\/2000\/svg\"<\/span>\n            &gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">rect<\/span> <span class=\"hljs-attr\">fill<\/span>=<span class=\"hljs-string\">\"none\"<\/span> <span class=\"hljs-attr\">height<\/span>=<span class=\"hljs-string\">\"256\"<\/span> <span class=\"hljs-attr\">width<\/span>=<span class=\"hljs-string\">\"256\"<\/span> \/&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">motion.path<\/span>\n                    <span class=\"hljs-attr\">d<\/span>=<span class=\"hljs-string\">\"M108,144H40a8,8,0,0,1-8-8V72a8,8,0,0,1,8-8h60a8,8,0,0,1,8,8v88a40,40,0,0,1-40,40\"<\/span>\n                    <span class=\"hljs-attr\">fill<\/span>=<span class=\"hljs-string\">\"none\"<\/span>\n                    <span class=\"hljs-attr\">stroke<\/span>=<span class=\"hljs-string\">\"#000\"<\/span>\n                    <span class=\"hljs-attr\">stroke-linecap<\/span>=<span class=\"hljs-string\">\"round\"<\/span>\n                    <span class=\"hljs-attr\">stroke-linejoin<\/span>=<span class=\"hljs-string\">\"round\"<\/span>\n                    <span class=\"hljs-attr\">stroke-width<\/span>=<span class=\"hljs-string\">\"12\"<\/span>\n                    <span class=\"hljs-attr\">initial<\/span>=<span class=\"hljs-string\">{{<\/span> <span class=\"hljs-attr\">pathLength:<\/span> <span class=\"hljs-attr\">0<\/span> }}\n                    <span class=\"hljs-attr\">animate<\/span>=<span class=\"hljs-string\">{{<\/span> <span class=\"hljs-attr\">pathLength:<\/span> <span class=\"hljs-attr\">1<\/span> }}\n                    <span class=\"hljs-attr\">transition<\/span>=<span class=\"hljs-string\">{{<\/span> <span class=\"hljs-attr\">duration:<\/span> <span class=\"hljs-attr\">2<\/span> }}\n                \/&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">motion.path<\/span>\n                    <span class=\"hljs-attr\">d<\/span>=<span class=\"hljs-string\">\"M224,144H156a8,8,0,0,1-8-8V72a8,8,0,0,1,8-8h60a8,8,0,0,1,8,8v88a40,40,0,0,1-40,40\"<\/span>\n                    <span class=\"hljs-attr\">fill<\/span>=<span class=\"hljs-string\">\"none\"<\/span>\n                    <span class=\"hljs-attr\">stroke<\/span>=<span class=\"hljs-string\">\"#000\"<\/span>\n                    <span class=\"hljs-attr\">stroke-linecap<\/span>=<span class=\"hljs-string\">\"round\"<\/span>\n                    <span class=\"hljs-attr\">stroke-linejoin<\/span>=<span class=\"hljs-string\">\"round\"<\/span>\n                    <span class=\"hljs-attr\">stroke-width<\/span>=<span class=\"hljs-string\">\"12\"<\/span>\n                    <span class=\"hljs-attr\">initial<\/span>=<span class=\"hljs-string\">{{<\/span> <span class=\"hljs-attr\">pathLength:<\/span> <span class=\"hljs-attr\">0<\/span> }}\n                    <span class=\"hljs-attr\">animate<\/span>=<span class=\"hljs-string\">{{<\/span> <span class=\"hljs-attr\">pathLength:<\/span> <span class=\"hljs-attr\">1<\/span> }}\n                    <span class=\"hljs-attr\">transition<\/span>=<span class=\"hljs-string\">{{<\/span> <span class=\"hljs-attr\">duration:<\/span> <span class=\"hljs-attr\">2<\/span> }}\n                \/&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">svg<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/span>\n    );\n};\n\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> AnimateSVG;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-26\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the code block above, you will notice that <code>pathLength<\/code> is used to add animation to the SVG. The initial animation is set to <code>0<\/code>, meaning nothing shows, and then within 2 seconds, the SVG will draw.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/10\/img_6357c54fd1a22.gif\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Animated SVG.<\/figcaption><\/figure>\n\n\n\n<p>Instead of adding the animation directly, you can use <code>variants<\/code> and add that single variant to both paths. You can also add more forms of animation, such as <code>opacity<\/code> if you wish.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-27\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">import<\/span> { motion } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'framer-motion'<\/span>;\n\n<span class=\"hljs-keyword\">const<\/span> AnimateSVG = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n    <span class=\"hljs-keyword\">const<\/span> pathVariants = {\n        <span class=\"hljs-attr\">hidden<\/span>: {\n            <span class=\"hljs-attr\">opacity<\/span>: <span class=\"hljs-number\">0<\/span>,\n            <span class=\"hljs-attr\">pathLength<\/span>: <span class=\"hljs-number\">0<\/span>,\n        },\n        <span class=\"hljs-attr\">visible<\/span>: {\n            <span class=\"hljs-attr\">opacity<\/span>: <span class=\"hljs-number\">1<\/span>,\n            <span class=\"hljs-attr\">pathLength<\/span>: <span class=\"hljs-number\">1<\/span>,\n            <span class=\"hljs-attr\">transition<\/span>: {\n                <span class=\"hljs-attr\">duration<\/span>: <span class=\"hljs-number\">2<\/span>,\n                <span class=\"hljs-attr\">ease<\/span>: <span class=\"hljs-string\">'easeInOut'<\/span>,\n            },\n        },\n    };\n\n    <span class=\"hljs-keyword\">return<\/span> (\n        <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"container\"<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">motion.svg<\/span>\n                <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"my-svg\"<\/span>\n                <span class=\"hljs-attr\">viewBox<\/span>=<span class=\"hljs-string\">\"0 0 256 256\"<\/span>\n                <span class=\"hljs-attr\">xmlns<\/span>=<span class=\"hljs-string\">\"http:\/\/www.w3.org\/2000\/svg\"<\/span>\n            &gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">rect<\/span> <span class=\"hljs-attr\">fill<\/span>=<span class=\"hljs-string\">\"none\"<\/span> <span class=\"hljs-attr\">height<\/span>=<span class=\"hljs-string\">\"256\"<\/span> <span class=\"hljs-attr\">width<\/span>=<span class=\"hljs-string\">\"256\"<\/span> \/&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">motion.path<\/span>\n                    <span class=\"hljs-attr\">d<\/span>=<span class=\"hljs-string\">\"M108,144H40a8,8,0,0,1-8-8V72a8,8,0,0,1,8-8h60a8,8,0,0,1,8,8v88a40,40,0,0,1-40,40\"<\/span>\n                    <span class=\"hljs-attr\">fill<\/span>=<span class=\"hljs-string\">\"none\"<\/span>\n                    <span class=\"hljs-attr\">stroke<\/span>=<span class=\"hljs-string\">\"#000\"<\/span>\n                    <span class=\"hljs-attr\">stroke-linecap<\/span>=<span class=\"hljs-string\">\"round\"<\/span>\n                    <span class=\"hljs-attr\">stroke-linejoin<\/span>=<span class=\"hljs-string\">\"round\"<\/span>\n                    <span class=\"hljs-attr\">stroke-width<\/span>=<span class=\"hljs-string\">\"12\"<\/span>\n                    <span class=\"hljs-attr\">variants<\/span>=<span class=\"hljs-string\">{pathVariants}<\/span>\n                    <span class=\"hljs-attr\">initial<\/span>=<span class=\"hljs-string\">\"hidden\"<\/span>\n                    <span class=\"hljs-attr\">animate<\/span>=<span class=\"hljs-string\">\"visible\"<\/span>\n                \/&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">motion.path<\/span>\n                    <span class=\"hljs-attr\">d<\/span>=<span class=\"hljs-string\">\"M224,144H156a8,8,0,0,1-8-8V72a8,8,0,0,1,8-8h60a8,8,0,0,1,8,8v88a40,40,0,0,1-40,40\"<\/span>\n                    <span class=\"hljs-attr\">fill<\/span>=<span class=\"hljs-string\">\"none\"<\/span>\n                    <span class=\"hljs-attr\">stroke<\/span>=<span class=\"hljs-string\">\"#000\"<\/span>\n                    <span class=\"hljs-attr\">stroke-linecap<\/span>=<span class=\"hljs-string\">\"round\"<\/span>\n                    <span class=\"hljs-attr\">stroke-linejoin<\/span>=<span class=\"hljs-string\">\"round\"<\/span>\n                    <span class=\"hljs-attr\">stroke-width<\/span>=<span class=\"hljs-string\">\"12\"<\/span>\n                    <span class=\"hljs-attr\">variants<\/span>=<span class=\"hljs-string\">{pathVariants}<\/span>\n                    <span class=\"hljs-attr\">initial<\/span>=<span class=\"hljs-string\">\"hidden\"<\/span>\n                    <span class=\"hljs-attr\">animate<\/span>=<span class=\"hljs-string\">\"visible\"<\/span>\n                \/&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">motion.svg<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/span>\n    );\n};\n\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> AnimateSVG;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-27\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Run the sandbox to preview the application in action:<\/p>\n\n\n<div\n\tclass=\"sandbox-embed responsive-embed  sandbox-embed--full-width\"\n\tstyle=\"padding-top: 125%\"\ndata-block-name=\"coderpad-sandbox-embed\">\n\t<iframe src=\"https:\/\/embed.coderpad.io\/sandbox?question_id=232870&#038;use_question_button\" width=\"640\" height=\"800\" loading=\"lazy\" aria-label=\"Try out the CoderPad sandbox\"><\/iframe>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">ta-da \ud83c\udf89!<\/h2>\n\n\n\n<p>In this guide, you have learned how to use Framer Motion to add animation to your React application elements, routes, and SVGs. It is important to note that the Framer Motion offers more than what has been covered in this guide, with many more animation options.<\/p>\n\n\n\n<p>This guide has laid a proper foundation to help you navigate through and use the library in your React application for the first time. You can now check out the <a href=\"https:\/\/www.framer.com\/docs\/\" target=\"_blank\" rel=\"noopener\">Framer Motion documentation<\/a> to understand more options and animate your React application better. You can also get the animated demo project from this article&#8217;s <a href=\"https:\/\/github.com\/olawanlejoel\/quotes-app-animation\" target=\"_blank\" rel=\"noopener\">GitHub repository<\/a>.<\/p>\n\n\n\n<p>Have fun coding!<\/p>\n\n\n\n<p><em>I\u2019m Joel Olawanle, a frontend developer and technical writer interested in making the web accessible to everyone by always looking for ways to give back to the tech community. Follow me and connect with me on <\/em><a href=\"https:\/\/twitter.com\/olawanle_joel\" target=\"_blank\" rel=\"noopener\"><em>Twitter<\/em><\/a><em>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Animations on web pages involve moving elements on the screen to improve the visual experience of users. In this article, you&#8217;ll learn how you can improve your React applications&#8217; visual experience with Framer Motion.<\/p>\n","protected":false},"author":1,"featured_media":21866,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9],"tags":[],"persona":[29],"blog-programming-language":[61],"keyword-cluster":[],"class_list":["post-21718","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"_links":{"self":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/21718","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/comments?post=21718"}],"version-history":[{"count":100,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/21718\/revisions"}],"predecessor-version":[{"id":32646,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/21718\/revisions\/32646"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media\/21866"}],"wp:attachment":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media?parent=21718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/categories?post=21718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/tags?post=21718"},{"taxonomy":"persona","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/persona?post=21718"},{"taxonomy":"blog-programming-language","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/blog-programming-language?post=21718"},{"taxonomy":"keyword-cluster","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/keyword-cluster?post=21718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}