Skip to content

[lexical] Bug Fix: Registering a transform no longer moves focus into an unfocused editor - #9425

Open
Vedthakar wants to merge 3 commits into
meta:mainfrom
Vedthakar:fix/5016-transform-focus
Open

Vedthakar wants to merge 3 commits into
meta:mainfrom
Vedthakar:fix/5016-transform-focus

Conversation

@Vedthakar

@Vedthakar Vedthakar commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Description

This solves issue #5016: with AutoLinkPlugin mounted, typing in another input on the page moves focus back into the editor.

Current behavior. registerNodeTransform calls markNodesWithTypesAsDirty, which opens an editor.update() that marks every existing node of that type dirty so the new transform runs on content that already exists. That update is tagged HISTORY_MERGE_TAG only. At commit, dirty nodes mean needsUpdate is true, so $updateDOMSelection runs. If the browser's document selection is still in the editor (browsers keep it there when you focus an <input>), it calls rootElement.focus(). Otherwise it moves the document selection into the editor. Either way the editor takes focus from the input.

AutoLinkPlugin's effect depends on matchers, so an inline matchers array (as in the issue's sandbox) re-registers the transforms on every render of the parent, which happens on every keystroke in the other input.

What this changes.

  • In markNodesWithTypesAsDirty, when the update is a new one and the editor's root element does not contain the active element, the update is also tagged with an internal tag (revalidate-unfocused). This re-validation is not a user edit, so on its own it must not move focus or the selection into an editor that doesn't have focus.
  • At commit, an update with that tag skips the DOM selection only if the selection did not change in the batch (it is not dirty and equals the current one). Update tags last for the whole batch, so the decision has to be made there: editor.focus() or any selection change made later in the same tick marks the selection dirty and is applied as on main (eg. AutoFocusPlugin mounted after AutoLinkPlugin).
  • When the editor has focus, the tags are unchanged (HISTORY_MERGE_TAG only). A transform that changes the DOM under the caret still gets the DOM selection restored.

Relation to #9177. #9177 (open) moves HISTORY_MERGE_TAG into the update callback with $addUpdateTag, so it also applies to an already-open update. It does not touch the selection or focus, so it doesn't fix this issue. The two changes are compatible. If #9177 lands first, the conflict is limited to the options argument of the same editor.update() call.

I left AutoLinkPlugin's dependency on matcher identity alone. With this fix, re-registering is harmless for focus, and memoizing matchers is an API decision for maintainers.

Closes #5016

Test plan

New tests:

  • packages/lexical/src/__tests__/unit/Issue5016Repro.test.ts:
    • "keeps focus in an input outside the editor": the browser keeps the document selection in the editor. After registerNodeTransform, the transform has run on the existing text, the update is tagged HISTORY_MERGE_TAG, and document.activeElement is still the input.
    • "does not move the DOM selection out of an input outside the editor": jsdom's default, where the selection follows focus. The document selection and the input's caret are unchanged.
    • "keeps focus and the caret when the editor has focus": a control. Focus and the DOM caret stay in the editor.
  • packages/lexical-react/src/__tests__/unit/LexicalAutoLinkPlugin.test.tsx › "does not take focus from another input when it re-renders": the issue's setup. AutoLinkPlugin gets an inline matchers array and the parent re-renders while the input has focus.

Before

On main with the tests applied and the source change reverted:

$ pnpm exec vitest --project unit --no-watch packages/lexical/src/__tests__/unit/Issue5016Repro.test.ts packages/lexical-react/src/__tests__/unit/LexicalAutoLinkPlugin.test.tsx
  × keeps focus in an input outside the editor
    AssertionError: expected <div contenteditable="true" …> to be <input></input>
  × does not move the DOM selection out of an input outside the editor
  × does not take focus from another input when it re-renders
    AssertionError: expected <div contenteditable="true" …> to be <input readonly value="ab"></input>
  Tests  3 failed | 1 passed (4)

After

On this branch, rebased onto current main:

  • packages/lexical/src/__tests__/browser/Issue5016TransformFocus.test.ts (browser, Chromium, real click into another input): 3 passed. "registering a transform leaves focus in another input" fails on main; "editor.focus() after registering a transform still focuses the editor" and "a selection made after registering a transform is still applied" fail with the first version of this PR, which tagged the batch with SKIP_DOM_SELECTION_TAG.
  • Issue5016Repro.test.ts, LexicalAutoLinkPlugin.test.tsx and LexicalEditor.test (unit): 99 passed.
  • tsc, tsc-test, eslint and prettier on the changed files: clean. Firefox and WebKit were not run locally.

Author: Ved Thakar (@Vedthakar)

🤖 Generated with Claude Code

@vercel

vercel Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
lexical Ready Ready Preview Nov 16, 2026 11:43pm UTC
lexical-playground Ready Ready Preview Nov 16, 2026 11:43pm UTC

Request Review

@pkg-pr-new

pkg-pr-new Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
lexical

npm i https://pkg.pr.new/lexical@9425

@lexical/a11y

npm i https://pkg.pr.new/@lexical/a11y@9425

@lexical/clipboard

npm i https://pkg.pr.new/@lexical/clipboard@9425

@lexical/code

npm i https://pkg.pr.new/@lexical/code@9425

@lexical/code-core

npm i https://pkg.pr.new/@lexical/code-core@9425

@lexical/code-prism

npm i https://pkg.pr.new/@lexical/code-prism@9425

@lexical/code-shiki

npm i https://pkg.pr.new/@lexical/code-shiki@9425

@lexical/compiler

npm i https://pkg.pr.new/@lexical/compiler@9425

@lexical/devtools-core

npm i https://pkg.pr.new/@lexical/devtools-core@9425

@lexical/dragon

npm i https://pkg.pr.new/@lexical/dragon@9425

@lexical/eslint-plugin

npm i https://pkg.pr.new/@lexical/eslint-plugin@9425

@lexical/extension

npm i https://pkg.pr.new/@lexical/extension@9425

@lexical/fast-check

npm i https://pkg.pr.new/@lexical/fast-check@9425

@lexical/file

npm i https://pkg.pr.new/@lexical/file@9425

@lexical/hashtag

npm i https://pkg.pr.new/@lexical/hashtag@9425

@lexical/headless

npm i https://pkg.pr.new/@lexical/headless@9425

@lexical/history

npm i https://pkg.pr.new/@lexical/history@9425

@lexical/html

npm i https://pkg.pr.new/@lexical/html@9425

@lexical/internal

npm i https://pkg.pr.new/@lexical/internal@9425

@lexical/link

npm i https://pkg.pr.new/@lexical/link@9425

@lexical/list

npm i https://pkg.pr.new/@lexical/list@9425

@lexical/mark

npm i https://pkg.pr.new/@lexical/mark@9425

@lexical/markdown

npm i https://pkg.pr.new/@lexical/markdown@9425

@lexical/mdast

npm i https://pkg.pr.new/@lexical/mdast@9425

@lexical/offset

npm i https://pkg.pr.new/@lexical/offset@9425

@lexical/overflow

npm i https://pkg.pr.new/@lexical/overflow@9425

@lexical/plain-text

npm i https://pkg.pr.new/@lexical/plain-text@9425

@lexical/react

npm i https://pkg.pr.new/@lexical/react@9425

@lexical/rich-text

npm i https://pkg.pr.new/@lexical/rich-text@9425

@lexical/selection

npm i https://pkg.pr.new/@lexical/selection@9425

@lexical/table

npm i https://pkg.pr.new/@lexical/table@9425

@lexical/tailwind

npm i https://pkg.pr.new/@lexical/tailwind@9425

@lexical/text

npm i https://pkg.pr.new/@lexical/text@9425

@lexical/utils

npm i https://pkg.pr.new/@lexical/utils@9425

@lexical/y

npm i https://pkg.pr.new/@lexical/y@9425

@lexical/yjs

npm i https://pkg.pr.new/@lexical/yjs@9425

commit: 81feabf

@rootnode-bot rootnode-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for tracking this down. The fix does what #5016 asks: with a real click into another input and real keystrokes while AutoLinkPlugin re-renders, focus stays in the input on this branch in both Chromium and Firefox. On main, focus jumps to the editor after the first key.

One regression to fix before merging

packages/lexical/src/LexicalUtils.ts:773: the skip tags are passed through the tag option, but update tags belong to the pending editor state, not to a single editor.update() call. Any later editor.update() in the same tick joins that batch (see packages/lexical-website/docs/concepts/updates.mdx) and inherits SKIP_DOM_SELECTION_TAG, so $commitPendingUpdates skips $updateDOMSelection for the whole batch. The description covers the order where the focus request comes first, but the reverse order breaks:

Browser test, prepopulated hello world, editor not focused main this PR
<AutoLinkPlugin/> then <AutoFocusPlugin/> caret at 11 (end) caret at 0
<HashtagPlugin/> then <AutoFocusPlugin/> caret at 11 caret at 0
<AutoLinkPlugin/> then a plugin whose effect calls editor.focus() focused not focused
editor.registerNodeTransform(TextNode, ...) then editor.focus() focused not focused

The Backwards Compatibility section of AGENTS.md asks that existing API behaviour stay intact, and nothing documents a required plugin order inside LexicalComposer. One way to keep the skip scoped to the re-validation is to commit it on its own when the editor is unfocused ({discrete: true, tag: [...]}), so that later updates start a fresh batch. That variant keeps the four new tests passing, at the cost of running the transform synchronously at registration. Deciding at commit time is another option.

Suggestions (optional, non-blocking)

  • Add a test that orders the focus request after the registration; it would have caught the regression above. The Testing Strategy section of AGENTS.md puts behaviour that depends on a real selection engine in browser tests (packages/**/__tests__/browser/**). The current tests place the DOM selection by hand in jsdom (Issue5016Repro.test.ts:89, LexicalAutoLinkPlugin.test.tsx:110). That matches Firefox but not Chromium, where the selection leaves the editor when the input is clicked.
  • The Test plan's After section describes the results; the Commits and Pull Requests section of AGENTS.md asks for the actual pasted output.

Comment thread packages/lexical/src/LexicalUtils.ts Resolved
Vedthakar and others added 3 commits November 16, 2026 18:38
… an unfocused editor

registerNodeTransform marks existing nodes of that type dirty so the new
transform runs on them. That update restored the DOM selection, which
focused the editor root, so a plugin re-registering its transforms (eg.
AutoLinkPlugin with inline matchers) took focus from another input. When
the editor does not have focus, also tag that update with
SKIP_DOM_SELECTION_TAG and SKIP_SELECTION_FOCUS_TAG.

Closes meta#5016

Signed-off-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com>
Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s the DOM selection

Update tags last for the whole batch, so tagging the re-validation with
SKIP_DOM_SELECTION_TAG also dropped a focus request or selection change
made later in the same tick (eg. AutoFocusPlugin mounted after
AutoLinkPlugin). Tag the re-validation with an internal tag, and skip
the DOM selection at commit only if the selection did not change in the
batch.

Add browser tests for both orders.

Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@Vedthakar

Copy link
Copy Markdown
Contributor Author

Thanks for the review, that was a real regression. Fixed in the latest push by deciding at commit time, the second option you mention:

  • The re-validation is tagged with an internal tag instead of SKIP_DOM_SELECTION_TAG. At commit, the DOM selection is skipped only if the selection did not change in the batch (not dirty, and equal to the current one). editor.focus() and any later selection change in the same tick mark the selection dirty, so they are applied as on main.
  • I preferred this to discrete: true because that would commit synchronously once per transform registration while the editor is unfocused, which is the usual state when plugins mount.
  • Added browser tests (Issue5016TransformFocus.test.ts) with a real click into another input: registration alone leaves focus in the input; registration followed by editor.focus() focuses the editor; registration followed by a selection change applies it. The first fails on main, the other two fail with the previous version of this PR.

Also rebased onto main and added a changeset. Run in Chromium only.

— Ved Thakar

@rootnode-bot rootnode-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for working on #5016. This looks good to me. Checking at commit time in packages/lexical/src/LexicalUpdates.ts stops a transform registration from pulling focus into an unfocused editor. editor.focus(), AutoFocusPlugin and a selection set in the same batch still apply, which addresses the earlier concern about skip tags leaking into batched updates. The new browser test runs in the real selection engine, and its two companion cases cover that batching path.

The suggestions below are optional and do not block the merge:

  1. Add a Verify: paragraph to the changeset (.changeset/transform-registration-keeps-focus.md). CONTRIBUTING.md ("Changesets") and .changeset/README.md ask a fix that changes observable editing behaviour to carry one, for example:
    Verify: with a plugin that re-registers its transforms on render (AutoLinkPlugin with an inline matchers array), typing in another input keeps focus in that input; editor.focus() and AutoFocusPlugin still focus the editor.
  2. Update the description. "What this changes" still says SKIP_DOM_SELECTION_TAG and SKIP_SELECTION_FOCUS_TAG are added to the update options, but the branch now uses the internal REVALIDATE_UNFOCUSED_TAG and decides at commit time. AGENTS.md ("Commits and Pull Requests") asks the Test plan to paste the real Before and After output. Please add packages/lexical/src/__tests__/browser/Issue5016TransformFocus.test.ts with its output, since the description becomes the squash commit.
  3. Transforms that move the caret (LexicalUpdates.ts:822-827). When the newly registered transform itself moves the caret, Point.set marks the selection dirty and the editor still takes focus. One example is the first registerLexicalHashtag over hello #world with the caret at the end while another input is focused. This matches main and the REVALIDATE_UNFOCUSED_TAG JSDoc, so it is not a regression. A sentence in the changeset, or a follow-up that tells transform-driven selection moves apart from requested ones, would set expectations.
  4. Browser coverage for the React case. The AutoLinkPlugin test is jsdom-only and recreates the browser's retained selection with setBaseAndExtent. A version under packages/lexical-react/src/__tests__/browser/ that uses a controlled input and userEvent.keyboard would cover the issue's setup end to end.

Automated review (maintainer link)

'lexical': patch
---

[fix] Registering a node transform, as `AutoLinkPlugin` does when it re-renders, no longer moves focus into an editor that does not have it

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Optional: CONTRIBUTING.md ("Changesets") and .changeset/README.md ask a fix that changes observable editing behaviour for a Verify: paragraph, so it is listed under "Behavior changes". For example:

Verify: with a plugin that re-registers its transforms on render (AutoLinkPlugin with an inline matchers array), typing in another input keeps focus in that input; editor.focus() and AutoFocusPlugin still focus the editor.

!(
tags.has(REVALIDATE_UNFOCUSED_TAG) &&
pendingSelection !== null &&
!pendingSelection.dirty &&

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Non-blocking: transforms the new registration runs can set dirty here themselves. A transform that splits or replaces the caret's text node goes through Point.set, which marks the selection dirty. For example, the first registerLexicalHashtag over hello #world with the caret at the end moves the caret to #world@6, and the editor still takes focus from a focused outside input. That matches main and the tag's JSDoc, so it is not a regression. It may be worth a line in the changeset, or a follow-up that tells transform-driven moves apart from requested ones.

This branch was successfully deployed

2 active deployments
Preview – lexical — 81feabf5 Deployed Nov 16, 2026 by vercel[bot]
Preview – lexical-playground — 81feabf5 Deployed Nov 16, 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 Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Editor steals focus to other components when AutoLinkPlugin is present

1 participant