Skip to content

fix(chat): respect prefers-reduced-motion in the scroll-follow spring - #4104

Merged
humbertovirtudes merged 1 commit into
meta:mainfrom
yyq1025:chat-spring-reduced-motion
Jul 24, 2026
Merged

humbertovirtudes merged 1 commit into
meta:mainfrom
yyq1025:chat-spring-reduced-motion

Conversation

@yyq1025

@yyq1025 yyq1025 commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Summary

Follow-up promised in #3800: the Chat scroll-follow spring in useChatStreamScroll ran its rAF animation regardless of prefers-reduced-motion.

Every spring entry point — the locked growth-follow (scrollIfLocked), scrollToBottom()'s default 'spring' behavior, and lock() — funnels through startAnimation, so this adds a single branch there: when the user prefers reduced motion, fall back to the existing jumpToBottom. Following the bottom still works exactly as before; it just lands in one frame instead of animating the travel. scrollToBottom({behavior: 'instant'}) and the first-fill positioning were already instant and are unchanged.

Reads the preference via the package's SSR-safe useMediaQuery, matching the convention already used by useStreamingText (#3730), Spinner, Skeleton, and Layer.

Test plan

  • Two new unit tests in useChatStreamScroll.test.tsx: post-first-fill growth positions synchronously with no animation frames scheduled, and default scrollToBottom() jumps instantly and re-locks. The matchMedia stub mirrors the pattern in useStreamingText.test.ts.
  • vitest run packages/core/src/Chat/useChatStreamScroll.test.tsx — 9/9 pass; eslint + prettier clean on the touched files.

🤖 Generated with Claude Code

Follow-up promised in meta#3800: the rAF spring in useChatStreamScroll
ignored prefers-reduced-motion. All spring entry points funnel through
startAnimation, which now falls back to the existing jumpToBottom under
the preference — locked following, scrollToBottom(), and lock() still
track the bottom, just without animated travel.

Co-Authored-By: Claude Fable 5 <[email protected]>
@vercel

vercel Bot commented Jul 20, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
astryx Ready Ready Preview, Comment Jul 20, 2026 3:30pm

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jul 20, 2026
@github-actions github-actions Bot added community Authored by a community contributor (not on the eng/design team) needs:code-review High-risk change (new package/component/API) — needs human code review before merge labels Jul 20, 2026
@github-actions

Copy link
Copy Markdown
Contributor

PR Analysis Report

Modified Components

Chat
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 5240 -
Complexity N/A Very High (425) -

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@astryxdesign/core N/A 4.7KB 0B

Accessibility Audit

Status: 1 accessibility violation(s) found — 1 serious.

Chat - 1 issue(s)
  • 🟠 serious: Ensure the contrast between foreground and background colors meets WCAG 2 AA minimum contrast ratio thresholds
    • Rule: color-contrast · Affects 2/8 stories · Learn more
    • WCAG: 1.4.3 (Level AA)

Generated by PR Enrichment workflow | View full report

@humbertovirtudes humbertovirtudes left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice fix. Funneling every spring entry point (scrollToBottom, lock, scrollIfLocked growth-follow) through the single startAnimation branch so one reduced-motion check covers them all is the right consolidation, and falling back to the existing instant jumpToBottom means following still tracks the bottom, it just does not animate the travel. Reusing the shared useMediaQuery hook rather than hand-rolling a matchMedia listener is the correct call.

The two red-to-green tests (streaming growth lands synchronously, default scrollToBottom jumps with an empty rAF queue) pin the contract well, and the matchMedia stub mirrors the existing useStreamingText test pattern. Changeset present. Approving.

@humbertovirtudes
humbertovirtudes enabled auto-merge (squash) July 24, 2026 18:02
@github-actions github-actions Bot removed the needs:code-review High-risk change (new package/component/API) — needs human code review before merge label Jul 24, 2026
@humbertovirtudes
humbertovirtudes merged commit fe8657a into meta:main Jul 24, 2026
20 checks passed
@github-actions
github-actions Bot deleted the chat-spring-reduced-motion branch July 25, 2026 08:17
cixzhang pushed a commit that referenced this pull request Sep 4, 2026
)

* fix(chat): gestures vouch for their scroll instead of unlocking (#5662)

`onWheel` and `onTouchMove` gated on `animatingRef`, which has meant "we
are following" only by coincidence. #4104 added a reduced-motion path
that follows without ever starting a spring, so both handlers became
permanent no-ops for readers with reduced motion, and the remaining
release path — `onScroll`'s direction check — is blind exactly when it
matters, since arriving content changes the geometry on nearly every
event.

Restores the arbitration this hook was extracted from (#1249): the
gesture does not unlock, it clears the suppression so the scroll it
produces is read as intent. Here that means waiving the synthetic-resize
guard for the frames the gesture covers and letting the direction check
decide. A gesture this scroller never consumed — a wheel over a nested
scrollable child, an overscroll at the bottom — produces no scroll event
and now leaves following alone, where before it released the tail
whenever the spring was live. `touchstart` replaces `touchmove` for the
reason it was the original listener: it fires once, before any movement,
and needs no per-drag bookkeeping.

`jumpToBottom` (#3800) also cleared `animatingRef` while `lockedRef`
stayed true, which no other caller did, so a running loop survived its
own cancellation and the next `startAnimation` stacked a second one on
top (#5663). A generation token makes the documented cancel real; with
the gesture handlers off `animatingRef`, it is loop bookkeeping again.

Co-Authored-By: Claude Opus 5 <[email protected]>

* fix(chat): a resize clamp landing on the bottom is never a release

A finger resting on a nested scrollable child bubbles `touchstart` to
the chat scroller, so the gesture waiver was live for a drag this
scroller never consumed. If an earlier block collapsed while the finger
was down, the browser clamped `scrollTop` up to the new bottom and the
waived guard read that clamp as the reader scrolling: following
released, and the lines streamed next stayed below the fold. The wheel
waiver had the same hole, two frames wide.

A clamp can only ever land on the new maximum, and a reader scrolling
up never lands there. So a scroll that arrives with changed geometry
and ends at the bottom is the clamp regardless of any gesture, and the
guard now keeps it synthetic. The waiver still lets a real scroll-up
through while content is arriving, which is what it was for.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* refactor(chat): cancel the spring's frame instead of tagging generations

The loop's continuation is always one pending rAF handle, and
`jumpToBottom` only ever runs from an event or another frame callback,
so `cancelAnimationFrame` on that handle is an exact cancel — the same
`rafRef` shape `useChatDictation` and `useSpeechRecognition` use.
`animate` goes back to taking no argument, and the generation counter,
its per-frame closure, and the check at the top of the tick are gone.

The test double now hands out real ids and honours cancellation, so the
stacked-loop test keeps proving the same thing. Comments around the
synthetic-resize guard and the wheel waiver are folded down to what the
code no longer says on its own.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* fix(chat): only a gesture this scroller consumes waives the guard

A bubbled gesture was treated as ownership of every scroll that
followed. With the spring trailing a fast stream, a finger resting on a
nested scroller plus a collapse above the viewport moved scrollTop up
by scroll anchoring — not a clamp, so it did not land on the bottom —
and the waived guard read the shift as the reader. Following released.

Ownership now comes from the gesture itself. A wheel up vouches only
when no vertically scrollable element between its target and this
scroller still has room above; scroll chaining hands a gesture outward
only from a scroller at its edge. A drag records its target and finger
position on touchstart and vouches from the first touchmove that pushes
this scroller up, under the same nested check, until the finger lifts.
A resting finger is not a gesture. The bottom-landing rule stays for the
one clamp a consumed gesture can still coincide with: an overscroll at
the bottom while a block above collapses.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* fix(chat): own the position while following instead of judging gestures

Every revision so far inferred whether a scroll was the reader's from
the gesture that preceded it, and every inference has a counterexample:
a bubbled touchstart, a nested scroller at its edge, then one with
`overscroll-behavior: contain`. The question was never who owns the
gesture. It is what can move a following container up other than the
reader, and there are two answers: the resize clamp onto the bottom,
and CSS scroll anchoring.

The clamp lands exactly on the bottom, which the guard already checks.
Anchoring is switched off on the scroll element while following and
restored when not — the hook is the only writer of the position while
locked, so anchoring has nothing to do there, and a reader up in the
history keeps it. With that, any upward scroll that does not land on
the bottom is the reader, and the wheel and touch listeners, the
gesture waiver, the two-frame expiry, and the synthetic-resize tracking
all go. Same shape as ListInput (#4740) and LegendList's
maintainVisibleContentPosition, for the same reason.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* fix(chat): hand back the scroll element's own overflow-anchor

Unlocking wrote '' over whatever inline overflow-anchor the consumer had
set, so a scroller that had disabled anchoring itself got it back once
the reader scrolled up. Capture the element's inline value when the
hook takes it over and restore that value on unlock and on cleanup.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* fix(chat): judge a scroll against the hook's own last write

Two ownership gaps from review. A reader's wheel that lands between two
spring frames was compared against the last observed position, and the
next frame's write outran it — net position still advanced, so the
reader stayed pinned. The spring now records the position it wrote, so
the coalesced scroll event that follows reads below it and releases;
the same comparison use-stick-to-bottom makes against the position it
set (`ignoreScrollToTop`).

And the anchoring switch no longer goes through the element's inline
style at all. It is an attribute, `data-astryx-chat-following`, and one
injected rule with `!important`, the pattern `highlightRanges` uses for
its dynamic stylesheet. A consumer's own `overflow-anchor` is never read,
written or restored — whatever they set, before or during a lock, is
what applies once the attribute is gone.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

---------

Co-authored-by: Claude Opus 5 <[email protected]>

This branch was successfully deployed

1 active deployment
Preview — c47ef5a1 Deployed Jul 20, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot. community Authored by a community contributor (not on the eng/design team)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants