DEV Community

Cover image for Animating React Components with Framer Motion: A Comprehensive Guide
Deepak Kumar
Deepak Kumar

Posted on • Originally published at raajaryan.tech

Animating React Components with Framer Motion: A Comprehensive Guide

BuyMeACoffee

Animations can significantly enhance the user experience by providing visual feedback and making interactions more engaging. In the realm of React, Framer Motion has emerged as a powerful and flexible library for creating animations. This guide will delve into the details of using Framer Motion in your React projects, covering everything from basic animations to complex interactions, and best practices for performance.

Introduction to Framer Motion

Framer Motion is an open-source animation library for React. Developed by Framer, it is designed to provide a seamless way to create animations and interactions. Unlike CSS animations, Framer Motion offers more control and flexibility, making it easier to create complex animations with minimal effort.

Key Features

  • Ease of Use: Framer Motion's API is designed to be intuitive, allowing developers to quickly add animations to their projects.

  • Performance: It leverages the power of the browser's requestAnimationFrame for smooth animations.

  • Declarative Syntax: Animations are defined in a declarative manner, making the code easier to read and maintain.

  • Integration with React: It fits naturally into the React ecosystem, allowing you to animate your components effortlessly.

Setting Up Framer Motion

To get started with Framer Motion, you'll need to install it in your React project. If you haven't already created a React project, you can do so using Create React App:

npx create-react-app framer-motion-demo
cd framer-motion-demo
Enter fullscreen mode Exit fullscreen mode

Next, install Framer Motion:

npm install framer-motion
Enter fullscreen mode Exit fullscreen mode

Or if you're using Yarn:

yarn add framer-motion
Enter fullscreen mode Exit fullscreen mode

With Framer Motion installed, you're ready to start creating animations.

Basic Animations

Initial and Animate

The motion component is the core of Framer Motion. It can be used to animate any HTML or SVG element. To create a simple animation, you can use the initial and animate props:

import React from 'react';
import { motion } from 'framer-motion';

const BasicAnimation = () => {
  return (
    <motion.div
      initial={{ opacity: 0 }}
      animate={{ opacity: 1 }}
      transition={{ duration: 1 }}
    >
      Hello, Framer Motion!
    </motion.div>
  );
};

export default BasicAnimation;
Enter fullscreen mode Exit fullscreen mode

In this example, the motion.div component fades in from an opacity of 0 to 1 over one second.

Hover and Tap Animations

Framer Motion makes it easy to add animations for hover and tap interactions:

import React from 'react';
import { motion } from 'framer-motion';

const HoverAndTap = () => {
  return (
    <motion.div
      whileHover={{ scale: 1.2 }}
      whileTap={{ scale: 0.8 }}
      style={{ width: 100, height: 100, backgroundColor: 'blue' }}
    >
      Hover and Tap Me!
    </motion.div>
  );
};

export default HoverAndTap;
Enter fullscreen mode Exit fullscreen mode

Here, the motion.div component scales up when hovered over and scales down when tapped.

Advanced Animations

Variants

Variants allow you to define multiple animation states and switch between them easily. This is useful for creating more complex animations.

import React from 'react';
import { motion } from 'framer-motion';

const boxVariants = {
  hidden: { opacity: 0, scale: 0.8 },
  visible: { opacity: 1, scale: 1, transition: { duration: 0.5 } },
};

const VariantsExample = () => {
  return (
    <motion.div
      variants={boxVariants}
      initial="hidden"
      animate="visible"
      style={{ width: 100, height: 100, backgroundColor: 'red' }}
    >
      Variants Animation
    </motion.div>
  );
};

export default VariantsExample;
Enter fullscreen mode Exit fullscreen mode

In this example, the motion.div component has two states: hidden and visible. The component transitions between these states based on the initial and animate props.

Keyframes

Keyframes allow you to define multiple steps within a single animation, giving you more control over the animation sequence.

import React from 'react';
import { motion } from 'framer-motion';

const KeyframesExample = () => {
  return (
    <motion.div
      animate={{ x: [0, 100, 50, 100, 0], opacity: [1, 0.5, 1] }}
      transition={{ duration: 2, ease: 'easeInOut' }}
      style={{ width: 100, height: 100, backgroundColor: 'green' }}
    >
      Keyframes Animation
    </motion.div>
  );
};

export default KeyframesExample;
Enter fullscreen mode Exit fullscreen mode

In this example, the motion.div component moves along the x-axis and changes opacity according to the specified keyframes.

Animating Lists

Animating lists can be challenging, but Framer Motion provides an elegant solution with the AnimatePresence component. This component helps animate the entry and exit of list items.

import React, { useState } from 'react';
import { motion, AnimatePresence } from 'framer-motion';

const ListAnimation = () => {
  const [items, setItems] = useState([1, 2, 3]);

  const addItem = () => {
    setItems([...items, items.length + 1]);
  };

  const removeItem = (index) => {
    setItems(items.filter((item, i) => i !== index));
  };

  return (
    <>
      <button onClick={addItem}>Add Item</button>
      <ul>
        <AnimatePresence>
          {items.map((item, index) => (
            <motion.li
              key={item}
              initial={{ opacity: 0, x: -100 }}
              animate={{ opacity: 1, x: 0 }}
              exit={{ opacity: 0, x: 100 }}
              transition={{ duration: 0.5 }}
              onClick={() => removeItem(index)}
            >
              Item {item}
            </motion.li>
          ))}
        </AnimatePresence>
      </ul>
    </>
  );
};

export default ListAnimation;
Enter fullscreen mode Exit fullscreen mode

In this example, list items are animated when they are added or removed from the list.

Gestures and Interactions

Framer Motion provides support for various gestures, such as drag and scroll animations. These interactions can greatly enhance the user experience.

Drag

import React from 'react';
import { motion } from 'framer-motion';

const DragExample = () => {
  return (
    <motion.div
      drag
      dragConstraints={{ left: -100, right: 100, top: -100, bottom: 100 }}
      style={{ width: 100, height: 100, backgroundColor: 'purple' }}
    >
      Drag Me!
    </motion.div>
  );
};

export default DragExample;
Enter fullscreen mode Exit fullscreen mode

In this example, the motion.div component can be dragged within the specified constraints.

Scroll Animations

import React from 'react';
import { motion, useAnimation } from 'framer-motion';
import { useInView } from 'react-intersection-observer';

const ScrollAnimation = () => {
  const controls = useAnimation();
  const [ref, inView] = useInView({ threshold: 0.1 });

  React.useEffect(() => {
    if (inView) {
      controls.start({ opacity: 1, scale: 1 });
    } else {
      controls.start({ opacity: 0, scale: 0.8 });
    }
  }, [controls, inView]);

  return (
    <motion.div
      ref={ref}
      initial={{ opacity: 0, scale: 0.8 }}
      animate={controls}
      transition={{ duration: 0.5 }}
      style={{ width: 100, height: 100, backgroundColor: 'orange' }}
    >
      Scroll to Animate
    </motion.div>
  );
};

export default ScrollAnimation;
Enter fullscreen mode Exit fullscreen mode

In this example, the motion.div component animates based on its visibility in the viewport, using the useInView hook from react-intersection-observer.

Keyframe Animations

Keyframe animations provide more granular control over the animation sequence, allowing you to define multiple steps within a single animation.

import React from 'react';
import { motion } from 'framer-motion';

const KeyframeExample = () => {
  return (
    <motion.div
      animate={{ x: [0, 100, 50, 100, 0], opacity: [1, 0.5, 1] }}
      transition={{ duration: 2, ease: 'easeInOut' }}
      style={{ width: 100, height: 100, backgroundColor: 'cyan' }}
    >
      Keyframe Animation
    </motion

.div>
  );
};

export default KeyframeExample;
Enter fullscreen mode Exit fullscreen mode

This example demonstrates how to animate the x position and opacity of a component using keyframes.

SVG Animations

Framer Motion can also be used to animate SVG elements, making it a powerful tool for creating interactive graphics.

import React from 'react';
import { motion } from 'framer-motion';

const SVGAnimation = () => {
  return (
    <motion.svg
      width="200"
      height="200"
      viewBox="0 0 200 200"
      initial={{ pathLength: 0 }}
      animate={{ pathLength: 1 }}
      transition={{ duration: 2, ease: 'easeInOut' }}
    >
      <motion.path
        d="M10 80 Q 95 10 180 80 T 280 80"
        stroke="black"
        strokeWidth="10"
        fill="none"
      />
    </motion.svg>
  );
};

export default SVGAnimation;
Enter fullscreen mode Exit fullscreen mode

In this example, an SVG path is animated to draw from start to finish.

Best Practices for Performance

When working with animations, performance is crucial to ensure a smooth and responsive user experience. Here are some best practices to keep in mind when using Framer Motion:

  1. Minimize Re-renders: Ensure that animations do not cause unnecessary re-renders of your components. Use memoization techniques to optimize performance.

  2. Use thewill-change CSS Property: This property can hint to the browser that an element will change, allowing the browser to optimize rendering.

  3. Limit the Number of Animated Elements: Animating a large number of elements simultaneously can be resource-intensive. Try to limit the number of elements being animated at any given time.

  4. Optimize Animation Timing: Use appropriate easing functions and durations to create smooth animations without causing performance issues.

  5. Usemotion Components Sparingly: While motion components are powerful, overusing them can lead to performance bottlenecks. Use them judiciously for key animations.

Conclusion

Framer Motion is a powerful and flexible library for creating animations in React. Its intuitive API and seamless integration with React make it an excellent choice for developers looking to enhance their user interfaces with animations. By understanding the basics and exploring advanced features, you can create engaging and performant animations that elevate the user experience. Whether you're animating simple elements, complex interactions, or even SVG graphics, Framer Motion provides the tools you need to bring your React applications to life.

πŸ’° You can help me by Donating

BuyMeACoffee

Top comments (2)

Collapse
 
anjapcodes profile image
Anja Petry

Nice introduction to Framer Motion!

A little correction though: in the SVG animations example
initial, animate, and transition need to be set in the to actually work (and not as shown in the ).

Thanks for sharing, Deepak

Collapse
 
best_codes profile image
Best Codes

Thanks for writing! I just started using this today, and this is quite helpful. Keep up the good work!