Enhancing User Experience with Framer Motion: Basic Animations

Learn the fundamentals of Framer Motion to add smooth and engaging animations to your React applications, improving user experience.

Sushil Magare /
Enhancing User Experience with Framer Motion: Basic Animations

In today's web, static interfaces often feel dull. Animations play a crucial role in creating engaging and intuitive user experiences. If you're working with React, Framer Motion is an incredibly powerful and easy-to-use library for bringing your UIs to life. It abstracts away much of the complexity of web animations, allowing you to focus on the creative aspects.

Let's explore some basic animations you can implement with Framer Motion.

Installation

First, you'll need to install Framer Motion:

1npm install framer-motion
2# or
3yarn add framer-motion

Your First Animation: Fade In

The core of Framer Motion is the motion component. You can turn any HTML or SVG element into a motion component by prefixing it with motion..

1
2import { motion } from "framer-motion";
3
4function FadeInDiv() {
5return (
6
7<motion.div
8initial={{ opacity: 0 }}
9animate={{ opacity: 1 }}
10transition={{ duration: 1 }}
11className="rounded-lg bg-blue-500 p-8 text-center text-white shadow-lg"
12>
13Hello Framer Motion!
14</motion.div>
15); }

Here's what's happening:

  • initial: Defines the state of the component before the animation starts (invisible, opacity: 0).
  • animate: Defines the state the component animates to (opacity: 1).
  • transition: Specifies the animation properties, like duration (how long the animation takes in seconds).

Animating on Hover

Interactivity is key. Framer Motion makes it simple to add hover effects.

1
2import { motion } from "framer-motion";
3
4function HoverScaleButton() {
5return (
6
7<motion.button
8whileHover={{ scale: 1.1 }}
9whileTap={{ scale: 0.9 }}
10className="rounded-md bg-emerald-600 px-6 py-3 text-white shadow-md hover:bg-emerald-700"
11>
12Hover Me!
13</motion.button>
14); } 
  • whileHover: Defines the animation that occurs when the mouse pointer is over the element.
  • whileTap: Defines the animation that occurs when the element is being clicked/tapped.

Using Variants for Cleaner Animations

For more complex animations or when animating multiple elements, variants provide a structured way to manage animation states.


import { motion } from "framer-motion";

const containerVariants = {
hidden: { opacity: 0 },
visible: {
opacity: 1,
transition: {
staggerChildren: 0.2
}
}
};

const itemVariants = {
hidden: { y: 20, opacity: 0 },
visible: { y: 0, opacity: 1 }
};

function ListAnimation() {
return (

<motion.ul
variants={containerVariants}
initial="hidden"
animate="visible"
className="list-disc pl-5 text-neutral-700 dark:text-neutral-300"
>
<motion.li variants={itemVariants}>Item 1</motion.li>
<motion.li variants={itemVariants}>Item 2</motion.li>
<motion.li variants={itemVariants}>Item 3</motion.li>
</motion.ul>
); }

export default ListAnimation;
    

Conclusion

Framer Motion is an invaluable tool for any React developer looking to add beautiful and performant animations to their applications. Even with these basic concepts, you can significantly enhance the user experience. Dive deeper into its documentation to explore more advanced features like gestures, drag, and scroll animations!