motion icon indicating copy to clipboard operation
motion copied to clipboard

[BUG] `AnimatePresence` + `layout` bugs

Open zinchenkoivan opened this issue 1 year ago โ€ข 13 comments

1. Read the FAQs ๐Ÿ‘‡

2. Describe the bug

After updating the library from version "framer-motion": "^10.16.16" to version "motion": "^11.18.0", a strange issue appeared. The toggle that contains layout (below on line 41) prevents the dialog window from being removed from the DOM. Maybe this issue is related to this one.

3. IMPORTANT: Provide a CodeSandbox reproduction of the bug

URL

4. Steps to reproduce

  1. Click Open
  2. Click Next
  3. Click Previous
  4. Click ะกlose
  5. Observe that element with class "rdk-portal" is still present in DOM elements (with red background)

5. Expected behavior

The element should be removed from the DOM when closed.

6. Video or screenshots

https://github.com/user-attachments/assets/c4741852-b29b-46ac-adbf-04a9cce308cd

7. Environment details mac OS 15.3, Google chrome latest.

zinchenkoivan avatar Feb 11 '25 07:02 zinchenkoivan

Just chiming in to say that we're observing the same bug: thanks for the great report and repro case.

siddarth avatar Feb 12 '25 22:02 siddarth

I've had same issue on my website, wrap your layout components with <AnimatePresence>, that worked for me ๐Ÿ‘

ImExoOdeex avatar Feb 13 '25 23:02 ImExoOdeex

I faced the exact same issue (using layoutId instead of layout though) and I found this issue thanks to the previous comment that made if go to the top of the issues list ๐ŸŽ‰

In my case, I'm using a motion.div with a layoutId that is a child of a modal using AnimatePresence higher in the tree.

I could bisect the exact change that introduced the issue: https://github.com/motiondivision/motion/pull/2992 and first appeared in v11.17.0.

This issue is the same as https://github.com/motiondivision/motion/issues/1619

ValentinH avatar Feb 14 '25 10:02 ValentinH

Chiming in to say I think this is also happening to me.

I have a component with a custom cursor that whenever the mouse is not on top of the component, the cursors lock in at the center of the component (using two different divs and layoutId):

{hoveringFilm && <motion.div
                layoutId="cursor"
                layout="position"
                className={"${styles.cursor} ${styles.cursorFollowing}"}
                transition={spring}
                key={"cursor"}
                initial={{ x: mouse.x - 50, y: mouse.y - 50 }}
                animate={{ x: mouse.x - 50, y: mouse.y - 50 }}
            >
                <motion.div key={"cursorTextWrapper"} className={styles.cursorTextWrapper} style={{ overflow: "hidden" }}> 
                         <motion.div className={styles.cursorText}>Play video</motion.div>
                 </motion.div>
              </motion.div>}

{!hoveringFilm && <motion.div
                layoutId="cursor"
                className={"${styles.cursor} ${styles.cursorFixed}"}
                key={"cursorFixed"}
                transition={spring}
               >
                <motion.div key={"cursorTextWrapper"} className={styles.cursorTextWrapper} style={{ overflow: "hidden" }}> 
                        <motion.div className={styles.cursorText}>Play video</motion.div>
                 </motion.div>
            </motion.div>}

I noticed that, whenever I hovered the component, afterwards I couldn't change route. The page transition would start but it would get stuck halfway. I tried removing the layout animation (removed the layout props) and this did stop happening. Following @ImExoOdeex 's suggestion I wrapped the divs in an <AnimatePresence> and it fixed it for me.

pchurro avatar Feb 14 '25 11:02 pchurro

A quick attempt at a reproduction hasn't worked so I'll take a look on Monday. In the meantime a really simple example I could throw in a test would be appreciated.

Something like this (though this didn't reproduce the bug yet)

import { AnimatePresence, motion } from "framer-motion"
import { useEffect, useState } from "react"

export const App = () => {
    const [width, setWidth] = useState(100)
    const [presenceState, setPresenceState] = useState(true)

    useEffect(() => {
        if (width === 200) return
        const timeout = setTimeout(() => {
            setWidth(50)

            setTimeout(() => {
                setWidth(200)
            }, 1000)
        }, 1000)

        return () => clearTimeout(timeout)
    }, [width])

    useEffect(() => {
        setTimeout(() => {
            setPresenceState(false)
        }, 2100)
    }, [presenceState])

    return (
        <>
            <AnimatePresence initial={false}>
                {presenceState && (
                    <motion.div exit={{ opacity: 0 }}>
                        <motion.div
                            layout
                            style={{
                                width,
                                height: 100,
                                background: "red",
                            }}
                        >
                            Presence
                        </motion.div>
                    </motion.div>
                )}
            </AnimatePresence>
        </>
    )
}

mattgperry avatar Feb 14 '25 15:02 mattgperry

Why was this closed though?

ValentinH avatar Feb 14 '25 16:02 ValentinH

Friday evening ๐Ÿ˜…

mattgperry avatar Feb 14 '25 16:02 mattgperry

We're having a bug that sounds similar after upgrading to 11.18.2 - layout animations are "cancelled"/immediately snapping when an item is added in AnimatePresence. This seems to be fixed though in v12.0.1, and the changelog description for that sounds relevant. I wasn't however able to repro in an isolated sandbox

Just adding this anecdote in case it's helpful. Again not sure if it's exactly the same bug but seemed vaguely similar

kelseyyoung avatar Feb 18 '25 18:02 kelseyyoung

Got a reproduction, hopefully a fix today/tomorrow

mattgperry avatar Feb 20 '25 11:02 mattgperry

Fix is out on 12.4.7, can those affected verify this fixes the issue for them?

mattgperry avatar Feb 20 '25 12:02 mattgperry

@mattgperry I can confirm 12.4.7 did fix my scenario. Thank you!

pchurro avatar Feb 20 '25 12:02 pchurro

I confirm that it fixed my issue as well. Thank you!!

ValentinH avatar Feb 20 '25 13:02 ValentinH

Also can confirm 12.4.7 fixed my issue. Thank you!

zinchenkoivan avatar Feb 20 '25 13:02 zinchenkoivan