An open source design system that's fully customizable and agent ready
TypeScript
fix(chat): make the scroll-follow spring respect prefers-reduced-motion (#4104)
Yueqian Yangfe8657a165ee
diff --git a/.changeset/chat-spring-reduced-motion.md b/.changeset/chat-spring-reduced-motion.mdnew file mode 100644index 0000000000..5da1ed0033--- /dev/null+++ b/.changeset/chat-spring-reduced-motion.md@@ -0,0 +1,6 @@+---+'@astryxdesign/core': patch+---++[fix] Chat/useChatStreamScroll: the scroll-follow spring now respects `prefers-reduced-motion` — locked following, `scrollToBottom()`, and `lock()` fall back to the existing instant jump, so the transcript still tracks the bottom without animated travel. Follow-up promised in #3800.+@yyq1025diff --git a/packages/core/src/Chat/useChatStreamScroll.test.tsx b/packages/core/src/Chat/useChatStreamScroll.test.tsxindex d9177e775f..dfea626714 100644--- a/packages/core/src/Chat/useChatStreamScroll.test.tsx+++ b/packages/core/src/Chat/useChatStreamScroll.test.tsx@@ -45,6 +45,23 @@ afterEach(() => {vi.unstubAllGlobals();});+// The global test setup polyfills matchMedia with a never-matching stub;+// this override makes only the reduced-motion query match, mirroring the+// pattern in useStreamingText.test.ts. Undone by afterEach's+// unstubAllGlobals.+function stubReducedMotion() {+ vi.stubGlobal('matchMedia', (query: string) => ({+ matches: query.includes('prefers-reduced-motion'),+ media: query,+ onchange: null,+ addListener: () => {},+ removeListener: () => {},+ addEventListener: () => {},+ removeEventListener: () => {},+ dispatchEvent: () => false,+ }));+}+function setGeometry(el: HTMLElement,{scrollHeight, clientHeight}: {scrollHeight: number; clientHeight: number},@@ -166,3 +183,35 @@ describe("useChatStreamScroll — scrollToBottom({behavior: 'instant'})", () =>expect(rafQueue.length).toBeGreaterThan(0);});});++describe('useChatStreamScroll — prefers-reduced-motion', () => {+ it('streaming growth jumps synchronously instead of springing', () => {+ stubReducedMotion();+ const {api, el} = renderHook();+ setGeometry(el, {scrollHeight: 1000, clientHeight: 400});+ flushRaf(); // consume the mount jump+ expect(el.scrollTop).toBe(600);++ // Post-first-fill growth would normally take the spring path; under+ // reduced motion it must land in the same synchronous step.+ setGeometry(el, {scrollHeight: 1400, clientHeight: 400});+ rafQueue = [];+ act(() => api.current!.scrollIfLocked());+ expect(el.scrollTop).toBe(1000);+ expect(rafQueue).toHaveLength(0);+ });++ it('default scrollToBottom falls back to an instant jump', () => {+ stubReducedMotion();+ const {api, el} = renderHook();+ setGeometry(el, {scrollHeight: 1000, clientHeight: 400});+ flushRaf();+ el.scrollTop = 100; // user scrolled up++ rafQueue = [];+ act(() => api.current!.scrollToBottom());+ expect(el.scrollTop).toBe(600);+ expect(rafQueue).toHaveLength(0);+ expect(api.current!.isLocked).toBe(true);+ });+});diff --git a/packages/core/src/Chat/useChatStreamScroll.ts b/packages/core/src/Chat/useChatStreamScroll.tsindex 345cb652e9..cc9a9f649c 100644--- a/packages/core/src/Chat/useChatStreamScroll.ts+++ b/packages/core/src/Chat/useChatStreamScroll.ts@@ -4,7 +4,7 @@/*** @file useChatStreamScroll.ts- * @input Uses React refs, state, callbacks+ * @input Uses React refs, state, callbacks, useMediaQuery* @output Exports useChatStreamScroll hook for AI chat scroll behavior* @position Utility hook — used by ChatLayout, also usable standalone*@@ -15,6 +15,8 @@* - The first fill positions instantly (whether content is present at* mount or arrives async); only subsequent growth springs* - `scrollToBottom({behavior: 'instant'})` jumps in one frame, no animation+ * - Under `prefers-reduced-motion`, every spring path falls back to the+ * same instant jump — following still works, it just doesn't animate** Uses scroll direction (lastScrollTop comparison) to detect user* intent — works for wheel, touch, scrollbar drag, keyboard, everything.@@ -26,6 +28,7 @@*/import {useCallback, useEffect, useRef, useState} from 'react';+import {useMediaQuery} from '../hooks/useMediaQuery';// =============================================================================// Types@@ -37,7 +40,8 @@ export interface ChatScrollToBottomOptions {* the spring animation. Use for programmatic positioning (opening a* conversation, restoring a session) — keep the default `'spring'` for* user-initiated scrolls like the scroll-to-bottom button. Mirrors the- * DOM's `scrollTo({behavior})`.+ * DOM's `scrollTo({behavior})`. When the user prefers reduced motion,+ * `'spring'` also jumps instantly.* @default 'spring'*/behavior?: 'instant' | 'spring';@@ -189,14 +193,6 @@ export function useChatStreamScroll({requestAnimationFrame(animate);}, [scrollRef, damping, stiffness, mass]);- const startAnimation = useCallback(() => {- if (!animatingRef.current && lockedRef.current) {- animatingRef.current = true;- lastTickRef.current = undefined;- requestAnimationFrame(animate);- }- }, [animate]);-// Jump to the bottom in a single frame — cancels any in-flight spring so// a later animation tick can't fight the assignment.const jumpToBottom = useCallback(() => {@@ -211,6 +207,27 @@ export function useChatStreamScroll({lastScrollTopRef.current = el.scrollTop;}, [scrollRef]);+ const prefersReducedMotion = useMediaQuery(+ '(prefers-reduced-motion: reduce)',+ );++ // Every spring entry point (scrollToBottom, lock, scrollIfLocked growth+ // follow) funnels through here, so this one branch covers them all.+ const startAnimation = useCallback(() => {+ if (!lockedRef.current) {+ return;+ }+ if (prefersReducedMotion) {+ jumpToBottom();+ return;+ }+ if (!animatingRef.current) {+ animatingRef.current = true;+ lastTickRef.current = undefined;+ requestAnimationFrame(animate);+ }+ }, [animate, jumpToBottom, prefersReducedMotion]);+// --- Public API ---const scrollToBottom = useCallback(