Fix: Terminal scrolls to top after tab switch (Issue #560)#561
Merged
waleedkadous merged 1 commit intomainfrom Feb 25, 2026
Merged
Conversation
…one toggling xterm's buffer.active.viewportY (backed by ydisp) becomes stale when the terminal container is hidden via display:none during tab switches or panel collapse. The browser resets the viewport scrollTop to 0, and xterm syncs ydisp to 0. When safeFit() later reads viewportY=0, it 'preserves' this incorrect position at the top of the buffer. Fix: Track scroll state (viewportY, baseY, wasAtBottom) externally in JS variables updated via term.onScroll(). These variables are immune to DOM state changes from display toggling. safeFit() now reads from the tracked state instead of xterm's buffer. Also rewrites Terminal.fit-scroll.test.tsx with 7 tests including 3 new ones specifically for the Issue #560 scenario, and adds the onScroll mock to all 9 other terminal test files.
Contributor
Author
Architect ReviewClean root cause analysis: xterm's Fix tracks scroll state externally via 175/175 tests pass. Approved. Architect review |
waleedkadous
added a commit
that referenced
this pull request
Feb 25, 2026
PR #561 changed safeFit()'s early-return condition from `if (!baseY)` to `if (!baseY && !scrollState.baseY)`, adding a dependency on the externally- tracked scrollState.baseY. This was unnecessary because baseY (from term.buffer.active.baseY) reflects actual buffer scrollback and is NOT affected by display:none toggling — unlike viewportY which the external tracking was designed to protect. The stale scrollState.baseY could cause safeFit() to take the scroll- preserving path when the buffer had no scrollback (e.g., after a clear), leading to incorrect scroll restoration with stale position values. Fix: Revert to `if (!baseY)` while keeping scrollState.wasAtBottom and scrollState.viewportY for position values (the core Issue #560 fix). Adds regression test for the stale scrollState.baseY scenario. Fixes #563
4 tasks
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.
Summary
Fixes #560
Root cause: xterm's
buffer.active.viewportY(backed by internalydisp) becomes stale when the terminal container is hidden viadisplay:noneduring tab switches or panel collapse. The browser resets the viewport element'sscrollTopto 0, and xterm syncsydispto 0. WhensafeFit()later readsviewportY=0, it preserves this incorrect position at the top of the buffer.Fix: Track scroll state (
viewportY,baseY,wasAtBottom) externally in JS variables updated viaterm.onScroll(). These variables are immune to DOM state changes from display toggling.safeFit()now reads from the tracked state instead of xterm buffer.Tests: Rewrote
Terminal.fit-scroll.test.tsxwith 7 tests (previously all 4 were skipped due to jsdomgetBoundingClientRectreturning 0x0). Three new tests specifically cover the Issue Terminal still scrolls to top of buffer (regression from #423) #560 scenario. AddedonScrollmock to all 9 other terminal test files.Changes
Terminal.tsxterm.onScroll(), modifiedsafeFit()to use tracked state, synced tracked state after replay flushTerminal.fit-scroll.test.tsxonScrollmock toMockTerminalclassTest plan
tsc --noEmit)