Skip to content

fix: don't fight browser scroll anchoring (#222) - #261

Draft
justin-schroeder wants to merge 3 commits into
masterfrom
fix/222-scroll-anchoring
Draft

justin-schroeder wants to merge 3 commits into
masterfrom
fix/222-scroll-anchoring

Conversation

@justin-schroeder

Copy link
Copy Markdown
Member

Fixes #222.

Root cause (found in the reporter's video): when an item is added or removed above the viewport, Chrome and Firefox scroll-anchor the page so visible content keeps its on-screen spot. Safari has no scroll anchoring, which is why it looked fine there. AutoAnimate compares page coordinates, so it saw visible items "move" by one row and animated them back, making them jump and slide. For removals, adjustScroll also undid the browser's anchoring, and re-inserting the leaving element made Chromium re-scroll.

Fix

  • remain: the browser's own scroll since the last scroll event (scroll events fire a frame later, so this is the anchoring shift, not user scrolling) is folded into the old coordinates. Items that kept their screen position don't animate. Page-scrolled content only; nested scrollers are unchanged.
  • remove of an element that was entirely above the viewport: skip adjustScroll (it's for removals that shorten the page), keep the leaving element out of anchor selection (overflow-anchor: none), and return to the browser's anchored scroll position after the element is re-inserted.
  • Seed the tracked scroll position at init, so a page that loads already scrolled isn't misread.

Tests: new scroll-anchoring.spec.ts (insert and remove above the viewport) samples a visible item's on-screen position every frame. Before the fix it fails on Chromium and Firefox and passes on WebKit, exactly matching the report. After the fix it passes in all three, 2 repeats each. The full e2e suite: 133 passed, 8 skipped.

@vercel

vercel Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
auto-animate Ready Ready Preview Nov 23, 2026 8:16am UTC

Request Review

@justin-schroeder

Copy link
Copy Markdown
Member Author

Parking as draft 🅿️ — root cause is right, but independent review found regressions vs master with this heuristic ("live scroll − last scroll-event position = anchoring"):

  1. removing the last item at the bottom of a scrolled page: the browser's scroll clamp is treated as anchoring → remaining rows jump (all browsers)
  2. removing a partially visible top row: page/content jumps (Chromium, Firefox)
  3. a user/programmatic scroll in the same task as an insert gets visually cancelled

The tests here only cover fully-offscreen insert/remove. Next step is a sounder way to tell anchoring apart from clamps/pending scrolls (e.g. measuring a visible reference element's viewport position across the mutation), with all of the above as regression tests. Not shipping in 0.11.0.

@justin-schroeder

Copy link
Copy Markdown
Member Author

Second, deeper attempt: no fully correct heuristic found. When a scroll and a mutation land in the same task, neither the scroll-event position nor geometry reliably separates user/programmatic scrolling from browser anchoring. I added draft regression specs to this branch. Notably, master already fails several of these (pending scroll + insert, partially visible insert, nested-container removal, already-scrolled init, smooth pages) in Chromium/Firefox, so this is long-standing, not new. Keeping this as a draft research branch; the user-facing workaround (overflow-anchor: none on the scroller) is documented on #222.

This branch was successfully deployed

1 active deployment
Preview — ef3fbb53 Deployed Nov 23, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Broken animation when add element outside viewport

1 participant