Repository navigation
Route change on a scrolled window causes jump in layout animation #3405
Replies: 1 comment
|
The jump happens because of a timing collision, not a Motion bug. Next.js App Router scrolls the window to the top on navigation, and Motion's layout system snapshots your outgoing element's position, then measures again after the DOM updates, to compute the FLIP delta. If AnimatePresence has already taken the exiting element out of normal flow (which it does for layout animations, typically via absolute positioning relative to its nearest positioned ancestor) and the page scroll changes between those two measurements, the delta ends up wrong because the exiting element is now positioned relative to a viewport that moved out from under it. Two documented props are relevant here. <motion.div layoutRoot style={{ position: "fixed", inset: 0 }}>
<AnimatePresence mode="popLayout">
<motion.div key={pathname} initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }}>
{children}
</motion.div>
</AnimatePresence>
</motion.div>Combine that with suppressing the router's own scroll reset during the transition ( |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
In client side route changes, the router automatically scrolls the window to the top. That forced scroll repositioning moves the outgoing content in the viewport, so Framer Motion begins its animation from a suddenly shifted layout. The result is a visible jump.
Here's an example with Next.js App Router
Does anyone have any tips?
Related:
All reactions