Skip to content

The open-source app everyone uses to manage agents at work

fix(ui): keep desktop shell pinned when scrollIntoView walks past body (#8071)

Devin Foley412a04c96491
diff --git a/ui/src/components/Layout.tsx b/ui/src/components/Layout.tsx
index 18fe7cc31a..a03400b1a8 100644
--- a/ui/src/components/Layout.tsx
+++ b/ui/src/components/Layout.tsx
@@ -36,6 +36,7 @@ import {
} from "../lib/navigation-scroll";
import { queryKeys } from "../lib/queryKeys";
import { scheduleMainContentFocus } from "../lib/main-content-focus";
+import { pinDocumentScrollToZero } from "../lib/pin-document-scroll";
import { cn } from "../lib/utils";
import { NotFoundPage } from "../pages/NotFound";
import { PluginSlotMount, resolveRouteSidebarSlot, usePluginSlots } from "../plugins/slots";
@@ -424,13 +425,24 @@ export function Layout() {
useEffect(() => {
const previousOverflow = document.body.style.overflow;
- document.body.style.overflow = isMobile ? "visible" : "hidden";
+ document.body.style.overflow = isMobile ? "visible" : "clip";
return () => {
document.body.style.overflow = previousOverflow;
};
}, [isMobile]);
+ // `scrollIntoView` walks every ancestor scroll container. On a long thread
+ // the post-submit `scrollIntoView` on the new comment reaches `<html>` and
+ // animates `documentElement.scrollTop` via the browser's internal scroll
+ // algorithm, which bypasses the CSS `overflow` on the root element and
+ // visually shifts the entire shell (sidebar included) off-screen. Pin
+ // both roots to scrollTop=0 on every scroll tick.
+ useEffect(() => {
+ if (isMobile) return;
+ return pinDocumentScrollToZero();
+ }, [isMobile]);
+
useEffect(() => {
if (typeof document === "undefined") return;
const mainContent = mainContentRef.current;
@@ -456,7 +468,7 @@ export function Layout() {
<div
className={cn(
"bg-background text-foreground pt-[env(safe-area-inset-top)]",
- isMobile ? "min-h-dvh" : "flex h-dvh flex-col overflow-hidden",
+ isMobile ? "min-h-dvh" : "flex h-dvh flex-col overflow-clip",
)}
>
<a
@@ -467,7 +479,7 @@ export function Layout() {
</a>
<WorktreeBanner />
<DevRestartBanner devServer={health?.devServer} />
- <div className={cn("min-h-0 flex-1", isMobile ? "w-full" : "flex overflow-hidden")}>
+ <div className={cn("min-h-0 flex-1", isMobile ? "w-full" : "flex overflow-clip")}>
{isMobile && sidebarOpen && (
<button
type="button"
diff --git a/ui/src/lib/pin-document-scroll.test.ts b/ui/src/lib/pin-document-scroll.test.ts
new file mode 100644
index 0000000000..1f154fab0c
--- /dev/null
+++ b/ui/src/lib/pin-document-scroll.test.ts
@@ -0,0 +1,76 @@
+// @vitest-environment jsdom
+
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { pinDocumentScrollToZero } from "./pin-document-scroll";
+
+describe("pinDocumentScrollToZero", () => {
+ afterEach(() => {
+ document.documentElement.scrollTop = 0;
+ document.body.scrollTop = 0;
+ });
+
+ it("snaps documentElement.scrollTop back to 0 when a scroll event fires after it drifts", () => {
+ const cleanup = pinDocumentScrollToZero();
+
+ document.documentElement.scrollTop = 250;
+ window.dispatchEvent(new Event("scroll"));
+
+ expect(document.documentElement.scrollTop).toBe(0);
+
+ cleanup();
+ });
+
+ it("snaps body.scrollTop back to 0 too (some engines target body for the root scroll)", () => {
+ const cleanup = pinDocumentScrollToZero();
+
+ document.body.scrollTop = 120;
+ window.dispatchEvent(new Event("scroll"));
+
+ expect(document.body.scrollTop).toBe(0);
+
+ cleanup();
+ });
+
+ it("leaves scrollTop alone when it is already 0 (no thrash on routine scrolls)", () => {
+ const cleanup = pinDocumentScrollToZero();
+
+ const docSetter = vi.spyOn(document.documentElement, "scrollTop", "set");
+ const bodySetter = vi.spyOn(document.body, "scrollTop", "set");
+
+ window.dispatchEvent(new Event("scroll"));
+
+ expect(docSetter).not.toHaveBeenCalled();
+ expect(bodySetter).not.toHaveBeenCalled();
+
+ docSetter.mockRestore();
+ bodySetter.mockRestore();
+ cleanup();
+ });
+
+ it("registers the listener in capture phase and NOT passive so the reset runs before paint", () => {
+ const addSpy = vi.spyOn(window, "addEventListener");
+
+ const cleanup = pinDocumentScrollToZero();
+
+ expect(addSpy).toHaveBeenCalledWith(
+ "scroll",
+ expect.any(Function),
+ // capture: true, no passive flag — a passive listener would let the
+ // browser paint the shifted frame before the reset runs.
+ { capture: true },
+ );
+
+ addSpy.mockRestore();
+ cleanup();
+ });
+
+ it("cleanup removes the listener — subsequent scroll events do not reset scrollTop", () => {
+ const cleanup = pinDocumentScrollToZero();
+ cleanup();
+
+ document.documentElement.scrollTop = 75;
+ window.dispatchEvent(new Event("scroll"));
+
+ expect(document.documentElement.scrollTop).toBe(75);
+ });
+});
diff --git a/ui/src/lib/pin-document-scroll.ts b/ui/src/lib/pin-document-scroll.ts
new file mode 100644
index 0000000000..9b838656ef
--- /dev/null
+++ b/ui/src/lib/pin-document-scroll.ts
@@ -0,0 +1,30 @@
+// Registers a capture-phase `scroll` listener on `win` that snaps the
+// `documentElement.scrollTop` and `body.scrollTop` back to 0 whenever they
+// drift. The desktop app shell expects neither to ever be non-zero — only
+// `#main-content` should scroll — but `scrollIntoView`'s ancestor walk can
+// reach `<html>` (which Chrome drives via its internal smooth-scroll
+// algorithm, bypassing the JS `scrollTop` setter and ignoring CSS
+// `overflow` on the root viewport). Snapping back on every scroll tick
+// keeps the shell visually pinned.
+//
+// Returns a cleanup function that removes the listener.
+export function pinDocumentScrollToZero(
+ doc: Document = document,
+ win: Window = window,
+): () => void {
+ const onScroll = () => {
+ if (doc.documentElement.scrollTop !== 0) {
+ doc.documentElement.scrollTop = 0;
+ }
+ if (doc.body.scrollTop !== 0) {
+ doc.body.scrollTop = 0;
+ }
+ };
+ // Intentionally NOT `passive: true` — a passive listener lets the browser
+ // paint the scrolled frame before this handler runs, producing a one-frame
+ // flash of the shifted shell. The handler doesn't call `preventDefault()`
+ // and `scroll` events aren't cancelable, so dropping the passive flag has
+ // no performance downside and runs the reset synchronously with dispatch.
+ win.addEventListener("scroll", onScroll, { capture: true });
+ return () => win.removeEventListener("scroll", onScroll, { capture: true });
+}