is a library that powers animations in , and it’s now available as an independent package that we can use in React applications. It has a very simple declarative API that makes it easy to create and orchestrate complex animations with the minimal amount of code. In this article, we’ll start with very basic animations and gradually move to the more advanced ones. Framer-motion Framer Note: animation examples in the article may not look smooth because of a low frame rate of GIF images. Rest assured, real animation are butter-smooth . You can play with them in the sandbox here . Setup We can start with framer-motion by simply installing it with command. yarn add framer-motion To animate elements, we’ll need to ditch primitive HTML elements ( , , , etc.) in favor of their “motion-infused” counterparts - , , , etc. These elements expose the properties that we’ll need to add our animations. div span path motion.div motion.span motion.path Get things moving To create the simplest animation, we can specify property that accepts an object with CSS properties that we want to animate. This is how we can animate opacity and background color of the : animate div { motion } ; One = ( <motion.div className= animate={{ opacity: , background: }} /> ); import from "framer-motion" const => () "rectangle" 0.5 "#ff00b1" The properties that we pass to represent the of the animation. Framer-motion will infer the initial state based on the specified CSS properties, or their defaults. For example, default opacity for CSS elements is (even if we don’t set it explicitly), so framer-motion knows how to animate it down to . animate final state 1 0.5 We can also set the initial values of animatable CSS properties using prop. It also accepts an object with CSS properties that will tell framer-motion what initial values should be like. In the example below, we fade in the rectangle by animating and properties: initial y opacity Two = ( <motion.div className= initial={{ opacity: , y: }} animate={{ opacity: , y: }} /> ); const => () "rectangle" 0 50 1 0 It’s worth mentioning that property is special - it’s not a real CSS property, but framer-motion understands it. There are a bunch of CSS -related properties that have shortcuts in framer-motion, so when we change property, we actually apply animation to property. Similarly, there are , , , and some other properties, you can find the complete list . y transform y transform: translateY() scale rotate scaleX scaleY here Animating state changes The animations that we’ve done so far only run when components mount. Now let’s see how we can animate elements when some internal state changes. We can set property to different values based on the internal state, and framer-motion will animate between those values when the state changes: animation Three = { [active, setActive] = React.useState( ); ( <motion.div className= animate={ active ? { background: , rotate: } : { background: , rotate: } } onClick={ setActive(!active)} > Click me! < const => () const false return "rectangle" "#ff00b1" 90 "#0D00FF" 0 => () /motion.div> ); }; Note that the component re-renders only when state changes, and not on every animation frame, which makes animations very efficient. Variants The real power of framer-motion comes from using . Let’s start by exploring how we can rewrite the previous example to use variants. variants We’ll begin by extracting inline definition of animatable properties from prop into a separate object. This object will contain key-value pairs, where keys are some meaningful names that we give to our animatable properties, and values are the properties themselves. Then we can pass this object to variants prop, and inside we can toggle animations based on the string names we gave to them: animate variants animation Four = { [active, setActive] = React.useState( ); rectangle: Variants = { active: { background: , rotate: }, disabled: { background: , rotate: } }; ( <motion.div className= variants={rectangle} animate={active ? : } onClick={ setActive(!active)} > Click me! < const => () const false const "#ff00b1" 90 "#0D00FF" 0 return "rectangle" "active" "disabled" => () /motion.div> ); }; This example works, but it’s not very useful. The power of variants is in orchestrating complex animations throughout a component tree, and to see that, we’ll need a slightly bigger example. In the example below, we have a container that has three child s inside of it. Container uses the same animation that we’ve seen before: div div div onClick Five = { container: Variants = { active: { background: }, disabled: { background: } }; [active, setActive] = React.useState( ); ( <motion.div variants={container} animate={active ? : } onClick={ setActive(!active)} className= > {[ , , ].map( ( <div key={value} className= /> ))} < const => () const "#ff00b1" "#0D00FF" const false return "active" "disabled" => () "container" 0 1 2 => value "box" /motion.div> ); }; Now we can animate children elements simultaneously with the parent by setting their own variants object. If the descriptive names of child animations match those of the parent, child animations will be triggered when parent animation is triggered. Notice how both and variants have the same keys and : container box active disabled Six = () => { container: Variants = { active: { : , }, disabled: { : } }; : Variants = { active: { : , opacity: }, disabled: { : , opacity: } }; [active, setActive] = React.useState( ); ( <motion.div variants={container} animate={active ? : } onClick={() => setActive(!active)} className= > {[ , , ]. (value => ( <motion.div ={value} className= variants={ } /> ))} </motion.div> ); }; const const background "#ff00b1" background "#0D00FF" const box rotate 90 1 rotate 0 0.7 const false return "active" "disabled" "container" 0 1 2 map key "box" box Configuring variants Variants also allow us to orchestrate the child animations. We can do that by providing property inside the animation object. transition For example, we can set children property, which specifies the delay in seconds between child animations: staggerChildren const container: Variants = { { , { } }, { } }; active: background: "#ff00b1" transition: staggerChildren: 0.5 disabled: background: "#0D00FF" Note how transition is applied only when we transition . Since we defined property inside variant, the stagger animation is only applied when we transition from , but not when we transition from to . into a given variant transition active disabled into active active disabled By default, variants start animating parent element and its children at the same time. We can control that behavior using property. We can set it to to make parent element animate first, or to , to make parent element animate after its children: when beforeChildren afterChildren const container: Variants = { { , { , } }, { } }; active: background: "#ff00b1" transition: staggerChildren: 0.5 when: "beforeChildren" disabled: background: "#0D00FF" With this configuration, the parent changes background color first, and then child elements rotate with a staggered delay. div There are a lot more properties of variants that we can control - animation delays, stagger direction, etc. You can find more information on them in framer-motion . documentation Wrapping up In this article, we’ve seen how easy it is to animate React components using declarative API that framer-motion provides. However, we just scratched the surface, since there’s a lot more that framer-motion is capable of - gestures, dragging, working with SVG paths and much more. If you’re interested in learning more - that will cover all the cool things that framer-motion has to offer. subscribe for my upcoming course