Repository navigation
fix(core): preserve caret in XDSChatComposerInput on controlled-value updates - #2397
Conversation
|
@lexs must be a member of the Meta Open Source team on Vercel to deploy. Learn more about collaboration on Vercel and other options here. |
🚀 Vercel Preview Deployment
|
PR Analysis Report📚 Storybook PreviewView Storybook for this PR 🧪 Sandbox PreviewView Sandbox for this PR No new or modified components detected. Bundle Size Summary
Accessibility AuditStatus: No accessibility violations detected. Generated by PR Enrichment workflow | Storybook | Sandbox | View full report |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
… 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.
88c0aae to
7bdbc18
Compare
cixzhang
left a comment
There was a problem hiding this comment.
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.
Summary
XDSChatComposerInputjumps the caret to offset 0 whenever the parent updates the controlledvaluewhile the editable is focused. The most visible repro is a slash-command pick that callssetValue('/feedback ')— the next keystroke lands at the start of the input instead of at the end:Root cause: the controlled-value sync effect always wrote
editableRef.current.textContent = controlledValuewhenever serialized DOM differed fromcontrolledValue. SettingtextContenttears down the existing text node, which collapses anySelectioninside the editable to offset 0.Fix
The effect now:
onChangeemission. ApendingEchoValueRefis set to the value we last emitted; the effect early-returns and consumes the marker whencontrolledValue === 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.textContent, and the editable was focused at the time, it callsselection.collapse(false)afterselectNodeContents(editable)so the next keystroke appends rather than prepends.Tests
Adds a
controlled value syncdescribe block toXDSChatComposerInput.test.tsxwith four cases:textContent."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).Full
pnpm testcouldn't be run locally (pre-existing 18 failures inXDSDateTimeInput.test.tsxonmain, unrelated locale issues). All Chat tests green.Notes