Repository navigation
Fix timeline scroll anchor while older rows load - #2427
Merged
Merged
Conversation
ymichael
added a commit
that referenced
this pull request
Oct 20, 2026
) ## 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`. #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 `ResizeObserver`s, 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](https://claude.com/claude-code) > AGENT GENERATED Co-authored-by: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong
When loading older timeline rows,
BottomAnchoredScrollBodycaptured both the currentscrollHeightandscrollTopbefore starting the request. If the user continued scrolling while that request was in flight, the pending anchor kept the earlierscrollTop. Once the rows mounted, prepend compensation restored that stale position plus the height delta, visibly undoing part of the user's scroll. Browser-native scroll anchoring also emits scroll events during a prepend, so those events cannot safely be treated as fresh user intent.What changed
scrollTopto the user's latest position but retain the originalscrollHeightbaseline used for compensation.This is UI-only and does not change the host-daemon protocol or any CLI surface.
How you verified
expected 200,received 250) and passed afterward.pnpm exec turbo run test --filter=@bb/app -- --run src/components/ui/bottom-anchored-scroll-body.scroll-preservation.test.tsx src/components/thread/timeline/useAutoLoadOlderRows.test.tsx src/components/thread/timeline/ThreadTimelineRows.height.test.tsxpnpm exec turbo run typecheck --filter=@bb/apppnpm exec turbo run test --filter=@bb/app --force— 431 files and 3,358 tests passed; 3 skipped.pnpm exec turbo run lint --filter=@bb/app— 0 errors.Fixes the timeline scroll-position jump while older messages load.