DEV Community

HasOne
HasOne

Posted on • Edited on

3 1

React Animation with Framer Motion

Getting Started

It's time to advance you React project, make more interesting add some animation. if you've ever tried in react to add transition you may feel bad, so let's introduce you a simple Library which gets the job done without any pain, so let's install the package first:

npm install framer-motion

# Yarn
yarn add framer-motion
Enter fullscreen mode Exit fullscreen mode

It's good idea to create new react project then install the framer motion in it.

First import the package:

import { motion } from "framer-motion";
Enter fullscreen mode Exit fullscreen mode

the framer-motion is all about <motion> component, and the second <motion.div> is html elment what you want to animate, cause without element nothing happen in even css, the animation is being controlled by <motion.h1> whenever you want to animate you UI you should invoke motion component

return (
   <motion.div>
      Hello, Framer-Motion
   </motion.div>
)
Enter fullscreen mode Exit fullscreen mode

What happened nothing, wait there is another thing to introduce animate props, there we define our animation logic, like opacity, color and so on... accepts all the CSS properties.
let's give it short:

return (
   <motion.div animate={{ opacity: 0, x: 300}}>
      Hello, Framer-Motion
   </motion.div>
)
Enter fullscreen mode Exit fullscreen mode

Alt Text

isn't cool?! but it too fast let's make it a little bit slow, if you remember I said we can use CSS properties in Framer-Motion it's time to use the Transition: duration:

<motion.div 
      animate={{ opacity: 0, x: 300 }} 
      transition={{ duration: 3 }}>
      Hello, Framer-Motion
</motion.div>
Enter fullscreen mode Exit fullscreen mode

Alt Text

SandBox

I hope tomorrow I'll post another post, that's it for now, thanks for your valuable time!

Resources:
https://www.framer.com/api/motion/animation/
Video: https://www.youtube.com/watch?v=2V1WK-3HQNk&list=PL4cUxeGkcC9iHDnQfTHEVVceOEBsOf07i

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay