Skip to content

fix(Chat): use callback ref for content observation (#2282) - #2285

Merged
cixzhang merged 5 commits into
mainfrom
navi/fix/auto-scroll-demo-story
May 22, 2026
Merged

cixzhang merged 5 commits into
mainfrom
navi/fix/auto-scroll-demo-story

Conversation

@cixzhang

@cixzhang cixzhang commented May 21, 2026 •

Copy link
Copy Markdown
Contributor

What

Fixes #2282 — auto-scroll was completely broken when XDSChatMessageList mounts conditionally (e.g. behind {!isEmpty && ...}).

Root Cause

useXDSChatNewMessages used a ref-object-based contentRef as a useEffect dependency. Ref objects have stable identity, so the effect only ran once — on mount. When XDSChatMessageList mounted later (after the first message arrived), contentRef.current was set but the observer effect never re-ran. No ResizeObserver ever attached → scrollIfLocked() never called → no auto-scroll.

This affects any consumer that conditionally renders XDSChatMessageList (like Clio does with {!isEmpty && <ChatView />}). The Storybook stories didn't catch it because they always render the message list with seed data from the start.

Fix

useXDSChatNewMessages now returns a callback ref instead of accepting a ref object. When the content element mounts or unmounts, the callback fires and the ResizeObserver attaches/detaches immediately — no state, no version counter. The ref callback itself is the subscription mechanism.

- const contentRef = useRef<HTMLElement>(null);
- const newMsgs = useXDSChatNewMessages({ contentRef, isLocked, onResize });
- // ... pass setContentRef to context manually
+ const newMsgs = useXDSChatNewMessages({ isLocked, onResize });
+ // newMsgs.contentRef is the callback ref — pass directly to context

Testing

  • 4 new tests covering: immediate mount, late mount, unmount cleanup, full XDSChatLayout integration
  • All 111 existing Chat tests pass

Storybook

Also adds ChatAutoScroll.stories.tsx with interactive demos:

  • Scroll Behavior Comparison — stream text vs tool calls vs custom elements
  • Rapid Tool Calls — stress test at 500ms intervals
  • Mixed Stream + Tools — realistic agent sequence showing transition points

Adds ChatAutoScroll.stories.tsx with three stories demonstrating
the difference in auto-scroll behavior between streaming text and
discrete block additions (tool calls, custom elements):

- ScrollBehaviorComparison: interactive controls to test each case
- RapidToolCalls: stress-tests rapid discrete additions
- MixedStreamAndTools: realistic sequence showing the transition
@vercel

vercel Bot commented May 21, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
xds-sandbox Ready Ready Preview, Comment May 21, 2026 11:36pm

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label May 21, 2026
- XDSBadge uses label prop, not children
- XDSText type='small' doesn't exist, use 'label' instead
@github-actions

github-actions Bot commented May 21, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

🧪 Sandbox Preview

View Sandbox for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

No new or modified components detected.

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@xds/core 19.5KB 30.1KB 4.5KB

Accessibility Audit

Status: No accessibility violations detected.


Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

Adds tests proving that useXDSChatNewMessages does NOT attach its
ResizeObserver when contentRef.current is set after the initial
effect run (e.g. when XDSChatMessageList mounts conditionally).

The tests assert the *broken* behavior — they will need to be
flipped to assert the correct behavior once the fix is applied:
- 'does NOT call onResize' → 'DOES call onResize'
- 'does NOT observe content element' → 'DOES observe content element'

Root cause: useEffect dep is [contentRef] (ref object, stable identity),
so the effect never re-runs when contentRef.current changes from null
to a DOM element.
useXDSChatNewMessages previously depended on a contentRef (ref object)
passed as an option, with useEffect dep [contentRef]. Since ref objects
have stable identity, the effect only ran once on mount. When
XDSChatMessageList mounted conditionally (e.g. {!isEmpty && ...}),
contentRef.current was null at effect time and the ResizeObserver never
attached — breaking auto-scroll entirely.

Fix: useXDSChatNewMessages now returns a callback ref. When the content
element mounts or unmounts, the callback fires and the observer
attaches/detaches immediately. No state, no version counter — the ref
callback itself is the subscription mechanism.

XDSChatLayout passes the hook's returned contentRef directly into
the layout context. XDSChatMessageList calls it when it mounts,
and the observer attaches on the spot.
@cixzhang cixzhang changed the title feat(storybook): add auto-scroll demo stories for issue #2282 fix(Chat): use callback ref for content observation (#2282) May 21, 2026
@cixzhang
cixzhang merged commit b6cf8ee into main May 22, 2026
20 checks passed
cixzhang added a commit that referenced this pull request Jun 21, 2026
* feat(storybook): add auto-scroll demo stories for issue #2282

Adds ChatAutoScroll.stories.tsx with three stories demonstrating
the difference in auto-scroll behavior between streaming text and
discrete block additions (tool calls, custom elements):

- ScrollBehaviorComparison: interactive controls to test each case
- RapidToolCalls: stress-tests rapid discrete additions
- MixedStreamAndTools: realistic sequence showing the transition

* fix: use correct XDSBadge label prop and XDSText type

- XDSBadge uses label prop, not children
- XDSText type='small' doesn't exist, use 'label' instead

* test: add failing tests for late-mount contentRef bug (#2282)

Adds tests proving that useXDSChatNewMessages does NOT attach its
ResizeObserver when contentRef.current is set after the initial
effect run (e.g. when XDSChatMessageList mounts conditionally).

The tests assert the *broken* behavior — they will need to be
flipped to assert the correct behavior once the fix is applied:
- 'does NOT call onResize' → 'DOES call onResize'
- 'does NOT observe content element' → 'DOES observe content element'

Root cause: useEffect dep is [contentRef] (ref object, stable identity),
so the effect never re-runs when contentRef.current changes from null
to a DOM element.

* fix(Chat): use callback ref for content observation (#2282)

useXDSChatNewMessages previously depended on a contentRef (ref object)
passed as an option, with useEffect dep [contentRef]. Since ref objects
have stable identity, the effect only ran once on mount. When
XDSChatMessageList mounted conditionally (e.g. {!isEmpty && ...}),
contentRef.current was null at effect time and the ResizeObserver never
attached — breaking auto-scroll entirely.

Fix: useXDSChatNewMessages now returns a callback ref. When the content
element mounts or unmounts, the callback fires and the observer
attaches/detaches immediately. No state, no version counter — the ref
callback itself is the subscription mechanism.

XDSChatLayout passes the hook's returned contentRef directly into
the layout context. XDSChatMessageList calls it when it mounts,
and the observer attaches on the spot.

* fix: add type="button" to test buttons
cixzhang added a commit that referenced this pull request Jun 21, 2026
* feat(storybook): add auto-scroll demo stories for issue #2282

Adds ChatAutoScroll.stories.tsx with three stories demonstrating
the difference in auto-scroll behavior between streaming text and
discrete block additions (tool calls, custom elements):

- ScrollBehaviorComparison: interactive controls to test each case
- RapidToolCalls: stress-tests rapid discrete additions
- MixedStreamAndTools: realistic sequence showing the transition

* fix: use correct XDSBadge label prop and XDSText type

- XDSBadge uses label prop, not children
- XDSText type='small' doesn't exist, use 'label' instead

* test: add failing tests for late-mount contentRef bug (#2282)

Adds tests proving that useXDSChatNewMessages does NOT attach its
ResizeObserver when contentRef.current is set after the initial
effect run (e.g. when XDSChatMessageList mounts conditionally).

The tests assert the *broken* behavior — they will need to be
flipped to assert the correct behavior once the fix is applied:
- 'does NOT call onResize' → 'DOES call onResize'
- 'does NOT observe content element' → 'DOES observe content element'

Root cause: useEffect dep is [contentRef] (ref object, stable identity),
so the effect never re-runs when contentRef.current changes from null
to a DOM element.

* fix(Chat): use callback ref for content observation (#2282)

useXDSChatNewMessages previously depended on a contentRef (ref object)
passed as an option, with useEffect dep [contentRef]. Since ref objects
have stable identity, the effect only ran once on mount. When
XDSChatMessageList mounted conditionally (e.g. {!isEmpty && ...}),
contentRef.current was null at effect time and the ResizeObserver never
attached — breaking auto-scroll entirely.

Fix: useXDSChatNewMessages now returns a callback ref. When the content
element mounts or unmounts, the callback fires and the observer
attaches/detaches immediately. No state, no version counter — the ref
callback itself is the subscription mechanism.

XDSChatLayout passes the hook's returned contentRef directly into
the layout context. XDSChatMessageList calls it when it mounts,
and the observer attaches on the spot.

* fix: add type="button" to test buttons
@cixzhang
cixzhang deleted the navi/fix/auto-scroll-demo-story branch June 21, 2026 16:59

This branch was successfully deployed

1 active deployment
Preview — b0a4de99 Deployed May 21, 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Auto-scroll doesn't trigger for tool calls and large custom elements

1 participant