Skip to content

Commit cf53005

Browse files
saphidgithub-actions[bot]claudejuliusmarminge
authored
fix(web): programmatic scrolls jump instead of glide when Reduce Motion is on (pingdotgg#16014)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]> Co-authored-by: SAPHID <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent cd8c2c5 commit cf53005

5 files changed

Lines changed: 74 additions & 20 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4192,7 +4192,9 @@ export default function ChatView(props: ChatViewProps) {
41924192
// The anchored end space must be gone before the scroll measures, or the
41934193
// list lands short of the real end (#6519).
41944194
requestAnimationFrame(() => {
4195-
void legendListRef.current?.scrollToEnd?.({ animated });
4195+
void legendListRef.current?.scrollToEnd?.({
4196+
animated: animated && !window.matchMedia("(prefers-reduced-motion: reduce)").matches,
4197+
});
41964198
});
41974199
};
41984200
const scrollToEndStable = useStableCallback(scrollToEnd);
@@ -4397,7 +4399,7 @@ export default function ChatView(props: ChatViewProps) {
43974399
scrollNode.addEventListener("scrollend", finishAnimatedPositioning, { once: true });
43984400
void list.scrollToIndex({
43994401
index: anchorIndex,
4400-
animated: true,
4402+
animated: !window.matchMedia("(prefers-reduced-motion: reduce)").matches,
44014403
viewPosition: 0,
44024404
viewOffset: CHAT_LIST_ANCHOR_OFFSET,
44034405
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1545,7 +1545,7 @@ const ConversationTimeline = memo(function ConversationTimeline({
15451545
onManualNavigation();
15461546
void listRef.current?.scrollToIndex({
15471547
index: item.rowIndex,
1548-
animated: true,
1548+
animated: !window.matchMedia("(prefers-reduced-motion: reduce)").matches,
15491549
viewOffset: 24,
15501550
});
15511551
}}

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

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class TestClock {
1717
private animationFrames = new Map<number, FrameRequestCallback>();
1818
private timeouts = new Map<number, { at: number; callback: () => void }>();
1919

20+
reducedMotion = false;
21+
2022
readonly env = {
2123
now: () => this.currentTime,
2224
requestAnimationFrame: (callback: FrameRequestCallback) => {
@@ -37,6 +39,7 @@ class TestClock {
3739
clearTimeout: (handle: number) => {
3840
this.timeouts.delete(handle);
3941
},
42+
prefersReducedMotion: () => this.reducedMotion,
4043
};
4144

4245
advanceBy(ms: number, frameMs = 16) {
@@ -201,33 +204,74 @@ describe("createPageScrollController", () => {
201204
);
202205
});
203206

204-
test("continues scrolling on hold without repeated keydown events and stops on keyup", () => {
207+
test("uses the current motion preference for page taps in both directions", () => {
205208
const clock = new TestClock();
206209
const container = {
207210
clientHeight: 600,
208211
scrollHeight: 4_000,
209-
scrollTop: 0,
212+
scrollTop: 1_200,
210213
getBoundingClientRect: () => ({ height: 600 }),
211214
};
212215
const controller = createPageScrollController({
213216
getContainer: () => container,
214217
getScrollPaddingBottomPx: () => 24,
215218
env: clock.env,
216219
});
217-
controller.handleKeyDown("PageDown");
218-
clock.advanceBy(PAGE_SCROLL_ANIMATION_MS + 50);
219220

220-
const afterHoldStarts = container.scrollTop;
221-
clock.advanceBy(200);
221+
for (const reducedMotion of [false, true, false]) {
222+
clock.reducedMotion = reducedMotion;
223+
for (const [key, destination] of [
224+
["PageDown", 1_740],
225+
["PageUp", 1_200],
226+
] as const) {
227+
const start = container.scrollTop;
228+
controller.handleKeyDown(key);
229+
controller.handleKeyUp(key);
230+
231+
if (!reducedMotion) {
232+
clock.advanceBy(16);
233+
expect(container.scrollTop).toBeGreaterThan(Math.min(start, destination));
234+
expect(container.scrollTop).toBeLessThan(Math.max(start, destination));
235+
clock.advanceBy(PAGE_SCROLL_ANIMATION_MS);
236+
}
237+
expect(container.scrollTop).toBeCloseTo(destination, 5);
238+
clock.advanceBy(PAGE_SCROLL_ANIMATION_MS * 2);
239+
expect(container.scrollTop).toBeCloseTo(destination, 5);
240+
}
241+
}
242+
});
243+
244+
test.each([false, true])(
245+
"continues scrolling on hold without repeated keydown events and stops on keyup (reduced motion: %s)",
246+
(reducedMotion) => {
247+
const clock = new TestClock();
248+
clock.reducedMotion = reducedMotion;
249+
const container = {
250+
clientHeight: 600,
251+
scrollHeight: 4_000,
252+
scrollTop: 0,
253+
getBoundingClientRect: () => ({ height: 600 }),
254+
};
255+
const controller = createPageScrollController({
256+
getContainer: () => container,
257+
getScrollPaddingBottomPx: () => 24,
258+
env: clock.env,
259+
});
260+
controller.handleKeyDown("PageDown");
261+
clock.advanceBy(PAGE_SCROLL_ANIMATION_MS + 50);
222262

223-
expect(container.scrollTop).toBeGreaterThan(afterHoldStarts);
263+
const afterHoldStarts = container.scrollTop;
264+
clock.advanceBy(200);
224265

225-
const stoppedAt = container.scrollTop;
226-
controller.handleKeyUp("PageDown");
227-
clock.advanceBy(250);
266+
expect(container.scrollTop).toBeGreaterThan(afterHoldStarts);
228267

229-
expect(container.scrollTop).toBe(stoppedAt);
230-
});
268+
const stoppedAt = container.scrollTop;
269+
controller.handleKeyUp("PageDown");
270+
clock.advanceBy(250);
271+
272+
expect(container.scrollTop).toBe(stoppedAt);
273+
},
274+
);
231275

232276
test("notifies once when a page scroll starts", () => {
233277
const clock = new TestClock();

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

Lines changed: 9 additions & 2 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

@@ -206,9 +208,14 @@ export function createPageScrollController({
206208
state.holdActive = false;
207209
};
208210

209-
const smoothScrollBy = (container: PageScrollContainer, deltaY: number) => {
211+
const scrollBy = (container: PageScrollContainer, deltaY: number) => {
210212
cancelDiscreteAnimation();
211213

214+
if (env.prefersReducedMotion()) {
215+
container.scrollTop += deltaY;
216+
return;
217+
}
218+
212219
const startScrollTop = container.scrollTop;
213220
const startTime = env.now();
214221

@@ -279,7 +286,7 @@ export function createPageScrollController({
279286
state.activeKey = key;
280287
onScrollStart?.(key);
281288

282-
smoothScrollBy(container, readPageScrollDistance(container) * getDirection(key));
289+
scrollBy(container, readPageScrollDistance(container) * getDirection(key));
283290

284291
state.holdDelayTimeout = env.setTimeout(() => {
285292
state.holdDelayTimeout = 0;

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

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

506506
requestAnimationFrame(() => {
507-
panelRef.current
508-
?.querySelector(`[data-theme-color-role="${visibleRole}"]`)
509-
?.scrollIntoView({ behavior: "smooth", block: "nearest" });
507+
panelRef.current?.querySelector(`[data-theme-color-role="${visibleRole}"]`)?.scrollIntoView({
508+
behavior: window.matchMedia("(prefers-reduced-motion: reduce)").matches ? "auto" : "smooth",
509+
block: "nearest",
510+
});
510511
});
511512
};
512513

0 commit comments

Comments
 (0)