Skip to content

fix: Back from a linked document keeps the reader's place - #2084

Merged
backnotprop merged 2 commits into
mainfrom
fix/linked-doc-scroll
Oct 22, 2026
Merged

backnotprop merged 2 commits into
mainfrom
fix/linked-doc-scroll

Conversation

@backnotprop

@backnotprop backnotprop commented Oct 22, 2026 •

Copy link
Copy Markdown
Owner

Following a link from the middle of a long document and pressing Back used to land the reader at the top (or bottom) of the document.

Now, when a linked document (HTML or markdown) is opened from the root, useLinkedDoc remembers the first block visible at the top of the scroll viewport and how far its top sat from the viewport's top. back() scrolls that block back to the same offset once the root has rendered, and again at about 150, 400 and 1000 ms to absorb late layout (re-applied highlights, images, a sidebar that changed the column width). A scroll the reader makes in between wins. Anchoring by block instead of raw pixels matters: coming back from an HTML document the column is wider, so the same scrollTop is a different place.

  • packages/ui/utils/scrollAnchor.ts (new): capture and restore.
  • useLinkedDoc: new optional scrollViewport option (additive; without it nothing changes). Noted in packages/ui/HANDOFF.md.
  • packages/editor/App.tsx passes its scroll viewport.
  • DOM test packages/ui/hooks/useLinkedDoc.scrollRestore.test.tsx (added to the CI DOM list); it fails without the fix.
  • Checked in headless Chromium on a real annotate session: a long markdown file linking to an HTML page and a markdown page; after Back the same paragraph is at the same offset in both cases.

The restore never fights a scroll it did not make: it ends on the reader's own input (wheel, touch, key, pointer), on any foreign scroll, on the next navigation and on unmount. back({ restoreScroll: false }) skips it and cancelScrollRestore() stops it; the app uses them where it scrolls to a target itself (a jump to a comment in the root, a Contents click, selecting an annotation, WebMCP reveal), since assigning scrollTop during a smooth scrollIntoView cancels it in Chromium.

Fixes #2082.

Opening a linked document from the root now remembers the first block
visible at the top of the scroll viewport and its offset; back() scrolls
that block back to the same place once the root has rendered, and again
as late layout settles, unless the reader scrolls first.

Fixes #2082.
…ll restore

back({ restoreScroll: false }) skips the restore, and cancelScrollRestore()
stops one still settling. App cancels it when it selects an annotation,
navigates from Contents or reveals for WebMCP, and the jump path's Back
skips it. The restore also ends on the reader's own input and on unmount,
and clears its frames and timers.
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.

[Bug] Back from a linked document does not restore the previous scroll position

1 participant