Framer Motion provides declarative animations in React.
Basic animation:
1import { motion } from "framer-motion";23function AnimatedBox() {4 return (5 <motion.div6 animate={{ scale: 1, opacity: 1 }}7 initial={{ scale: 0.5, opacity: 0 }}8 transition={{ duration: 0.5 }}9 >10 Animated content11 </motion.div>12 );13}
Hover/tap animations:
1<motion.button2 whileHover={{ scale: 1.05 }}3 whileTap={{ scale: 0.95 }}4 animate={{ backgroundColor: "#fff" }}5>6 Click me7</motion.button>
List animations:
1import { AnimatePresence, motion } from "framer-motion";23function AnimatedList({ items }) {4 return (5 <AnimatePresence>6 {items.map(item => (7 <motion.div8 key={item.id}9 initial={{ opacity: 0, x: -50 }}10 animate={{ opacity: 1, x: 0 }}11 exit={{ opacity: 0, x: 50 }}12 transition={{ duration: 0.3 }}13 >14 {item.text}15 </motion.div>16 ))}17 </AnimatePresence>18 );19}
Page transitions:
1import { motion, AnimatePresence } from "framer-motion";2import { useLocation } from "react-router-dom";34function AnimatedRoutes() {5 const location = useLocation();67 return (8 <AnimatePresence mode="wait">9 <motion.div10 key={location.pathname}11 initial={{ opacity: 0 }}12 animate={{ opacity: 1 }}13 exit={{ opacity: 0 }}14 >15 <Routes location={location}>16 <Route path="/" element={<Home />} />17 <Route path="/about" element={<About />} />18 </Routes>19 </motion.div>20 </AnimatePresence>21 );22}
Benefits: