Skip to content

Commit 4bc6597

Browse files
Agi-Asivincentkoc
andauthored
fix(ui): Control UI smooth scrolling ignores the reduced-motion preference (#130428)
* fix(ui): respect prefers-reduced-motion in imperative scrolling Six Control UI actions passed behavior: "smooth" directly to scrollIntoView, bypassing the reduced-motion preference because explicit ScrollOptions are not overridden by the global CSS rule. Route all six call sites through a shared resolveScrollBehavior() helper that returns "auto" under prefers-reduced-motion: reduce and keeps the requested behavior otherwise, mirroring the guards already used by chat/scroll.ts and panels-tools-skills.ts. Closes #127335 * fix(ui): centralize imperative scroll motion policy --------- Co-authored-by: Agi-Asi <[email protected]> Co-authored-by: Vincent Koc <[email protected]>
1 parent 1d568b1 commit 4bc6597

12 files changed

Lines changed: 107 additions & 20 deletions

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { afterEach, describe, expect, it, vi } from "vitest";
2+
import { resolveScrollBehavior } from "./scroll-behavior.ts";
3+
4+
function stubMatchMedia(reduced: boolean) {
5+
const matchMedia = vi.fn().mockReturnValue({ matches: reduced });
6+
vi.stubGlobal("matchMedia", matchMedia);
7+
return matchMedia;
8+
}
9+
10+
afterEach(() => {
11+
vi.unstubAllGlobals();
12+
});
13+
14+
describe("resolveScrollBehavior", () => {
15+
it("uses auto for smooth scrolling under reduced motion", () => {
16+
stubMatchMedia(true);
17+
expect(resolveScrollBehavior()).toBe("auto");
18+
});
19+
20+
it("keeps smooth scrolling without reduced motion", () => {
21+
const matchMedia = stubMatchMedia(false);
22+
expect(resolveScrollBehavior("smooth")).toBe("smooth");
23+
expect(matchMedia).toHaveBeenCalledWith("(prefers-reduced-motion: reduce)");
24+
});
25+
26+
it("keeps smooth scrolling when matchMedia is unavailable", () => {
27+
vi.stubGlobal("matchMedia", undefined);
28+
expect(resolveScrollBehavior("smooth")).toBe("smooth");
29+
});
30+
31+
it.each(["auto", "instant"] as const)("preserves %s without querying motion", (behavior) => {
32+
const matchMedia = stubMatchMedia(true);
33+
expect(resolveScrollBehavior(behavior)).toBe(behavior);
34+
expect(matchMedia).not.toHaveBeenCalled();
35+
});
36+
});

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function resolveScrollBehavior(behavior: ScrollBehavior = "smooth"): ScrollBehavior {
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;
8+
}

‎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/channels/channels-page.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { t } from "../../i18n/index.ts";
1717
import { resolveChannelPairingAuthSignature } from "../../lib/channels/index.ts";
1818
import { formatUiError, formatUiExternalText } from "../../lib/format-error.ts";
1919
import type { GatewayConnectionScope } from "../../lib/gateway-connection-lifecycle.ts";
20+
import { resolveScrollBehavior } from "../../lib/scroll-behavior.ts";
2021
import {
2122
GatewayPageController,
2223
type GatewayPageChange,
@@ -567,7 +568,7 @@ class ChannelsPage extends OpenClawLightDomElement {
567568
this.setPairingFilter(channel, accountId);
568569
void this.updateComplete.then(() => {
569570
this.renderRoot.querySelector("#channels-pairing-requests")?.scrollIntoView({
570-
behavior: "smooth",
571+
behavior: resolveScrollBehavior(),
571572
block: "start",
572573
});
573574
});

‎ui/src/pages/chat/components/chat-transcript-controller.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { ref } from "lit/directives/ref.js";
1515
import { repeat } from "lit/directives/repeat.js";
1616
import { styleMap } from "lit/directives/style-map.js";
1717
import { McpAppUnmountGate } from "../../../components/mcp-app-unmount.ts";
18+
import { resolveScrollBehavior } from "../../../lib/scroll-behavior.ts";
1819
import { areUiSessionKeysEquivalent } from "../../../lib/sessions/session-key.ts";
1920
import {
2021
CHAT_TRANSCRIPT_END_THRESHOLD_PX,
@@ -491,7 +492,7 @@ class ChatSessionVirtualizerHost implements ReactiveControllerHost, ChatTranscri
491492
this.threadInnerElement
492493
?.querySelector(".chat-bubble--reply-target")
493494
?.classList.remove("chat-bubble--reply-target");
494-
bubble.scrollIntoView?.({ behavior: "smooth", block: "center" });
495+
bubble.scrollIntoView?.({ behavior: resolveScrollBehavior(), block: "center" });
495496
bubble.classList.add("chat-bubble--reply-target");
496497
bubble.addEventListener(
497498
"animationend",

‎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
}

‎ui/src/pages/config/config-page.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import { formatUiError } from "../../lib/format-error.ts";
5252
import { isMissingOperatorReadScopeError } from "../../lib/gateway-errors.ts";
5353
import { canCallGatewayMethod } from "../../lib/gateway-methods.ts";
5454
import { loadModels } from "../../lib/model-catalog-store.ts";
55+
import { resolveScrollBehavior } from "../../lib/scroll-behavior.ts";
5556
import { OpenClawLightDomElement } from "../../lit/openclaw-element.ts";
5657
import { PollController } from "../../lit/poll-controller.ts";
5758
import { SubscriptionsController } from "../../lit/subscriptions-controller.ts";
@@ -600,7 +601,7 @@ export class ConfigPage extends OpenClawLightDomElement {
600601
if (!target) {
601602
return;
602603
}
603-
target.scrollIntoView?.({ behavior: "smooth", block: "start" });
604+
target.scrollIntoView?.({ behavior: resolveScrollBehavior(), block: "start" });
604605
this.pendingRouteTargetId = null;
605606
}
606607

‎ui/src/pages/config/view-appearance.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
renderSettingsValue,
1818
} from "../../components/settings-ui.ts";
1919
import { t } from "../../i18n/index.ts";
20+
import { resolveScrollBehavior } from "../../lib/scroll-behavior.ts";
2021
import { APPEARANCE_SETTINGS_TARGET_IDS } from "./route-data.ts";
2122
import {
2223
renderChatPreferencesSection,
@@ -132,7 +133,7 @@ function focusCustomThemeImportInput() {
132133
return;
133134
}
134135
if (typeof input.scrollIntoView === "function") {
135-
input.scrollIntoView({ block: "center", behavior: "smooth" });
136+
input.scrollIntoView({ block: "center", behavior: resolveScrollBehavior() });
136137
}
137138
input.focus();
138139
input.select();

0 commit comments

Comments
 (0)