Skip to content

Commit 4c417d0

Browse files
fix(web): programmatic scrolls jump instead of glide when Reduce Motion is on
Scroll to end, PageUp/PageDown from the composer, timeline minimap jumps and the theme editor's color reveal now move instantly when the system asks for reduced motion. The preference is read at the moment of each action, so changing it applies without a reload. Normal motion is unchanged. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent a1d9d72 commit 4c417d0

5 files changed

Lines changed: 79 additions & 5 deletions

File tree

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6240,7 +6240,9 @@ export default function ChatView(props: ChatViewProps) {
62406240
// The anchored end space must be gone before the scroll measures, or the
62416241
// list lands short of the real end (#6519).
62426242
requestAnimationFrame(() => {
6243-
void legendListRef.current?.scrollToEnd?.({ animated });
6243+
void legendListRef.current?.scrollToEnd?.({
6244+
animated: animated && !window.matchMedia("(prefers-reduced-motion: reduce)").matches,
6245+
});
62446246
});
62456247
}, []);
62466248
useEffect(() => {

‎apps/web/src/components/chat/MessagesTimeline.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1389,7 +1389,7 @@ export const MessagesTimeline = memo(function MessagesTimeline({
13891389
onManualNavigation();
13901390
void listRef.current?.scrollToIndex({
13911391
index: item.rowIndex,
1392-
animated: true,
1392+
animated: !prefersReducedMotion,
13931393
viewOffset: 24,
13941394
});
13951395
}}

‎apps/web/src/components/chat/pageScrollController.test.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class TestClock {
1616
private nextHandle = 1;
1717
private animationFrames = new Map<number, FrameRequestCallback>();
1818
private timeouts = new Map<number, { at: number; callback: () => void }>();
19+
reducedMotion = false;
1920

2021
readonly env = {
2122
now: () => this.currentTime,
@@ -37,6 +38,7 @@ class TestClock {
3738
clearTimeout: (handle: number) => {
3839
this.timeouts.delete(handle);
3940
},
41+
prefersReducedMotion: () => this.reducedMotion,
4042
};
4143

4244
advanceBy(ms: number, frameMs = 16) {
@@ -201,6 +203,67 @@ describe("createPageScrollController", () => {
201203
);
202204
});
203205

206+
test("moves a full page without animation frames when motion is reduced", () => {
207+
const clock = new TestClock();
208+
clock.reducedMotion = true;
209+
const container = {
210+
clientHeight: 600,
211+
scrollHeight: 1_800,
212+
scrollTop: 0,
213+
getBoundingClientRect: () => ({ height: 600 }),
214+
};
215+
const controller = createPageScrollController({
216+
getContainer: () => container,
217+
getScrollPaddingBottomPx: () => 24,
218+
env: clock.env,
219+
});
220+
const pageDistance = getPageScrollDistancePx({
221+
containerHeightPx: 600,
222+
scrollPaddingBottomPx: 24,
223+
});
224+
225+
controller.handleKeyDown("PageDown");
226+
227+
expect(container.scrollTop).toBe(pageDistance);
228+
229+
controller.handleKeyUp("PageDown");
230+
clock.advanceBy(PAGE_SCROLL_ANIMATION_MS);
231+
232+
expect(container.scrollTop).toBe(pageDistance);
233+
});
234+
235+
test("reads the motion preference on each press", () => {
236+
const clock = new TestClock();
237+
const container = {
238+
clientHeight: 600,
239+
scrollHeight: 4_000,
240+
scrollTop: 0,
241+
getBoundingClientRect: () => ({ height: 600 }),
242+
};
243+
const controller = createPageScrollController({
244+
getContainer: () => container,
245+
getScrollPaddingBottomPx: () => 24,
246+
env: clock.env,
247+
});
248+
const pageDistance = getPageScrollDistancePx({
249+
containerHeightPx: 600,
250+
scrollPaddingBottomPx: 24,
251+
});
252+
253+
controller.handleKeyDown("PageDown");
254+
controller.handleKeyUp("PageDown");
255+
clock.advanceBy(16);
256+
257+
expect(container.scrollTop).toBeGreaterThan(0);
258+
expect(container.scrollTop).toBeLessThan(pageDistance);
259+
260+
clock.advanceBy(PAGE_SCROLL_ANIMATION_MS);
261+
clock.reducedMotion = true;
262+
controller.handleKeyDown("PageDown");
263+
264+
expect(container.scrollTop).toBeCloseTo(pageDistance * 2, 5);
265+
});
266+
204267
test("continues scrolling on hold without repeated keydown events and stops on keyup", () => {
205268
const clock = new TestClock();
206269
const container = {

‎apps/web/src/components/chat/pageScrollController.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ type PageScrollEnv = {
8181
cancelAnimationFrame: (handle: number) => void;
8282
setTimeout: (callback: () => void, delay: number) => number;
8383
clearTimeout: (handle: number) => void;
84+
prefersReducedMotion: () => boolean;
8485
};
8586

8687
function getDefaultEnv(): PageScrollEnv {
@@ -90,6 +91,7 @@ function getDefaultEnv(): PageScrollEnv {
9091
cancelAnimationFrame: (handle) => window.cancelAnimationFrame(handle),
9192
setTimeout: (callback, delay) => window.setTimeout(callback, delay),
9293
clearTimeout: (handle) => window.clearTimeout(handle),
94+
prefersReducedMotion: () => window.matchMedia("(prefers-reduced-motion: reduce)").matches,
9395
};
9496
}
9597

@@ -209,6 +211,12 @@ export function createPageScrollController({
209211
const smoothScrollBy = (container: PageScrollContainer, deltaY: number) => {
210212
cancelDiscreteAnimation();
211213

214+
// Read per press so a preference change applies without a reload.
215+
if (env.prefersReducedMotion()) {
216+
container.scrollTop += deltaY;
217+
return;
218+
}
219+
212220
const startScrollTop = container.scrollTop;
213221
const startTime = env.now();
214222

‎apps/web/src/components/settings/ThemeEditorPanel.tsx‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -513,9 +513,10 @@ export function ThemeEditorPanel({
513513
if (!reveal) return;
514514

515515
requestAnimationFrame(() => {
516-
panelRef.current
517-
?.querySelector(`[data-theme-color-role="${visibleRole}"]`)
518-
?.scrollIntoView({ behavior: "smooth", block: "nearest" });
516+
panelRef.current?.querySelector(`[data-theme-color-role="${visibleRole}"]`)?.scrollIntoView({
517+
behavior: window.matchMedia("(prefers-reduced-motion: reduce)").matches ? "auto" : "smooth",
518+
block: "nearest",
519+
});
519520
});
520521
}, []);
521522

0 commit comments

Comments
 (0)