Skip to content

Commit 0b08f08

Browse files
authored
fix(ui): centralize imperative scroll motion policy
1 parent 882bcda commit 0b08f08

6 files changed

Lines changed: 63 additions & 46 deletions

File tree

‎ui/src/lib/scroll-behavior.test.ts‎

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
// Control UI tests cover the shared reduced-motion-aware scroll behavior resolver.
21
import { afterEach, describe, expect, it, vi } from "vitest";
32
import { resolveScrollBehavior } from "./scroll-behavior.ts";
43

5-
// jsdom does not implement window.matchMedia, so each test installs a
6-
// controllable stub and removes it afterwards to keep behavior observable.
7-
function stubMatchMedia(reduced: boolean): ReturnType<typeof vi.fn> {
4+
function stubMatchMedia(reduced: boolean) {
85
const matchMedia = vi.fn().mockReturnValue({ matches: reduced });
96
vi.stubGlobal("matchMedia", matchMedia);
107
return matchMedia;
@@ -15,30 +12,25 @@ afterEach(() => {
1512
});
1613

1714
describe("resolveScrollBehavior", () => {
18-
it("downgrades smooth scrolling to auto under prefers-reduced-motion: reduce", () => {
15+
it("uses auto for smooth scrolling under reduced motion", () => {
1916
stubMatchMedia(true);
2017
expect(resolveScrollBehavior()).toBe("auto");
21-
expect(resolveScrollBehavior("smooth")).toBe("auto");
2218
});
2319

24-
it("keeps smooth scrolling when reduced motion is not requested", () => {
20+
it("keeps smooth scrolling without reduced motion", () => {
2521
const matchMedia = stubMatchMedia(false);
26-
expect(resolveScrollBehavior()).toBe("smooth");
2722
expect(resolveScrollBehavior("smooth")).toBe("smooth");
2823
expect(matchMedia).toHaveBeenCalledWith("(prefers-reduced-motion: reduce)");
2924
});
3025

31-
it("keeps the requested behavior when matchMedia is unavailable", () => {
32-
// Node/SSR-style environments may lack window.matchMedia entirely.
26+
it("keeps smooth scrolling when matchMedia is unavailable", () => {
3327
vi.stubGlobal("matchMedia", undefined);
34-
expect(resolveScrollBehavior()).toBe("smooth");
3528
expect(resolveScrollBehavior("smooth")).toBe("smooth");
3629
});
3730

38-
it("preserves non-animated behaviors without consulting the media query", () => {
31+
it.each(["auto", "instant"] as const)("preserves %s without querying motion", (behavior) => {
3932
const matchMedia = stubMatchMedia(true);
40-
expect(resolveScrollBehavior("auto")).toBe("auto");
41-
expect(resolveScrollBehavior("instant")).toBe("instant");
33+
expect(resolveScrollBehavior(behavior)).toBe(behavior);
4234
expect(matchMedia).not.toHaveBeenCalled();
4335
});
4436
});

‎ui/src/lib/scroll-behavior.ts‎

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
1-
// Shared imperative-scroll behavior resolver for the Control UI.
2-
// The global `prefers-reduced-motion` CSS rule cannot override `ScrollOptions`
3-
// passed directly to `scrollIntoView`/`scrollTo`, so every imperative smooth
4-
// scroll must resolve its behavior through this helper to honor the user's
5-
// reduced-motion preference.
6-
7-
/**
8-
* Resolves the requested `ScrollBehavior`, downgrading it to `auto` when the
9-
* user prefers reduced motion. Non-animated behaviors pass through unchanged,
10-
* and environments without `window.matchMedia` (SSR/tests) keep the request.
11-
*/
121
export function resolveScrollBehavior(behavior: ScrollBehavior = "smooth"): ScrollBehavior {
13-
if (behavior === "auto" || behavior === "instant") {
14-
return behavior;
15-
}
16-
if (typeof window === "undefined" || typeof window.matchMedia !== "function") {
17-
return behavior;
18-
}
19-
return window.matchMedia("(prefers-reduced-motion: reduce)").matches ? "auto" : behavior;
2+
return behavior === "smooth" &&
3+
typeof window !== "undefined" &&
4+
typeof window.matchMedia === "function" &&
5+
window.matchMedia("(prefers-reduced-motion: reduce)").matches
6+
? "auto"
7+
: behavior;
208
}

‎ui/src/pages/agents/panels-tools-skills.browser.test.ts‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,10 @@ describe("agents tools panel (browser)", () => {
564564
]);
565565
});
566566

567-
it("opens the collapsed group and tool row from a live tool chip", async () => {
567+
it.each([
568+
{ reduced: true, behavior: "auto" },
569+
{ reduced: false, behavior: "smooth" },
570+
] as const)("opens a live tool chip with $behavior scrolling", async ({ reduced, behavior }) => {
568571
const container = document.createElement("div");
569572
document.body.append(container);
570573
render(
@@ -636,9 +639,16 @@ describe("agents tools panel (browser)", () => {
636639
expect(group.open).toBe(false);
637640
expect(tool.open).toBe(false);
638641

642+
const summary = tool.querySelector<HTMLElement>("summary");
643+
if (!summary) {
644+
container.remove();
645+
throw new Error("expected agent tool summary");
646+
}
647+
const scrollIntoView = vi.fn();
648+
tool.scrollIntoView = scrollIntoView;
649+
const focus = vi.spyOn(summary, "focus");
650+
vi.stubGlobal("matchMedia", vi.fn().mockReturnValue({ matches: reduced }));
639651
const previousUrl = window.location.href;
640-
// Shared jsdom workers can observe URL changes before finally/afterEach,
641-
// so inspect the intended deep link without mutating browser history.
642652
const replaceState = vi.spyOn(window.history, "replaceState").mockImplementation(() => {});
643653
try {
644654
chip.click();
@@ -653,7 +663,11 @@ describe("agents tools panel (browser)", () => {
653663
expect(requestedUrl).toBeInstanceOf(URL);
654664
expect((requestedUrl as URL).hash).toBe("#agent-tool-read");
655665
expect(window.location.href).toBe(previousUrl);
666+
expect(scrollIntoView).toHaveBeenCalledWith({ block: "center", behavior });
667+
expect(focus).toHaveBeenCalledOnce();
656668
} finally {
669+
vi.unstubAllGlobals();
670+
focus.mockRestore();
657671
replaceState.mockRestore();
658672
container.remove();
659673
}

‎ui/src/pages/agents/panels-tools-skills.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
resolveToolSections,
3030
} from "../../lib/agents/display.ts";
3131
import { formatUiExternalText } from "../../lib/format-error.ts";
32+
import { resolveScrollBehavior } from "../../lib/scroll-behavior.ts";
3233
import type { SkillGroup } from "../../lib/skills-grouping.ts";
3334
import { groupSkills } from "../../lib/skills-grouping.ts";
3435
import {
@@ -172,12 +173,9 @@ function handleRuntimeToolJump(event: Event, anchorId: string) {
172173
window.history.replaceState(null, "", nextUrl);
173174

174175
requestAnimationFrame(() => {
175-
const reducedMotion =
176-
typeof window.matchMedia === "function" &&
177-
window.matchMedia("(prefers-reduced-motion: reduce)").matches;
178176
target.scrollIntoView?.({
179177
block: "center",
180-
behavior: reducedMotion ? "auto" : "smooth",
178+
behavior: resolveScrollBehavior(),
181179
});
182180
target.querySelector<HTMLElement>("summary")?.focus();
183181
});

‎ui/src/pages/chat/scroll.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,7 @@ describe("programmatic scroll guard", () => {
619619
afterEach(() => {
620620
vi.useRealTimers();
621621
vi.restoreAllMocks();
622+
vi.unstubAllGlobals();
622623
});
623624

624625
it("handleChatScroll suppresses own scroll event when scrollTop is at the programmatic target", () => {
@@ -730,6 +731,32 @@ describe("programmatic scroll guard", () => {
730731
expect(host.chatNewMessagesBelow).toBe(false);
731732
});
732733

734+
it("uses auto under reduced motion without extending the scroll guard", async () => {
735+
const frames = installAnimationFrameQueue();
736+
const { host, container } = createScrollHost({
737+
scrollHeight: 2000,
738+
scrollTop: 500,
739+
clientHeight: 400,
740+
});
741+
const scrollTo = vi.fn();
742+
(container as unknown as HTMLElement).scrollTo = scrollTo;
743+
vi.stubGlobal("matchMedia", vi.fn().mockReturnValue({ matches: true }));
744+
host.chatHasAutoScrolled = true;
745+
host.chatUserNearBottom = false;
746+
747+
scheduleChatScroll(host, true, true, { source: "manual" });
748+
await host.updateComplete;
749+
frames.runNext();
750+
751+
expect(scrollTo).toHaveBeenCalledWith({ top: 2000, behavior: "auto" });
752+
expect(host.chatIsProgrammaticScroll).toBe(true);
753+
expect(frames.callbacks).toHaveLength(1);
754+
755+
frames.runNext(16);
756+
expect(host.chatIsProgrammaticScroll).toBe(false);
757+
expect(frames.callbacks).toHaveLength(0);
758+
});
759+
733760
it("stops a smooth manual scroll after the user scrolls up", async () => {
734761
const frames = installAnimationFrameQueue();
735762
const { host, container } = createScrollHost({

‎ui/src/pages/chat/scroll.ts‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Control UI module implements app scroll behavior.
2+
import { resolveScrollBehavior } from "../../lib/scroll-behavior.ts";
23
import { areUiSessionKeysEquivalent } from "../../lib/sessions/session-key.ts";
34
import type { RenderLifecycle } from "./render-lifecycle.ts";
45
import { getSessionCacheValue, setSessionCacheValue } from "./session-cache.ts";
@@ -215,18 +216,15 @@ export function scheduleCommittedChatScroll(
215216
host.chatHasAutoScrolled = true;
216217
}
217218
host.chatFollowLocked = false;
218-
const smoothEnabled =
219-
smooth &&
220-
(typeof window === "undefined" ||
221-
typeof window.matchMedia !== "function" ||
222-
!window.matchMedia("(prefers-reduced-motion: reduce)").matches);
219+
const behavior = resolveScrollBehavior(smooth ? "smooth" : "auto");
220+
const smoothEnabled = behavior === "smooth";
223221
const scrollTop = target.scrollHeight;
224222
host.chatProgrammaticScrollTarget = scrollTop;
225223
host.chatIsProgrammaticScroll = true;
226224
if (host.chatScrollToEnd) {
227-
host.chatScrollToEnd({ behavior: smoothEnabled ? "smooth" : "auto" });
225+
host.chatScrollToEnd({ behavior });
228226
} else if (typeof target.scrollTo === "function") {
229-
target.scrollTo({ top: scrollTop, behavior: smoothEnabled ? "smooth" : "auto" });
227+
target.scrollTo({ top: scrollTop, behavior });
230228
} else {
231229
target.scrollTop = scrollTop;
232230
}

0 commit comments

Comments
 (0)