Repository navigation
DataViews: Fix infinite-scroll list jumping while a page loads - #79335
StevenDufresne wants to merge 3 commits into
Conversation
The infinite-scroll scroll-anchor restoration could move the scroll position out from under the user. On a window advance it captures an anchor element and its offset, then on a later render re-applies that offset. When pages load asynchronously the user keeps scrolling between capture and restore, and the restoration treated that user-driven movement as drift to "correct" — snapping the list back to the capture-time position and undoing the scroll. Store the scrollTop at capture and add the intervening scroll delta back into the adjustment, so only the content-driven shift (items added or removed above the anchor) is compensated. When the user does not scroll during the load the delta is zero and behaviour is unchanged. Also add an "Async Infinite Scroll" story that reveals data one page at a time asynchronously with a toggling isLoading, which reproduces the jump; the existing synchronous InfiniteScroll story never exercises the restoration path. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Size Change: +53 B (0%) Total Size: 8.6 MB 📦 View Changed
|
|
Flaky tests detected in 4ed6867. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/27801353562
|
|
Thanks for the PR! I haven't been able to repro the issue on storybook by checking out the trunk version of |
| const scrollAdjustment = currentOffset - anchor.viewportOffset; | ||
| // Add back any scrolling the user did since capture so we only | ||
| // compensate the content-driven shift, not their own scroll. | ||
| const scrollAdjustment = |
There was a problem hiding this comment.
Thanks for the PR! While this code doesn't create the jump, it can also lead to not detecting that we're at the bottom again and scrolling down can't fetch more items (see video).
Screen.Recording.2026-06-19.at.10.07.57.AM.mov
I tried a few things like triggering a scroll event or even offsetting by a couple of pixels but it seems it's also connected to the fact that we have skeleton items(affects how much a user can keep scrolling) and BOTTOM_THRESHOLD (and top) values.
I guess there would be the same issue for scrolling up and maybe we need to have an effect attached to loading value that calculates again things, outside of scroll effect.
--cc @tellthemachines that definitely knows more in this area.
|
Thanks for the quick eyes. I'll admit the agent disobeyed the rules and opened this early. It should have stayed in draft until I could give it a detailed audit. I'll reopen afterwards to make sure your time is well spent. (It did work for my use case in a separate project though) 🙇 |
|
@StevenDufresne no worries, let me know when it's ready for review! |
|
I opened a new PR, #79546, to make a couple of changes:
|
What?
Fixes a bug in DataViews infinite scroll where the list jumps (the scroll position is yanked upward) while you scroll down to load more, when the consumer loads pages asynchronously.
This PR also adds an
Async Infinite Scrollstory that reproduces the bug in one step.Why?
The infinite-scroll scroll-anchor restoration is meant to keep the viewport stable when items are inserted/removed (e.g. paging in older items). It works by capturing an anchor element and its viewport offset when the window advances, then re-applying that offset after the next render.
The problem: capture happens inside the throttled
scrollhandler the instant the window advances, but the restoration runs on a later render (it's gated onisLoading). When pages load asynchronously, the user keeps scrolling between capture and restore. The restoration can't distinguish that user-driven movement from a content-driven shift, so it "corrects" it — snapping the list back to where the anchor sat at capture time and undoing the scrolling the user did while the page was loading.The existing synchronous
InfiniteScrollstory never hits this: it passes noisLoadingand resolves data synchronously, so the restoration effect never runs and there's no async gap for the user to scroll into. Real consumers that load over the network (a notifications list, for example) hit it consistently.How?
Using content-position
Pand scrollS, an element's offset from the container top isP − S.viewportOffset = P1 − S1currentOffset = P2 − S2; the old adjustmentcurrentOffset − viewportOffsetresolves the scroll position toP2 − P1 + S1— i.e. it pins the anchor to its capture-time position, discarding the user's scroll(S2 − S1). For a pure scroll-down appendP2 = P1, so the adjustment isS1 − S2, exactly negating the user's scroll.The fix stores
scrollTopat capture and adds the intervening scroll delta back:So only the content-driven shift is compensated:
P2 = P1→ adjustment0→ no jump.P2 = P1 + H→ adjustmentH→ position correctly held.When the user does not scroll during the load,
scrollTop == capturedScrollTop, the added term is0, and behaviour is byte-for-byte identical to before — so this only changes the currently-broken case.Testing Instructions
npm run storybook:devpackages/dataviews/src/hooks/use-infinite-scroll.tsfromtrunk(git checkout trunk -- packages/dataviews/src/hooks/use-infinite-scroll.ts), keep the story, and repeat step 3 — the list jumps upward as each page settles.Testing Instructions for Keyboard
Not a UI/markup change. The list can still be scrolled with the keyboard (focus a row, then
Page Down/ arrow keys); the same stability applies.Screenshots or screencast
Use of AI Tools
This change was authored with the assistance of AI tooling (Claude Code). The root cause was diagnosed against a live consumer, the fix and the repro story were reviewed and verified by a human in Storybook before submitting.