question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Animate React Components (not just native DOM elements)

See original GitHub issue

I quite often use the @reach/dialog package for creating modals or other things things like off-screen mobile menus.

I’d like to animate the <DialogOverlay> and <DialogContent> components using Framer Motion.

In react-spring, I could do something like this:

const AnimatedDialogOverlay = animated(DialogOverlay);
const AnimatedDialogContent = animated(DialogContent);

Is there a way to do something similar in Framer Motion or can you only animate native DOM elements?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

7reactions
lukebennett88commented, Sep 20, 2020

Sure thing @lednhatkhanh, here is an example:

import React from 'react';
import { DialogOverlay, DialogContent } from '@reach/dialog';
import { AnimatePresence, motion } from 'framer-motion';

function Sidebar({ children, isSidebarOpen, setSidebarOpen }) {
  const handleDismiss = () => setSidebarOpen(false);

  const MotionDialogOverlay = motion.custom(DialogOverlay);
  const MotionDialogContent = motion.custom(DialogContent);

  return (
    <AnimatePresence>
      {isSidebarOpen && (
        <MotionDialogOverlay
          initial={{ opacity: 0 }}
          animate={{ opacity: 1 }}
          exit={{ opacity: 0 }}
          onDismiss={handleDismiss}
        >
            <MotionDialogContent
              initial={{ x: '100%' }}
              animate={{ x: '0%' }}
              exit={{ x: '100%' }}
              transition={{ min: 0, max: 100, bounceDamping: 9 }}
              aria-label="Sidebar menu"
            >
              {children}
            </MotionDialogContent>
        </MotionDialogOverlay>
      )}
    </AnimatePresence>
  );
}

export { Sidebar };
1reaction
lednhatkhanhcommented, Sep 20, 2020

@lukebennett88 Thank you so much!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Animation Add-Ons - React
Every DOM component that React can render is available for use. However, component does not need to be a DOM component. It can...
Read more >
Animate React components with a single line using AutoAnimate
AutoAnimate makes for a smooth experience when an element changes in the DOM. I would like to compare AutoAnimate with React Transition ...
Read more >
How to Animate Components' Entrance and Exit in React
React Transition Group does NOT do the animation for you, that is it does not provide the animation. It facilitates adding the animation...
Read more >
Animations with React: How a simple component can affect ...
Next, we will use the useEffect hook to synchronize our component with the DOM state. When the element is mounted, and after we...
Read more >
5 Ways to animate a React app. - Medium
React Transition Group changes the classes when component lifecycle changes. In turn, animated style should be described in CSS classes.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found