From 392574fe2a2a4a5d4f22e7a43ef6266604dc6548 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Sun, 22 Nov 2026 03:40:25 +0800 Subject: [PATCH] fix(ui): don't show a scrollbar over content that fits after a resize (#5016) A scroll range of one pixel or less is WebKit rounding after a resize (a splitter drag leaves scrollHeight 319 over clientHeight 318), not content: the overlay scrollbar and the editor scrollbar thumb no longer show for it. --- src/editor/core/cm/ideaParity.test.ts | 2 ++ src/editor/core/cm/lanes.ts | 3 ++- src/ui/Scrollbar.test.tsx | 21 +++++++++++++++++++++ src/ui/Scrollbar.tsx | 7 +++++-- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/editor/core/cm/ideaParity.test.ts b/src/editor/core/cm/ideaParity.test.ts index e998fcbb0..82b17faa4 100644 --- a/src/editor/core/cm/ideaParity.test.ts +++ b/src/editor/core/cm/ideaParity.test.ts @@ -308,6 +308,8 @@ describe("error stripe", () => { it("finds the scrollbar thumb the marks must let through", () => { // Nothing to scroll: no thumb. expect(scrollbarThumb(0, 400, 400)).toBeNull(); + expect(scrollbarThumb(0, 401, 400)).toBeNull(); + expect(scrollbarThumb(0, 402, 400)).not.toBeNull(); // A quarter of the document visible: a 100px thumb, at the top, then at the bottom. expect(scrollbarThumb(0, 1600, 400)).toEqual({ top: 0, bottom: 100 }); expect(scrollbarThumb(1200, 1600, 400)).toEqual({ top: 300, bottom: 400 }); diff --git a/src/editor/core/cm/lanes.ts b/src/editor/core/cm/lanes.ts index 94ed977c9..8a265f96b 100644 --- a/src/editor/core/cm/lanes.ts +++ b/src/editor/core/cm/lanes.ts @@ -42,7 +42,8 @@ export function scrollbarThumb( minThumb = MIN_THUMB, ): { top: number; bottom: number } | null { const max = scrollHeight - clientHeight; - if (max <= 0 || clientHeight <= 0) return null; + // A pixel of range is WebKit rounding after a resize, not content to scroll to. + if (max <= 1 || clientHeight <= 0) return null; const length = Math.min(clientHeight, Math.max(minThumb, (clientHeight * clientHeight) / scrollHeight)); const top = (Math.min(Math.max(scrollTop, 0), max) / max) * (clientHeight - length); return { top, bottom: top + length }; diff --git a/src/ui/Scrollbar.test.tsx b/src/ui/Scrollbar.test.tsx index 40a078730..83f665942 100644 --- a/src/ui/Scrollbar.test.tsx +++ b/src/ui/Scrollbar.test.tsx @@ -43,4 +43,25 @@ describe("Scrollbar", () => { expect(onScroll).toHaveBeenCalledTimes(1); expect(onScroll).toHaveBeenLastCalledWith(top); }); + + it.each([ + [318, 318, false], + // WebKit after a splitter drag: one commit row, scrollHeight a pixel over the viewport. + [318, 319, false], + [318, 320, true], + [100, 1000, true], + ])("shows the track for clientHeight %i and scrollHeight %i: %s", (clientHeight, scrollHeight, shown) => { + const { getByTestId, container } = render( {}} />); + const scroller = getByTestId("scroller"); + Object.defineProperties(scroller, { + clientHeight: { value: clientHeight }, + scrollHeight: { value: scrollHeight }, + scrollTop: { value: 0 }, + }); + act(() => { + scroller.dispatchEvent(new Event("scroll")); + }); + const track = container.querySelector("[aria-hidden]")!; + expect(track.style.display !== "none").toBe(shown); + }); }); diff --git a/src/ui/Scrollbar.tsx b/src/ui/Scrollbar.tsx index dd705a62f..fbe2b075b 100644 --- a/src/ui/Scrollbar.tsx +++ b/src/ui/Scrollbar.tsx @@ -88,10 +88,13 @@ function layout(el: HTMLElement | null, t: HTMLElement | null, th: HTMLElement | if (!el || !t || !th) return null; const height = el.clientHeight; const max = el.scrollHeight - height; + // WebKit can report scrollHeight a pixel over clientHeight after the scroller is resized to a + // fractional height (dragging a splitter), with nothing overflowing: that is no scroll range. + const scrolls = max > 1; t.style.top = `${el.offsetTop}px`; t.style.height = `${height}px`; - t.style.display = max > 0 ? "" : "none"; - if (max <= 0) return null; + t.style.display = scrolls ? "" : "none"; + if (!scrolls) return null; const size = Math.max(MIN_THUMB, (height * height) / el.scrollHeight); const range = height - size; th.style.height = `${size}px`;