motion icon indicating copy to clipboard operation
motion copied to clipboard

[BUG] Issue with Exit Animations in React 19 / Next.js 15 / Framer-Motion Application

Open Looxor opened this issue 1 year ago • 0 comments

Problem Description

When implementing page transitions in a React application using Framer Motion, there is an inconsistent behavior with exit animations:

  1. First Transition: Exit animations on the Home page work correctly when leaving the page for the first time
  2. Subsequent Transitions: When returning to the Home page and attempting to leave again (without browser refresh), the exit animations are prematurely interrupted:
    • The "EXIT" animation starts but is cut short
    • The route change occurs almost immediately
    • The complete exit animation sequence is not played

Technical Context

  • React 19
  • Next.js 15
  • Motion 11.13.5
  • Client-side navigation
  • AnimatePresence component for handling mount/unmount animations

Expected Behavior

The exit animation should:

  • Play completely before any route change occurs
  • Maintain consistent behavior across multiple navigation attempts
  • Follow the same timing and sequence regardless of navigation history

This issue appears to be related to how Framer Motion handles exit animations in conjunction with Next.js route changes, particularly in subsequent navigation attempts after the initial page load.

useTransition.ts

const navigateDirect = useCallback(async (nextRoute: RouteKeys) => {
    if (isAnimating) return;
    setIsAnimating(true);
    setViewTransition("CLOSING");
    setPendingRoute(nextRoute);
    
    await new Promise((resolve) => {
      controls.start("EXIT").then(() => { setTimeout(resolve, 500) });
    });
    
    router.push(nextRoute);
    setCurrentRoute(nextRoute);
    setIsAnimating(false);
  }, [controls, isAnimating, router, setCurrentRoute, setViewTransition]);

Template.ts (Wrapper)

<AnimatePresence mode="wait" onExitComplete={() => { 
  if (pendingRoute) { 
    setTimeout(() => {
      router.push(pendingRoute);
      setPendingRoute(null);
      setIsAnimating(false);
    }, 500);
  }
}}>
  <motion.div key={currentRoute} initial="READY" animate={controls} exit="EXIT">
    {children}
  </motion.div>
</AnimatePresence>

ButtonNextPage.tsx (Triger Redirection to the Next Page)

interface NextPageProps { className?: string; variants: Variants; style: React.CSSProperties }

const NextPage: React.FC<NextPageProps> = ({ className, style, variants }) => {
  const { navigateDirect, isAnimating } = useTransition();
  const handleClick = useCallback(async () => {
    if (isAnimating) return;
    try { await navigateDirect("/Page2") } 
    catch (error) { console.error("Navigation error:", error) }
  }, [navigateDirect, isAnimating]);

  return (
    <m.div className={className} variants={variants} style={style}>
      <Button className={styles.NextPage_button} aria-label="Next Page" onClick={handleClick} disabled={isAnimating}>
        <NextPageIcon className={styles.NextPage}/>
      </Button>
    </m.div>
  );
};
export default NextPage;

Looxor avatar Dec 29 '24 15:12 Looxor