motion icon indicating copy to clipboard operation
motion copied to clipboard

Exit animation not working

Open HamzaHamani opened this issue 2 years ago • 9 comments

The exit animation for a <Header Component aint working and its already wrapper with Animate Presence

inside Header there is a multiple Links for a nested route , when i click to display the

initial animation works, but when i click on a Link to a route the <Header just disapear without an animation

BTW ive already tried wrapping the whole page with Animate Presence and its not working also

import { useEffect } from "react";
import { Outlet, useLocation } from "react-router-dom";
import Header from "../ui/Header";
import { AnimatePresence } from "framer-motion";


  return (
    <div>
      <Header /> 
      <div className="h-[90vh]  bg-bg200 w-full   overflow-hidden ">
        <AnimatePresence mode="wait">
          <Outlet />
        </AnimatePresence>
      </div>
    </div>
  );
}

export default Admin;

Header Code

export default function Header() {
  const [isActive, setIsActive] = useState(false);

  return (
    <>
      <div
        onClick={() => {
          setIsActive(!isActive);
        }}
        className={styles.button}
      >
        <div
          className={`${styles.burger} ${isActive ? styles.burgerActive : ""}`}
        ></div>
        <AnimatePresence mode="wait">{isActive && <Nav />}</AnimatePresence>
      </div>
    </>
  );
}

HamzaHamani avatar Dec 21 '23 17:12 HamzaHamani

You are probably missing the key attribute. Try: <AnimatePresence mode="wait">{isActive && <div key={"nav"}><Nav /></div>}</AnimatePresence> The different elements being animated in/out have to have different keys And don't forget only works on direct children (that's why simply wrapping the whole page also wouldn't work)

pchurro avatar Dec 21 '23 17:12 pchurro

sadly it didnt work. when i navigate to other route inside the Admin page , exit animation doesnt work

HamzaHamani avatar Dec 21 '23 19:12 HamzaHamani

I'm also seeing this bug using [email protected]

  const CopyNotification = ({ isNotificationOpen }) => {
    return (
      <AnimatePresence>
        {isNotificationOpen && (
          <motion.div
            key="copy-notification"
            className="absolute left-1/2 top-0 z-50 select-none bg-white px-2 py-1 drop-shadow-lg before:absolute before:bottom-0 before:left-1/2 before:h-0 before:w-0 before:-translate-x-1/2 before:translate-y-full before:border-x-8 before:border-t-[8px] before:border-x-transparent before:border-t-white"
            initial={{
              opacity: 0,
              x: "-50%",
              y: "-50%",
              scale: 0.2
            }}
            animate={{
              opacity: 1,
              y: "-120%",
              scale: 1,
              transition: { ease: "backInOut", duration: 0.2 }
            }}
            exit={{
              opacity: 0,
              transition: { ease: "easeOut", duration: 1 }
            }}
          >
            Copied!
          </motion.div>
        )}
      </AnimatePresence>
    );
  };

ChazUK avatar Dec 22 '23 12:12 ChazUK

Probably there is an error in the navigating page, even key errors will cause the page to refresh

abdulfattahaud avatar Dec 27 '23 10:12 abdulfattahaud

actually the page doesnt refresh , its SPA so everything working normally

HamzaHamani avatar Dec 27 '23 15:12 HamzaHamani

Yeah, I see. Me as well after I fixed that thought it's gonna work, but it didn't

abdulfattahaud avatar Dec 27 '23 15:12 abdulfattahaud

In the original example, the direct child of AnimatePresence isn't being unmounted.

Feel free to reopen with a sandbox example.

mattgperry avatar Jan 02 '24 14:01 mattgperry

I was facing the same issue and found this article.

Dropping it in here for my fellow coders

mixmaker avatar May 03 '24 13:05 mixmaker

I'm also experiencing the same thing and have tried many ways to fix it, and I finally figured out which part is making it not work. I think the reason why the animation is not running is because of , it is not triggering the exit animation because it is not unmounting as expected which causes the exit animation not to be triggered.

athayanaufalbrasta avatar Feb 13 '25 16:02 athayanaufalbrasta