Skip to content

fix(app): keep the reading position when older timeline rows load - #5879

Merged
ymichael merged 1 commit into
mainfrom
bb/repro-and-investigate-android-timeline-jump-thr_rtd7gdb7qi
Oct 20, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/repro-and-investigate-android-timeline-jump-thr_rtd7gdb7qi

Conversation

@ymichael

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

When you scroll up a long thread, older pages load and get inserted above the reader. Two scroll writers then disagreed about where the reader was:

  1. A stale prepend anchor. useAutoLoadOlderRows asked BottomAnchoredScrollBody to record scrollTop when the request started, 600px before the top. After the page arrived, it restored recorded scrollTop + inserted height. Fix timeline scroll anchor while older rows load #2427 advanced that record only on scroll events that follow wheel, touch, or key input, so fling momentum after the finger lifts was never counted. The content jumped by however far the fling coasted after the request started, up to ~600px on Android.
  2. TanStack overwriting the restore. The first older page usually takes the list past 20 rows, which turns windowing on in the same commit. The scroll body wrote the correct scrollTop, and ~12 ms later TanStack's applyScrollAdjustment applied a measurement correction to its cached offset from before the write. That wiped out the restore and sent the reader thousands of px back toward the start of the newly loaded history (e.g. 4742 → 865).

Both come from several writers (fetch-time prepend restore, TanStack size corrections, native overflow-anchor) each computing an absolute position from their own stale model.

What changed

  • New ScrollPositionKeeper (apps/app/src/components/ui/scroll-position-keeper.ts) is the single owner of the reading position while the reader is scrolled away from the bottom. It anchors the first row that starts in the viewport (descending into nested rows of a row that straddles the top) by its offset in the content, not by scrollTop. Your own scrolling, including momentum, and navigation writes never change that offset; only layout changes above the anchor do. It folds those into relative scrollTop += Δ writes computed from live layout. The anchor is never captured ahead of time, so it cannot go stale.
  • Triggers: scroll events (reconcile, then re-pick the anchor), content/viewport ResizeObservers, every TimelineWindowedItems commit, and TanStack onChange (after it repositions rows).
  • TimelineWindowedItems: while the keeper is active, TanStack no longer adjusts or writes scroll (shouldAdjustScrollPositionOnItemSizeChange → false, scrollToFn skips elementScroll). It registers a row-offset source so the keeper can locate an anchor row the window has not mounted yet (the windowing-activation commit), and the keeper feeds TanStack its writes through observeElementOffset so the range is recomputed before paint. Stuck-to-bottom behaviour is unchanged.
  • BottomAnchoredScrollBody: removes captureScrollAnchor, the scrollHeight-delta prepend restore, and the input-gated anchor bookkeeping. holdContentPosition now holds the element edge through the keeper (covering async resizes during the hold) instead of toggling native anchoring and nudging scrollTop. Native overflow-anchor is off only while the keeper holds a row, and comes back at the bottom.
  • TimelineDetailScroll / useStickyBottomScroll: nested detail scrollers use the same keeper when detached.

No wire, CLI, or plugin API changes. BottomAnchorContextValue is app-internal.

How you verified

  • Repro on the Android emulator (Pixel 9, Chrome 133), driving real adb touch flings against a real 1,954-event thread, with a per-frame row-position probe over CDP:
    • main: visible jump equal to the coasted distance (−606, −446, −574 px). When windowing activated, the reading row unmounted (teleport). Also a 1691 px jump at an idle page load.
    • branch: idle page loads 0, 0, 0, 1, 2, 3 px. Mid-fling loads keep the fling velocity continuous.
  • Desktop Chrome (headless, CDP wheel input): main unmounts the reading row at the windowing-activation load (2/2 runs); the branch keeps it (0 px, plus one load that coincided with a 120 px wheel tick).
  • iOS Safari Simulator 18.4 (Maestro flings): branch matches main. The reading row settles within 0–11 px of fling motion on both, and WebKit shows one transient frame at the write on both builds.
  • New scroll-position-keeper.test.ts (prepend, momentum before arrival, composition with scrolling, foreign writes, unmounted anchor via a row-offset source, nested anchors, inactive mode, edge holds). New body test keeps the reading row still when older rows load after momentum scrolling fails on main (no compensation without the fetch-time capture) and passes here. Existing scroll-preservation tests moved to geometry where rows move with scrollTop.
  • pnpm exec turbo run typecheck lint test --filter='...[origin/main]': 16/16 tasks pass.

🤖 Generated with Claude Code

AGENT GENERATED

Scrolled-up timelines now keep the reading position with one owner, the
ScrollPositionKeeper. It anchors a row by its offset in the content and
folds layout changes above that row into relative scrollTop writes.
Neither the fetch-time prepend capture nor TanStack's cached-offset
corrections write the scroll position any more while the reader is
detached, so fling momentum and windowing activation can no longer
undo the reader's position.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit 4c6be2c into main Oct 20, 2026
28 checks passed
@ymichael
ymichael deleted the bb/repro-and-investigate-android-timeline-jump-thr_rtd7gdb7qi branch October 20, 2026 18:24
ymichael added a commit that referenced this pull request Oct 21, 2026
The timeline list sits in an AutoHeightContainer whose explicit height
follows its content one ResizeObserver tick late. TimelineWindowedItems
snapped it before TanStack's scroll writes, but since #5879 the
ScrollPositionKeeper makes those writes and never snapped it. When an
older page prepended rows, the keeper's correction was clamped to the
old maximum: the reader lost their place and landed on the old bottom.
The next scroll event then read that position as a return to the
bottom, reattached, and the shell scrolled to the real bottom.

The keeper now runs registered scroll extent syncs before any write
past the current extent, and TimelineWindowedItems registers the
auto-height snap. iOS hold timing is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael added a commit that referenced this pull request Oct 22, 2026
Window timeline lists on iOS WebKit too. ScrollPositionKeeper (#5879)
stays the only scroll writer, and it gains a hold mode for iOS. While a
finger is down, momentum is running, or the edge is bouncing, it keeps
the correction as a transform on the scroll content. It writes the
correction to scrollTop once scrolling has been still for 150 ms.
TanStack reads scrollTop plus the held offset, so the window matches what
is on screen.

Also:
- Upgrade @tanstack/react-virtual to 3.14.20 (virtual-core 3.19.5) and
  drop the virtual-core 3.17.1 patch, which is fixed upstream.
- Restore the saved reading row when a detached reader's scrollport
  changes width (iPad rotation), clamped to the row's new height.
- Re-read the live scroll extent before treating a position past the
  cached extent as a return to the bottom.
- While a saved-row restore waits for its row, park at the bottom
  instead of the scroll root's initial position. That position could
  keep loading older pages, all the way to the start of the thread.
- Document browser scroll ownership in docs/timeline-pagination.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael added a commit that referenced this pull request Oct 22, 2026
The timeline list sits in an AutoHeightContainer whose explicit height
follows its content one ResizeObserver tick late. TimelineWindowedItems
snapped it before TanStack's scroll writes, but since #5879 the
ScrollPositionKeeper makes those writes and never snapped it. When an
older page prepended rows, the keeper's correction was clamped to the
old maximum: the reader lost their place and landed on the old bottom.
The next scroll event then read that position as a return to the
bottom, reattached, and the shell scrolled to the real bottom.

The keeper now runs registered scroll extent syncs before any write
past the current extent, and TimelineWindowedItems registers the
auto-height snap. iOS hold timing is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

1 participant