diff --git a/ui-tui/src/__tests__/viewportStore.test.ts b/ui-tui/src/__tests__/viewportStore.test.ts index 7a571fb95a..d9b2e66251 100644 --- a/ui-tui/src/__tests__/viewportStore.test.ts +++ b/ui-tui/src/__tests__/viewportStore.test.ts @@ -87,4 +87,22 @@ describe('viewportStore', () => { expect(getScrollbarSnapshot(handle as any).top).toBe(10) }) + + it('uses fresh scroll height to clear stale scrollbar non-bottom state after shrink', () => { + const handle = { + getFreshScrollHeight: () => 40, + getScrollHeight: () => 60, + getScrollTop: () => 20, + getViewportHeight: () => 20 + } + + const snap = getScrollbarSnapshot(handle as any) + + expect(snap).toEqual({ + scrollHeight: 40, + top: 20, + viewportHeight: 20 + }) + expect(scrollbarSnapshotKey(snap)).toBe('20:20:40') + }) }) diff --git a/ui-tui/src/lib/viewportStore.ts b/ui-tui/src/lib/viewportStore.ts index 25acbd8beb..ab5dcf3e18 100644 --- a/ui-tui/src/lib/viewportStore.ts +++ b/ui-tui/src/lib/viewportStore.ts @@ -70,12 +70,24 @@ export function getScrollbarSnapshot(s?: ScrollBoxHandle | null): ScrollbarSnaps } const viewportHeight = Math.max(0, s.getViewportHeight()) - const scrollHeight = Math.max(viewportHeight, s.getScrollHeight()) - const maxTop = Math.max(0, scrollHeight - viewportHeight) + const top = Math.max(0, s.getScrollTop()) + const cachedScrollHeight = Math.max(viewportHeight, s.getScrollHeight()) + let scrollHeight = cachedScrollHeight + let maxTop = Math.max(0, scrollHeight - viewportHeight) + + if (top < maxTop) { + const freshScrollHeight = Math.max(viewportHeight, s.getFreshScrollHeight?.() ?? cachedScrollHeight) + const freshMaxTop = Math.max(0, freshScrollHeight - viewportHeight) + + if (top >= freshMaxTop) { + scrollHeight = freshScrollHeight + maxTop = freshMaxTop + } + } return { scrollHeight, - top: Math.max(0, Math.min(maxTop, s.getScrollTop())), + top: Math.max(0, Math.min(maxTop, top)), viewportHeight } }