lexical icon indicating copy to clipboard operation
lexical copied to clipboard

[lexical][lexical-react] Bug Fix: add ignoreFocusChange option to OnC…

Open Ranjan00001 opened this issue 5 months ago • 5 comments

                                                                                                                                                                                                                                     Adds ignoreFocusChange option to OnChangePlugin. When true, onChange is not called for updates tagged with FOCUS_TAG (e.g., when editor.focus() runs via AutoFocusPlugin on mount). This prevents spurious onChange calls that aren't actual content changes.                                                                                                                                                                    

Defaults to false (backwards compatible).

Also exports FOCUS_TAG from lexical so consumers can check for it in the tags set received by onChange.

Closes #4493

Reproducing the issue

Without this fix, using AutoFocusPlugin triggers an unwanted onChange call on mount:

 <LexicalComposer initialConfig={...}>                                                                                                                                                                                                   
   <AutoFocusPlugin />                                                                                                                                                                                                                   
   <OnChangePlugin onChange={(editorState) => {                                                                                                                                                                                          
     // Called on mount due to AutoFocusPlugin's editor.focus(),                                                                                                                                                                         
     // even though no content changed                                                                                                                                                                                                   
   }} />                                                                                                                                                                                                                                 
 </LexicalComposer>                                                                                                                                                                                                                      

With ignoreFocusChange={true}, the focus-triggered call is suppressed:

What changed

  • lexical/src/index.ts — export FOCUS_TAG
  • @lexical/react/LexicalOnChangePlugin — add ignoreFocusChange prop (default false)
  • Unit tests — 7 tests total covering tag-based suppression, editor.focus() trigger path, and tags passthrough

Ranjan00001 avatar Jun 13 '26 01:06 Ranjan00001

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

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview, Comment Jun 18, 2026 2:41pm
lexical-playground Ready Ready Preview, Comment Jun 18, 2026 2:41pm

Request Review

vercel[bot] avatar Jun 13 '26 01:06 vercel[bot]

Tried reproducing this locally. Sharing what I saw in case it's useful.

The bug only seemed to surface when editor.focus() lands in its own commit. When setRootElement and focus collapse into the same commit, that commit ends up with history-merge and the default OnChangePlugin skips it. Inserting await new Promise(r => setTimeout(r, 0)) between setRootElement and focus splits them apart, and the focus commit then arrives alone with just the focus tag, which the default OnChangePlugin doesn't skip. That seemed to match AutoFocusPlugin's useEffect timing in a real React mount.

For a regression test, mirroring OnChangePlugin's listener with and without the tags.has(FOCUS_TAG) skip on a setup that uses the macrotask gap might be enough — the default variant fires once with ["focus"], the ignoreFocusChange variant stays silent.

CodeNode / CodeHighlight didn't seem to be required in my setup — plain paragraph reproduced it too.

mayrang avatar Jun 13 '26 02:06 mayrang

@mayrang @potatowagon pleas check now. I hav added unit tests.

Ranjan00001 avatar Jun 18 '26 14:06 Ranjan00001

The tests pass on --project unit, but every case calls $addUpdateTag(FOCUS_TAG) by hand inside editor.update. That only checks the option matching the tag — not the actual #4493 trigger, which is editor.focus() itself emitting FOCUS_TAG and causing a spurious onChange when AutoFocusPlugin runs focus on mount. None of the cases call editor.focus(), so that path isn't exercised.

A test that does — contenteditable rootElement set, one initial update, a microtask awaited, then editor.focus() in act — would cover it: true keeps onChange silent, false fires it once.

Also — could you flesh out the PR description a bit? It's still the template placeholders (Describe the changes in this pull request, Closes #<!-- issue number -->, Before/After). Even just a short description, Closes #4493, and a quick repro would make it much easier to review.

mayrang avatar Jun 18 '26 16:06 mayrang

The description needs a rewrite. Every line has leading whitespace that GitHub renders as a code block, and it doesn't follow the repo's PR template (## Description / ## Test plan / Before & After). The "What changed" section is a file list — other merged PRs in this repo explain the bug mechanism instead. For this PR, that's: editor.focus() internally calls $addUpdateTag(FOCUS_TAG), committing an update with no dirty nodes that OnChangePlugin doesn't skip, so onChange fires spuriously.

Also worth noting: FOCUS_TAG was internal-only before this PR. Now that it's exported, consumers can also filter directly in their onChange callback via tags.has(FOCUS_TAG) — the convenience prop mirrors ignoreHistoryMergeTagChange and is consistent, but the export is independently useful.

Left two inline comments on the code.

mayrang avatar Jun 21 '26 06:06 mayrang

Closing this as stale, feel free to re-open or create a new PR if you'd like to continue this work

etrepum avatar Aug 21 '26 22:08 etrepum