Skip to content

Commit cb9c9ed

Browse files
fix(web): read reduced motion when scrolling
1 parent daba7f2 commit cb9c9ed

4 files changed

Lines changed: 27 additions & 54 deletions

File tree

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4383,8 +4383,6 @@ export default function ChatView(props: ChatViewProps) {
43834383
};
43844384
const fallbackTimer = window.setTimeout(finishAnimatedPositioning, 750);
43854385
scrollNode.addEventListener("scrollend", finishAnimatedPositioning, { once: true });
4386-
// An instant scroll also ends with `scrollend` (or the fallback when nothing moved), so it
4387-
// settles the same way.
43884386
void list.scrollToIndex({
43894387
index: anchorIndex,
43904388
animated: !window.matchMedia("(prefers-reduced-motion: reduce)").matches,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1510,7 +1510,7 @@ const ConversationTimeline = memo(function ConversationTimeline({
15101510
onManualNavigation();
15111511
void listRef.current?.scrollToIndex({
15121512
index: item.rowIndex,
1513-
animated: !prefersReducedMotion,
1513+
animated: !window.matchMedia("(prefers-reduced-motion: reduce)").matches,
15141514
viewOffset: 24,
15151515
});
15161516
}}

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

Lines changed: 24 additions & 48 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+
1920
reducedMotion = false;
2021

2122
readonly env = {
@@ -203,68 +204,43 @@ describe("createPageScrollController", () => {
203204
);
204205
});
205206

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", () => {
207+
test("uses the current motion preference for page taps in both directions", () => {
236208
const clock = new TestClock();
237209
const container = {
238210
clientHeight: 600,
239211
scrollHeight: 4_000,
240-
scrollTop: 0,
212+
scrollTop: 1_200,
241213
getBoundingClientRect: () => ({ height: 600 }),
242214
};
243215
const controller = createPageScrollController({
244216
getContainer: () => container,
245217
getScrollPaddingBottomPx: () => 24,
246218
env: clock.env,
247219
});
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");
263220

264-
expect(container.scrollTop).toBeCloseTo(pageDistance * 2, 5);
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+
}
265242
});
266243

267-
// Holding the key is the user moving the page, so it stays continuous under reduced motion.
268244
test.each([false, true])(
269245
"continues scrolling on hold without repeated keydown events and stops on keyup (reduced motion: %s)",
270246
(reducedMotion) => {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,9 @@ export function createPageScrollController({
208208
state.holdActive = false;
209209
};
210210

211-
const smoothScrollBy = (container: PageScrollContainer, deltaY: number) => {
211+
const scrollBy = (container: PageScrollContainer, deltaY: number) => {
212212
cancelDiscreteAnimation();
213213

214-
// Read per press so a preference change applies without a reload.
215214
if (env.prefersReducedMotion()) {
216215
container.scrollTop += deltaY;
217216
return;
@@ -287,7 +286,7 @@ export function createPageScrollController({
287286
state.activeKey = key;
288287
onScrollStart?.(key);
289288

290-
smoothScrollBy(container, readPageScrollDistance(container) * getDirection(key));
289+
scrollBy(container, readPageScrollDistance(container) * getDirection(key));
291290

292291
state.holdDelayTimeout = env.setTimeout(() => {
293292
state.holdDelayTimeout = 0;

0 commit comments

Comments
 (0)