{"id":8025,"date":"2025-07-19T13:32:23","date_gmt":"2025-07-19T13:32:23","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=8025"},"modified":"2025-07-19T13:32:23","modified_gmt":"2025-07-19T13:32:23","slug":"creating-animated-ui-with-framer-motion-8","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/creating-animated-ui-with-framer-motion-8\/","title":{"rendered":"Creating Animated UI with Framer Motion"},"content":{"rendered":"<h1>Creating Animated UI with Framer Motion<\/h1>\n<p>The demand for rich and interactive user interfaces (UIs) is at an all-time high. Developers are looking for tools that enable them to create smooth, captivating animations while maintaining optimal performance. One standout library in this space is <strong>Framer Motion<\/strong>, a powerful animation library for React applications.<\/p>\n<h2>What is Framer Motion?<\/h2>\n<p>Framer Motion is an open-source library that provides a simple and declarative API for building animations in React. It enables developers to create engaging UIs without requiring an extensive understanding of CSS animations or complex JavaScript logic. With its powerful features, Framer Motion streamlines the animation process and enhances user experience.<\/p>\n<h2>Installing Framer Motion<\/h2>\n<p>Before we dive into creating animations, let\u2019s see how to install Framer Motion in your React project. You can easily install it via npm or yarn. Here\u2019s how:<\/p>\n<pre><code>npm install framer-motion\n<\/code><\/pre>\n<pre><code>yarn add framer-motion\n<\/code><\/pre>\n<h2>Basic Animation Concepts<\/h2>\n<p>Framer Motion revolves around motion components, which can be used to create animated elements within your React application. The primary components we will use are:<\/p>\n<ul>\n<li><strong>motion.div<\/strong>: A div that can be animated.<\/li>\n<li><strong>AnimatePresence<\/strong>: Used to manage exit animations.<\/li>\n<\/ul>\n<h2>Creating Your First Animated Component<\/h2>\n<p>Let\u2019s start by creating a simple animated button using Framer Motion. Here\u2019s how you can do this:<\/p>\n<pre><code>import { motion } from \"framer-motion\";\n\nfunction AnimatedButton() {\n    return (\n        &lt;motion.button \n            whileHover={{ scale: 1.1 }} \n            whileTap={{ scale: 0.9 }}\n            style={{ padding: \"10px 20px\", fontSize: \"16px\" }}\n        &gt;\n            Click Me!\n        &lt;\/motion.button&gt;\n    );\n}\n<\/code><\/pre>\n<p>In the example above, we use <strong>whileHover<\/strong> and <strong>whileTap<\/strong> properties to scale the button when the user hovers over it or taps it. This simple effect adds an interactive layer to the UI.<\/p>\n<h2>Animating With Variants<\/h2>\n<p>Variants are a powerful feature in Framer Motion that allow you to define multiple states for a component. This is particularly useful for creating complex animations that require transitioning between different visual states. Let\u2019s explore how to implement variants:<\/p>\n<pre><code>import { motion } from \"framer-motion\";\n\nconst buttonVariants = {\n    initial: { opacity: 0, scale: 0.8 },\n    animate: { opacity: 1, scale: 1 },\n    exit: { opacity: 0, scale: 0.8 }\n};\n\nfunction AnimatedDiv() {\n    return (\n        &lt;motion.div\n            initial=\"initial\"\n            animate=\"animate\"\n            exit=\"exit\"\n            variants={buttonVariants}\n            style={{ width: \"100px\", height: \"100px\", backgroundColor: \"teal\" }}\n        &gt;\n        &lt;\/motion.div&gt;\n    );\n}\n<\/code><\/pre>\n<p>Here, we define an object called <strong>buttonVariants<\/strong>, which contains three states: <strong>initial<\/strong>, <strong>animate<\/strong>, and <strong>exit<\/strong>. This allows our component to transition smoothly between these states, creating a polished animation effect.<\/p>\n<h2>Combining Animations With Gesture Control<\/h2>\n<p>Gestures such as drag, hover, and tap can enhance user interactivity. Framer Motion makes it easy to incorporate these gestures into your animations. Let\u2019s see how to create a draggable component:<\/p>\n<pre><code>import { motion } from \"framer-motion\";\n\nfunction DraggableBox() {\n    return (\n        &lt;motion.div\n            drag\n            dragConstraints={{ left: -100, top: -100, right: 100, bottom: 100 }}\n            style={{ width: \"100px\", height: \"100px\", backgroundColor: \"coral\" }}\n        &gt;\n        &lt;\/motion.div&gt;\n    );\n}\n<\/code><\/pre>\n<p>In the <strong>DraggableBox<\/strong> component, we use the <strong>drag<\/strong> property to make the box draggable within specified constraints. Users can actively engage with this component, making your UI feel more dynamic.<\/p>\n<h2>Animating Lists with AnimatePresence<\/h2>\n<p>Animating lists can be challenging, especially when items enter or exit the view. Framer Motion\u2019s <strong>AnimatePresence<\/strong> component allows you to manage exit animations seamlessly. Here\u2019s a simple example:<\/p>\n<pre><code>import { motion, AnimatePresence } from \"framer-motion\";\nimport { useState } from \"react\";\n\nfunction AnimatedList() {\n    const [items, setItems] = useState([\"Item 1\", \"Item 2\", \"Item 3\"]);\n\n    const removeItem = (index) =&gt; {\n        const newItems = items.filter((_, i) =&gt; i !== index);\n        setItems(newItems);\n    };\n\n    return (\n        &lt;div&gt;\n            &lt;AnimatePresence&gt;\n                {items.map((item, index) =&gt; (\n                    &lt;motion.div\n                        key={item}\n                        initial={{ opacity: 0 }}\n                        animate={{ opacity: 1 }}\n                        exit={{ opacity: 0 }}\n                        transition={{ duration: 0.5 }}\n                    &gt;\n                        &lt;div onClick={() =&gt; removeItem(index)}&gt;{item}&lt;\/div&gt;\n                    &lt;\/motion.div&gt;\n                ))}\n            &lt;\/AnimatePresence&gt;\n        &lt;\/div&gt;\n    );\n}\n<\/code><\/pre>\n<p>In this example, we create a simple list of items. When an item is clicked, it is removed from the list with a fade-out animation, thanks to <strong>AnimatePresence<\/strong>.<\/p>\n<h2>Advanced Animations with Orchestration<\/h2>\n<p>Sometimes, you may want to orchestrate multiple animations to produce more complex effects. You can achieve this by using lifecycle methods and callbacks. Here&#8217;s an example:<\/p>\n<pre><code>import { motion } from \"framer-motion\";\n\nfunction OrchestratedAnimation() {\n    const sequenceVariants = {\n        initial: { opacity: 0 },\n        animate: {\n            opacity: 1,\n            transition: {\n                staggerChildren: 0.2 \/\/ Stagger the children animation\n            }\n        }\n    };\n\n    const childVariants = {\n        initial: { y: 20, opacity: 0 },\n        animate: { y: 0, opacity: 1 }\n    };\n\n    return (\n        &lt;motion.div variants={sequenceVariants} initial=\"initial\" animate=\"animate\"&gt;\n            &lt;motion.div variants={childVariants}&gt;Child 1&lt;\/motion.div&gt;\n            &lt;motion.div variants={childVariants}&gt;Child 2&lt;\/motion.div&gt;\n            &lt;motion.div variants={childVariants}&gt;Child 3&lt;\/motion.div&gt;\n        &lt;\/motion.div&gt;\n    );\n}\n<\/code><\/pre>\n<p>In this example, we defined a parent and child animation sequence. The children will enter the view with a staggered effect, creating an engaging visual narrative as users interact with your app.<\/p>\n<h2>Best Practices for Performance<\/h2>\n<p>While animations can greatly enhance user experience, they can also lead to performance issues if not properly managed. Here are some best practices:<\/p>\n<ul>\n<li><strong>Keep animations simple<\/strong>: Avoid overly complex animations that might slow down your application.<\/li>\n<li><strong>Use hardware acceleration<\/strong>: Prefer properties like transform and opacity to leverage GPU acceleration.<\/li>\n<li><strong>Minimize layout thrashing<\/strong>: Batch DOM updates and use animation libraries to avoid excessive reflows.<\/li>\n<li><strong>Limit animation duration<\/strong>: Keep animations short to maintain user engagement without becoming distracting.<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>Framer Motion is a versatile and powerful library that enables React developers to create engaging and interactive UIs with minimal effort. By leveraging its intuitive API, you&#8217;ve learned the foundations of animating components, using variants, handling gestures, orchestrating complex animations, and maintaining performance.<\/p>\n<p>Whether you\u2019re setting out to build a simple interactive button or a complex animated list, Framer Motion opens up a world of possibilities for enhancing your applications&#8217; user interfaces. Start experimenting with Framer Motion in your projects, and elevate your UIs to the next level!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating Animated UI with Framer Motion The demand for rich and interactive user interfaces (UIs) is at an all-time high. Developers are looking for tools that enable them to create smooth, captivating animations while maintaining optimal performance. One standout library in this space is Framer Motion, a powerful animation library for React applications. What is<\/p>\n","protected":false},"author":95,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[285],"tags":[397],"class_list":["post-8025","post","type-post","status-publish","format-standard","category-system-design","tag-system-design"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/8025","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/95"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=8025"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/8025\/revisions"}],"predecessor-version":[{"id":8026,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/8025\/revisions\/8026"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=8025"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=8025"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=8025"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}