Logo

dev-resources.site

for different kinds of informations.

Creating a Smooth Animated Menu with React and Framer Motion

Published at
9/16/2024
Categories
react
framermotion
animation
Author
netanelben
Categories
3 categories in total
react
open
framermotion
open
animation
open
Author
10 person written this
netanelben
open
Creating a Smooth Animated Menu with React and Framer Motion

Creating a Smooth Animated Menu with React and Framer Motion

Introduction

Building an intuitive and engaging user experience often involves adding motion to UI elements, making the interface feel more dynamic. In this post, I’ll guide you through how I built a menu with smooth, staggered animations using React and Framer Motion. By leveraging these technologies, I achieved a polished interaction flow, enhancing the menu’s functionality and visual appeal.

the animation in work

Here’s how I approached the task.

Tools and Libraries

For this project, I used:

  • React: To structure and manage the UI components using custom hooks paradigm.
  • Framer Motion: A powerful library for animations in React, enabling declarative and complex animations with ease.
  • TypeScript: For adding type safety to the components.

Setting up the Component

The main component, AnimatedMenuButtons is responsible for rendering the menu items and handling their animation states. Here’s a breakdown of the key parts of the code:

1. Component Props

The component takes in three main props:

  • menuItems: An array of menu items.
  • MenuButton: A custom React component for rendering the menu buttons.

Code snippet:

interface Props {
  menuItems: any[];
  MenuButton: React.FC<any>;
}
Enter fullscreen mode Exit fullscreen mode

2. Animation Setup

Using Framer Motion’s motion and stagger features, I set up a staggered animation to display the menu items sequentially, creating a smooth transition. The staggerMenuItems function delays the appearance of each list item by 0.24 seconds.

Code snippet:

const staggerMenuItems = stagger(0.24, { startDelay: 0.05 });
Enter fullscreen mode Exit fullscreen mode

This creates a clean, staggered animation where each button slides into view after a short delay and disappear the same way upon click.


Adding Animations with Framer Motion

3. Handling Animations

I used the useAnimate hook from Framer Motion to control the animation. The animate function handles the transitions for the menu items. Initially, each menu item (li) starts with zero opacity and is translated 200px to the right, which then animates to full opacity and its original position.

animate(
  "li",
  { opacity: 1, translateX: 0 },
  {
    duration: 0.8,
    delay: staggerMenuItems,
  }
);
Enter fullscreen mode Exit fullscreen mode

4. Click Animation

When a menu item is clicked, the clickHandler triggers an animation where the item fades out and slides back to the right. This animation runs before triggering the onClick event provided by the menu item.

const clickHandler = (onClick: () => void) => {
  animate(
    "li",
    { opacity: 0, translateX: 200 },
    {
      duration: 0.8,
      delay: staggerMenuItems,
      onComplete: () => {
        // Important note: mind here that this callback will invoke for each of the <li/> items and if this is not intended, use the then chain callback to run only once.
      },
    }
  ).then( onCompleteFunc );
};
Enter fullscreen mode Exit fullscreen mode

This approach ensures the button clicks are animated smoothly, providing a more immersive experience for the user.


Rendering the Menu Items

Each menu item is rendered inside a motion.li component, where the initial animation state is defined. The onClick event is passed down to trigger the clickHandler, which runs the animation before executing the item's click logic.

Code snippet:

<motion.li
  key={index}
  initial={{
    opacity: 0,
    translateX: 200,
  }}
  onClick={() => clickHandler(item.onClick)}
>
  <MenuButton {...item} />
</motion.li>
Enter fullscreen mode Exit fullscreen mode

Conclusion

Using Framer Motion in React made it straightforward to build an interactive menu with smooth animations. The ability to easily control animation sequences, such as staggered entrances and exits, adds a layer of sophistication to the UI.

If you’re looking to enhance your next project with modern animations, I highly recommend giving Framer Motion a try. It’s incredibly versatile, and with just a few lines of code, you can transform a static interface into something dynamic and engaging.

The full code can be found here:
https://codesandbox.io/p/sandbox/animated-menu-65zz88

Stay tuned for more animation tips!


Let me know in the comments how it worked for you!

framermotion Article's
30 articles in total
Favicon
Building a food simulation game with Next.js and generative design
Favicon
How to Create a Flipping Card Animation Using Framer Motion
Favicon
State-of-the-Art Web Design with Remix, Tailwind, and Framer Motion
Favicon
How I Created a Stunning Portfolio with Next.js, Tailwind CSS, and Framer Motion
Favicon
Creating the iMessage Card Stack Animation: The Interactive Layer
Favicon
Creating the iMessage Card Stack Animation: The Timeline Design
Favicon
How to build awesome website with stunning animation?
Favicon
Adding motion to 3D models with Framer Motion and Three.js
Favicon
Creating a Smooth Animated Menu with React and Framer Motion
Favicon
My-Portfolio
Favicon
Watch Out For Broken Links, 404 Page With Framer Motion, TailwindCSS and NextJs
Favicon
Could not find a declaration file for module framer-motion Error in Next.js 14
Favicon
How to add Animations and Transitions in React
Favicon
Creating a text writing animation in React using Framer-Motion
Favicon
Unveiling My Portfolio Website: A Fusion of Tech and Creativity 🚀
Favicon
React developer portfolio template use framer motion to animate components
Favicon
Building a mini casual game with Next.js
Favicon
Creating a Multi-Level Sidebar Animation with Tailwind and Framer Motion in React
Favicon
Easy Animated Tabs in ReactJS with Framer Motion
Favicon
A Tinder-like card game with Framer-Motion
Favicon
Elevating Web Design with Framer Motion: Bringing Your Website to Life
Favicon
Animating the Web: Route Transitions in Next.js with Framer Motion
Favicon
Enhancing Form Usability with Framer Motion: A Guide to Animated, Chunked Form Transitions
Favicon
How to Build a fully accessible Accordion with HeadlessUI, Framer Motion, and TailwindCSS
Favicon
Build Portfolio Theme in Tailwind CSS & Next.js
Favicon
How to create Scroll-Linked Animations with React and Framer Motion 💃🏻🕺🏻
Favicon
How to build 3 simple animation with framer motion
Favicon
How to create an awesome navigation menu using chakra-UI and framer-motion.
Favicon
Make a slide open envelope
Favicon
Animate everything with Framer Motion

Featured ones: