Make cool animations in Next.js !

Make cool animations in Next.js !

Introduction

Next.js is a popular framework for building server-rendered React applications. It offers many features out of the box, including built-in support for automatic code splitting, optimized performance, and easy deployment to production. In this article, we’ll explore how to use the former-motion library to add smooth animations to our Next.js applications. What is framer-motion ?

framer-motion is a library for animating components in React applications. It is built on top of the popular animation library react-spring, and offers an easy-to-use API for defining and controlling complex animations. framer-motion also includes a set of pre-defined animations and transitions that can be used out of the box, as well as the ability to create custom animations and effects.

One of the key features of framer-motion is its support for animating the layout of components, allowing developers to create smooth, fluid animations that respond to user interactions and changes in the application state. This makes it a powerful tool for creating engaging and user-friendly experiences in React applications.

framer-motion is developed and maintained by the team at Framer, a popular design and prototyping tool for web and mobile applications. It is open-source and freely available on GitHub. For more information and documentation, check out the library’s website at framer.com/api/motion.

Getting started

here is a simple example of an advanced animation using the framer-motion library:

import React, { useState } from 'react';
import { motion, AnimatePresence } from 'framer-motion';

function MyComponent() {
  const [isVisible, setIsVisible] = useState(true);
  return (
    <AnimatePresence>
      {isVisible && (
        <motion.div
          initial={{ scale: 0 }}
          animate={{ scale: 1 }}
          exit={{ scale: 0 }}
          transition={{ duration: 1 }}
        >
          This is my advanced animation!
        </motion.div>
      )}
    </AnimatePresence>
  );
}

In this example, we’ve created a element that animates its scale from 0 to 1 when it is added to the DOM, and animates back to 0 when it is removed. We've also used the component to control the animation's visibility and ensure that the exit animation is performed when the element is removed from the DOM.

This is just a simple example, but the react-framer-motion library allows for many more advanced animations and effects. For more information and examples, check out the library’s documentation.

More Advanced Example

import { motion } from "framer-motion";

const container = {
  hidden: { opacity: 0 },
  show: {
    opacity: 1,
    transition: {
      when: "beforeChildren",
      staggerChildren: 0.1,
    },
  },
};

const item = {
  hidden: { y: 20, opacity: 0 },
  show: { y: 0, opacity: 1 },
};

const MyComponent = () => (
  <motion.ul variants={container} initial="hidden" animate="show">
    <motion.li variants={item}>Item 1</motion.li>
    <motion.li variants={item}>Item 2</motion.li>
    <motion.li variants={item}>Item 3</motion.li>
    <motion.li variants={item}>Item 4</motion.li>
  </motion.ul>
);

In this example, the MyComponent renders a

    list that animates in when the component is mounted. The animation is defined using the container and item variables, which specify the starting and ending styles for the animation, as well as the transition to use. The when property of the transition object is set to "beforeChildren", which means that the animation will start before the list items are animated in. The staggerChildren property is set to 0.1, which means that the animation of each list item will be staggered by 0.1 seconds.

    Conclusion

    In conclusion, the react framer-motion library is a powerful tool for adding smooth, engaging animations to Next.js applications. Its intuitive API and support for animating layout make it easy to create complex, responsive animations that enhance the user experience. Whether you’re building a simple landing page or a complex web application, framer-motion can help you bring your designs to life and make your Next.js application stand out. Give it a try and see how it can improve your Next.js projects