{"id":7611,"date":"2025-07-06T15:32:30","date_gmt":"2025-07-06T15:32:29","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=7611"},"modified":"2025-07-06T15:32:30","modified_gmt":"2025-07-06T15:32:29","slug":"creating-animated-ui-with-framer-motion-6","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/creating-animated-ui-with-framer-motion-6\/","title":{"rendered":"Creating Animated UI with Framer Motion"},"content":{"rendered":"<h1>Creating Animated UI with Framer Motion<\/h1>\n<p>Animation plays a crucial role in enhancing user experience by making interfaces feel more dynamic and responsive. For React developers, <strong>Framer Motion<\/strong> stands out as one of the most powerful libraries for creating captivating animations with ease. In this article, we will explore how to effectively use Framer Motion to create animated UI elements in your web applications.<\/p>\n<h2>What is Framer Motion?<\/h2>\n<p><strong>Framer Motion<\/strong> is an open-source animation library designed for React. It brings animations to life using a simple API that allows developers to create sophisticated animations without the complexities often associated with CSS animations or JavaScript animations. It excels in managing animations for different states, making it ideal for building interactive UIs.<\/p>\n<p>With Framer Motion, you can create animations that respond to user input, allowing for seamless transitions between different components, states, or views.<\/p>\n<h2>Getting Started with Framer Motion<\/h2>\n<p>To begin using Framer Motion, first, ensure that you have a React development environment set up. If you haven\u2019t done that yet, follow these steps to create a new React app:<\/p>\n<pre><code>npx create-react-app framer-motion-demo\ncd framer-motion-demo\nnpm start<\/code><\/pre>\n<p>Next, you\u2019ll need to install Framer Motion. You can do this using npm:<\/p>\n<pre><code>npm install framer-motion<\/code><\/pre>\n<h3>Basic Usage<\/h3>\n<p>Now that you have Framer Motion installed, let\u2019s create a simple animated component. Start by importing the necessary modules into your React component:<\/p>\n<pre><code>import { motion } from 'framer-motion';<\/code><\/pre>\n<p>Here\u2019s a simple example of a bouncing box:<\/p>\n<pre><code>const BouncingBox = () =&gt; {\n  return (\n    &lt;motion.div \n      style={{\n        width: '100px',\n        height: '100px',\n        backgroundColor: 'blue',\n      }} \n      animate={{ y: [0, -50, 0] }} \n      transition={{ duration: 0.5, repeat: Infinity }}\n    \/&gt;\n  );\n};<\/code><\/pre>\n<p>This code snippet creates a blue box that bounces indefinitely using Framer Motion\u2019s <strong>animate<\/strong> and <strong>transition<\/strong> properties. The <strong>y<\/strong> value in the <strong>animate<\/strong> property dictates its vertical movement.<\/p>\n<h2>Animating on Mount and Unmount<\/h2>\n<p>Framer Motion also allows you to specify animations when components mount or unmount. You can achieve this using the <strong>initial<\/strong> and <strong>exit<\/strong> props:<\/p>\n<pre><code>const BoxWithMountUnmount = () =&gt; {\n  const [isVisible, setIsVisible] = useState(true);\n  \n  return (\n    &lt;div&gt;\n      &lt;button onClick={() =&gt; setIsVisible(!isVisible)}&gt;Toggle Box&lt;\/button&gt;\n      {isVisible &amp;&amp; (\n        &lt;motion.div\n          initial={{ opacity: 0 }}\n          animate={{ opacity: 1 }}\n          exit={{ opacity: 0 }}\n          style={{\n            width: '100px',\n            height: '100px',\n            backgroundColor: 'red',\n          }}\n        \/&gt;\n      )}\n    &lt;\/div&gt;\n  );\n};<\/code><\/pre>\n<p>In this example, clicking the &#8220;Toggle Box&#8221; button will fade the red box in and out by animating its opacity on mount and unmount using the <strong>initial<\/strong> and <strong>exit<\/strong> props.<\/p>\n<h2>Creating Routes with Animation<\/h2>\n<p>Animating routes can greatly improve the perceived performance of a web app. You can achieve this by leveraging <strong>AnimatePresence<\/strong>, which allows you to animate components as they enter and leave the DOM:<\/p>\n<pre><code>import { AnimatePresence } from 'framer-motion';\n\nconst App = () =&gt; {\n  const [showFirstContainer, setShowFirstContainer] = useState(true);\n  \n  return (\n    &lt;div&gt;\n      &lt;button onClick={() =&gt; setShowFirstContainer(!showFirstContainer)}&gt;Toggle Container&lt;\/button&gt;\n      &lt;AnimatePresence&gt;\n        {showFirstContainer ? (\n          &lt;motion.div\n            key=\"first\"\n            initial={{ opacity: 0 }}\n            animate={{ opacity: 1 }}\n            exit={{ opacity: 0 }}\n          &gt;\n            &lt;h1&gt;First Container&lt;\/h1&gt;\n          &lt;\/motion.div&gt;\n        ) : (\n          &lt;motion.div\n            key=\"second\"\n            initial={{ scale: 0 }}\n            animate={{ scale: 1 }}\n            exit={{ scale: 0 }}\n          &gt;\n            &lt;h1&gt;Second Container&lt;\/h1&gt;\n          &lt;\/motion.div&gt;\n        )}\n      &lt;\/AnimatePresence&gt;\n    &lt;\/div&gt;\n  );\n};<\/code><\/pre>\n<p>In this code, clicking the button toggles between two containers, each with its own unique animation defined by the initial and exit states.<\/p>\n<h2>Transitions and Easing Functions<\/h2>\n<p>To enhance the sophistication of your animations, Framer Motion provides options for transitions and easing. You can customize the <strong>duration<\/strong>, <strong>ease<\/strong>, and various other properties:<\/p>\n<pre><code>const EasedBox = () =&gt; {\n  return (\n    &lt;motion.div\n      initial={{ x: '-100vw' }}\n      animate={{ x: 0 }}\n      exit={{ x: '100vw' }}\n      transition={{\n        duration: 1,\n        ease: [0.5, 0, 0.5, 1]\n      }}\n      style={{ width: '100px', height: '100px', backgroundColor: 'green' }}\n    \/&gt;\n  );\n};<\/code><\/pre>\n<p>In this case, the green box smoothly enters from the left and smoothly exits to the right using a cubic bezier easing function.<\/p>\n<h2>Complex Animations: Staggered Effects<\/h2>\n<p>Framer Motion makes it simple to create staggered animations for lists or collections. You can pass an array of animations for each item, creating a nice entrance or exit effect:<\/p>\n<pre><code>const StaggeredList = () =&gt; {\n  const items = [1, 2, 3, 4, 5];\n  return (\n    &lt;motion.ul initial=\"hidden\" animate=\"visible\"&gt;\n      {items.map((item, index) =&gt; (\n        &lt;motion.li\n          key={item}\n          variants={{\n            hidden: { opacity: 0, y: 20 },\n            visible: { opacity: 1, y: 0 },\n          }}\n          transition={{ delay: index * 0.2 }}\n        &gt;\n          Item {item}\n        &lt;\/motion.li&gt;\n      ))}\n    &lt;\/motion.ul&gt;\n  );\n};<\/code><\/pre>\n<p>This example demonstrates how to animate a list of items with a staggered effect that adds a touch of elegance to the component&#8217;s entrance.<\/p>\n<h2>Using Hooks and Custom Components<\/h2>\n<p>Framer Motion allows developers to create custom hooks for complex animations, enhancing reusability. Here&#8217;s an example of a custom hook that handles a toggled state with animations:<\/p>\n<pre><code>import { useEffect } from 'react';\n\nconst useAnimationToggle = () =&gt; {\n  const [isVisible, setIsVisible] = useState(false);\n\n  const toggle = () =&gt; setIsVisible(!isVisible);\n\n  useEffect(() =&gt; {\n    \/\/ Animation logic can be included here\n  }, [isVisible]);\n\n  return { isVisible, toggle };\n};\n\nconst AnimatedToggleBox = () =&gt; {\n  const { isVisible, toggle } = useAnimationToggle();\n\n  return (\n    &lt;div&gt;\n      &lt;button onClick={toggle}&gt;Toggle Box&lt;\/button&gt;\n      {isVisible &amp;&amp; (\n        &lt;motion.div\n          initial={{ opacity: 0 }}\n          animate={{ opacity: 1 }}\n          exit={{ opacity: 0 }}\n          style={{ width: '100px', height: '100px', backgroundColor: 'orange' }}\n        \/&gt;\n      )}\n    &lt;\/div&gt;\n  );\n};<\/code><\/pre>\n<p>This custom hook abstracts the visibility toggling logic and can be reused for different animated components, promoting code reusability and separation of concerns.<\/p>\n<h2>Conclusion<\/h2>\n<p>Framer Motion is an incredibly powerful tool for React developers, enabling them to add animations quickly and efficiently. With its intuitive API, managing animations becomes a relatively straightforward task, allowing developers to focus more on building dynamic user interfaces. Through the examples provided, you can see how Framer Motion simplifies tasks such as animating component transitions, staggering lists, and creating sophisticated, interactive UIs.<\/p>\n<p>To further your knowledge and skills, experiment with Framer Motion\u2019s advanced features and combine it with other React libraries to create even richer experiences. As you incorporate animations into your projects, remember that good animation practice enhances usability and should be used thoughtfully to create an enjoyable user experience.<\/p>\n<h2>Further Reading<\/h2>\n<p>For more detailed documentation and advanced techniques, visit the official <a href=\"https:\/\/www.framer.com\/docs\/motion\/\" target=\"_blank\">Framer Motion documentation<\/a>. Happy animating!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating Animated UI with Framer Motion Animation plays a crucial role in enhancing user experience by making interfaces feel more dynamic and responsive. For React developers, Framer Motion stands out as one of the most powerful libraries for creating captivating animations with ease. In this article, we will explore how to effectively use Framer Motion<\/p>\n","protected":false},"author":92,"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-7611","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\/7611","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\/92"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=7611"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7611\/revisions"}],"predecessor-version":[{"id":7612,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7611\/revisions\/7612"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=7611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=7611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=7611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}