Skip to content

Fix timeline scroll anchor while older rows load - #2427

Merged
ymichael merged 1 commit into
mainfrom
bb/fix-scroll-position-jumps-thr_6p77xirk9k
Aug 25, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/fix-scroll-position-jumps-thr_6p77xirk9k

Conversation

@ymichael

Copy link
Copy Markdown
Collaborator

What was wrong

When loading older timeline rows, BottomAnchoredScrollBody captured both the current scrollHeight and scrollTop before starting the request. If the user continued scrolling while that request was in flight, the pending anchor kept the earlier scrollTop. 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

  • Track whether the next scroll event follows direct wheel, touch, keyboard, or pointer input.
  • While a prepend anchor is pending, advance its scrollTop to the user's latest position but retain the original scrollHeight baseline used for compensation.
  • Clear the direct-input marker when capturing an anchor, so browser-generated scroll events cannot replace the explicit position.
  • Extend the scroll-preservation tests to exercise actual row prepends, continued user scrolling during the load, and native browser anchoring.

This is UI-only and does not change the host-daemon protocol or any CLI surface.

How you verified

  • Confirmed the new regression test failed before the implementation (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.tsx
  • pnpm exec turbo run typecheck --filter=@bb/app
  • pnpm 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.
  • Reproduced against the affected real thread with dev-browser and a controlled 1.2-second older-page delay: the tracked message moved 252px before the fix and 1px after it.

Fixes the timeline scroll-position jump while older messages load.

AGENT GENERATED

@ymichael
ymichael merged commit 392c978 into main Aug 25, 2026
15 checks passed
@ymichael
ymichael deleted the bb/fix-scroll-position-jumps-thr_6p77xirk9k branch August 25, 2026 21:13
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]>
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