Skip to content

fix(core): preserve caret in XDSChatComposerInput on controlled-value updates - #2397

Merged
cixzhang merged 2 commits into
mainfrom
fix-chat-composer-caret
Jun 2, 2026
Merged

cixzhang merged 2 commits into
mainfrom
fix-chat-composer-caret

Conversation

@lexs

@lexs lexs commented May 28, 2026

Copy link
Copy Markdown
Contributor

Summary

XDSChatComposerInput jumps the caret to offset 0 whenever the parent updates the controlled value while the editable is focused. The most visible repro is a slash-command pick that calls setValue('/feedback ') — the next keystroke lands at the start of the input instead of at the end:

Type /, press Enter to pick /feedback, then type X → value becomes "X/feedback ".

Root cause: the controlled-value sync effect always wrote editableRef.current.textContent = controlledValue whenever serialized DOM differed from controlledValue. Setting textContent tears down the existing text node, which collapses any Selection inside the editable to offset 0.

Fix

The effect now:

  1. Skips exactly one echo of our own onChange emission. A pendingEchoValueRef is set to the value we last emitted; the effect early-returns and consumes the marker when controlledValue === pendingEchoValueRef.current. This avoids redundant DOM rebuilds for the common "we typed → emit → parent setState → re-render" round-trip. The marker is one-shot — consumed on use, and invalidated by any non-echoing external update — so a later external set back to the same string is still applied.
  2. Restores the caret to the end of the new content on a true external override. When the effect does need to overwrite textContent, and the editable was focused at the time, it calls selection.collapse(false) after selectNodeContents(editable) so the next keystroke appends rather than prepends.

Tests

Adds a controlled value sync describe block to XDSChatComposerInput.test.tsx with four cases:

  • Programmatic value change places caret at end (the slash-command repro).
  • Echo of own emission is a no-op — DOM text node is preserved.
  • Unfocused external change still updates textContent.
  • One-shot marker invalidation: emit "hello" → parent sets "world" → parent sets "hello" is applied (regression for the stale-cache failure mode codex flagged on the first draft of the fix).
✓ packages/core/src/Chat/XDSChatComposerInput.test.tsx (41 tests) 543ms
✓ packages/core/src/Chat/ (10 files | 118 tests passed)

Full pnpm test couldn't be run locally (pre-existing 18 failures in XDSDateTimeInput.test.tsx on main, unrelated locale issues). All Chat tests green.

Notes

  • No public API change; props and behavior for controlled mode are unchanged for parents that don't move the value out from under the user.
  • Independent observation that fed into this fix: redundant DOM rebuilds on every echo also caused noticeable flicker / dropped chars when typing fast through Scout's chat composer.

@vercel

vercel Bot commented May 28, 2026

Copy link
Copy Markdown

@lexs must be a member of the Meta Open Source team on Vercel to deploy.
- Click here to add @lexs to the team.
- If you initiated this build, request access.

Learn more about collaboration on Vercel and other options here.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label May 28, 2026
@github-actions

github-actions Bot commented May 28, 2026 •

Copy link
Copy Markdown
Contributor

🚀 Vercel Preview Deployment

Status ✅ Deployed
Preview Open Preview
Commit 7bdbc18
Inspect Vercel Dashboard
Workflow View Logs

No authentication required — anyone with the link can view the preview.

@github-actions

github-actions Bot commented May 28, 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 21.1KB 32.5KB 4.8KB

Accessibility Audit

Status: No accessibility violations detected.


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

@vercel

vercel Bot commented May 28, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
xds-sandbox Error Error Jun 1, 2026 11:47pm

Request Review

lexs added 2 commits June 1, 2026 23:44
… updates

Programmatic value changes from the parent (e.g. a slash-command pick
that calls `setValue('/feedback ')`) used to collapse the caret to
offset 0 — the next keystroke landed at the start of the input instead
of at the end. The controlled-value sync effect always wrote
`editableRef.current.textContent = value`, which tears down the
existing text node and resets any Selection inside the editable.

The effect now:

- Skips exactly one echo of our most recent `onChange` emission via a
  one-shot `pendingEchoValueRef` marker, so we don't rebuild the DOM
  for value updates that simply round-trip through the parent. The
  marker is consumed on use (or invalidated by any non-echoing update)
  so a later external set back to the same string is still applied.
- When a true external override does require rewriting `textContent`,
  restores the caret to the end of the new content (via
  `selectNodeContents` + `collapse(false)`) if the editable was
  focused at the time.

Adds tests covering: caret-at-end after programmatic value change,
echo no-op (DOM and text node preserved), unfocused external change,
and the cache-invalidation regression around the one-shot marker.
…/set-state-in-effect

The early-return restructure moved `setIsEmpty` to the top-level of the
effect body, which the rule flagged as unguarded set-state-in-effect.
Wrap the DOM-mutation work (and the trailing `setIsEmpty`) back inside
an explicit `if (serialize(editable) !== controlledValue)` block,
mirroring the original code's nesting. Same semantics; tests unchanged.

@cixzhang cixzhang 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.

Clean fix for a real UX bug. The one-shot echo marker is well-designed (consumed on match, invalidated on non-echo override, tests cover the stale-cache regression). Caret-to-end on external writes is strictly better than caret-to-start.

Approved — noting separately that the effect-based controlled-value sync pattern is architectural debt we should revisit (contentEditable isn't a controlled input, and the echo-detection is fighting that reality). Will look at alternative approaches.

@cixzhang
cixzhang merged commit d44d7a4 into main Jun 2, 2026
21 of 22 checks passed
@cixzhang
cixzhang deleted the fix-chat-composer-caret branch June 21, 2026 16:55

This branch had an error being deployed

1 failed deployment
Preview — 7bdbc185 Deployed Jun 1, 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.

2 participants