Skip to content

[Breaking Change][lexical] Bug Fix: Preserve DOM element when composing on segmented TextNode middle - #8784

Merged
etrepum merged 2 commits into
meta:mainfrom
mayrang:fix/5065-segmented-composition
Jul 5, 2026
Merged

etrepum merged 2 commits into
meta:mainfrom
mayrang:fix/5065-segmented-composition

Conversation

@mayrang

@mayrang mayrang commented Jul 5, 2026 •

Copy link
Copy Markdown
Collaborator

Breaking Change

During IME composition, a segmented TextNode may be temporarily switched to normal mode to preserve the DOM element that the browser's composition tracker is bound to. Subclass transforms or method overrides that assume the node is always in segmented mode should account for this transient state.

Description

IME composition breaks when it starts in the middle of a segmented TextNode (e.g. MentionNode). Typing a few characters, the composition text appears correctly, but the mention's background color bleeds into the composed text and the cursor briefly flashes before the first character on Chrome.

The root cause is in insertText: when the caret is in the middle of a segmented node, the code calls replace() to swap it with a plain TextNode. This destroys the original DOM element and creates a new one — but the browser's composition tracker is bound to the original element, so the replacement breaks the active composition session.

Fix

During composition ($getCompositionKey() !== null), use setMode('normal') instead of replace(). This changes the node's internal mode while keeping the same node key, so the reconciler calls updateDOM on the existing DOM element rather than creating a new one. The browser's composition tracker stays attached and input continues normally.

After composition ends, the node is still a subclass instance (e.g. MentionNode) in normal mode, which means subclass-specific DOM styling (background color, class names from createDOM) persists. A new $cleanupComposedSubclass function replaces the subclass instance with a plain TextNode at composition end, clearing the stale styling. This runs from all four browser composition-end paths:

  1. Chrome: COMPOSITION_END_COMMAND → $onCompositionEndImpl
  2. Firefox: onInput defer → $onCompositionEndImpl
  3. Safari non-Backspace: insertFromComposition in $handleBeforeInput
  4. Safari Backspace: $handleKeyDown → $onCompositionEndImpl

Closes #5065

Test plan

  • pnpm vitest run --project unit — all pass (3 new tests in LexicalSelection.test.ts).
    • "insertText during composition preserves node key" — verifies key preservation and mode change.
    • "insertText without composition replaces segmented node" — verifies existing replace behavior unchanged.
    • "insertText during composition preserves format and style" — verifies format/style carry over and node stays attached.
    • Tests 1 and 3 fail on pre-fix code, confirming they guard the new behavior.
  • Manual playground (Chrome, Firefox, Safari):
    • Type @ to insert a MentionNode, place cursor in the middle of the mention text, start IME composition (Korean/Japanese/Chinese).
    • Chrome: no cursor flash before first character, mention background clears after composition ends.
    • Firefox: composition works, background clears on commit.
    • Safari: composition works, background clears on commit (including when ending with Return or Backspace).
  • tsc / prettier / eslint clean.

…extNode middle (meta#5065)

During IME composition in the middle of a segmented TextNode, use
setMode('normal') instead of replace() to keep the same node key and
DOM element, preventing the browser's composition tracker from breaking.
At composition end, replace TextNode subclasses with plain TextNode to
clear stale subclass-specific styling. Covers Chrome, Firefox, and
Safari composition-end paths.
@vercel

vercel Bot commented Jul 5, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview Jul 5, 2026 10:54am
lexical-playground Ready Ready Preview Jul 5, 2026 10:54am

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 5, 2026
@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Jul 5, 2026

@etrepum etrepum left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is a clever solution! I think it could have some edge cases depending on what that specific node is doing (e.g. transforms or overrides of some of the relevant methods), so maybe we should try and clearly document that any segmented TextNode can temporarily be switched to normal mode and have its text changed during IME composition? I can't really think of another solution that would be very clean without specific reconciler support, but since this should work in most cases as-is I think documentation is probably better than additional complexity.

Addresses reviewer feedback: document that segmented TextNode subclasses
may temporarily enter normal mode during active composition.
@mayrang

mayrang commented Jul 5, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks! Added documentation in 9063d63 — a JSDoc note on TextNode.setMode() about the transient mode switch during composition, and an inline comment at the guard site in insertText explaining the lifecycle (setMode('normal') → composition → $cleanupComposedSubclass replaces with plain TextNode).

@etrepum etrepum changed the title [lexical] Bug Fix: Preserve DOM element when composing on segmented TextNode middle [Breaking Change][lexical] Bug Fix: Preserve DOM element when composing on segmented TextNode middle Jul 5, 2026
@etrepum
etrepum added this pull request to the merge queue Jul 5, 2026
Merged via the queue into meta:main with commit 0b2c6ab Jul 5, 2026
40 checks passed
@etrepum etrepum mentioned this pull request Jul 9, 2026
AlessioGr added a commit to payloadcms/payload that referenced this pull request Sep 11, 2026
Upgrades `lexical` and every installed `@lexical/*` package from
`0.41.0` to `0.50.0` for Payload 3.x. Updates the dependency pins,
lockfile, and `lexicalTargetVersion`.

Also exposes the 15 React entry points added on main in
[#17554](#17554) through
`@payloadcms/richtext-lexical/lexical/react/*`, so custom features can
use these APIs through Payload.

## Breaking changes

**No application changes or data migration are needed when using
Payload's built-in rich text features.**

If you maintain custom rich text features, check that they still compile
and that custom content loads, copies, and pastes correctly. Lexical
removed some older APIs, tightened TypeScript types, and changed how
custom nodes are loaded and copied. Tests that inspect the editor's HTML
may also need updated selectors or snapshots because Lexical adds some
internal markup.

**Do not install `lexical` or `@lexical/*` yourself for use with
Payload.** Remove any direct dependencies you added for the editor and
use Payload's re-exports from `@payloadcms/richtext-lexical/lexical` and
`@payloadcms/richtext-lexical/lexical/*`. Payload supplies the matching
versions; mixing versions can break the editor. This is the existing
[custom feature
requirement](https://payloadcms.com/docs/rich-text/custom-features).

The 3.x Markdown implementation and converter defaults are preserved.
This does not backport the v4 Markdown API changes.

## Breaking changes and Payload migrations

Reviewed
[0.42.0](https://github.com/facebook/lexical/releases/tag/v0.42.0),
[0.43.0](https://github.com/facebook/lexical/releases/tag/v0.43.0),
[0.44.0](https://github.com/facebook/lexical/releases/tag/v0.44.0),
[0.45.0](https://github.com/facebook/lexical/releases/tag/v0.45.0),
[0.46.0](https://github.com/facebook/lexical/releases/tag/v0.46.0),
[0.47.0](https://github.com/facebook/lexical/releases/tag/v0.47.0),
[0.48.0](https://github.com/facebook/lexical/releases/tag/v0.48.0),
[0.49.0](https://github.com/facebook/lexical/releases/tag/v0.49.0),
[0.50.0](https://github.com/facebook/lexical/releases/tag/v0.50.0).
Listed below are breaking API/type changes and behavior changes
explicitly identified as breaking in the release notes. Deprecations and
routine bug fixes are omitted. **Yes** means we had to change Payload
code; **No** means no Payload migration was required.

| Release | Breaking change | Required migration in Payload | Changes
made |
| ------- |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| ----------------------------- |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
| 0.42 | [Experimental `hasFitNestedTables` moves out of the table
package into the
playground](meta/lexical#8210). | No | - |
| 0.42 | [The deprecated context-menu plugin is
removed](meta/lexical#8199). | Yes | Removed
the invalid `LexicalContextMenuPlugin` proxy and package export. Custom
consumers must use
`@payloadcms/richtext-lexical/lexical/react/LexicalNodeContextMenuPlugin`
and adapt to its node-context-menu API. |
| 0.43 | [Nested editors delegate asynchronously to their
parent](meta/lexical#8308), restoring the
behavior from before 0.42.0. | No | - |
| 0.45 | [`$getReconciledDirection` now inherits direction through
shadow-root nodes](meta/lexical#8479). | No
| - |
| 0.45 | [Backspace at the start of a non-empty block preserves the
current block type and
formatting](meta/lexical#8493). | No | - |
| 0.45 | [Moving nodes between parents adjusts selection
offsets](meta/lexical#8501). | No | - |
| 0.45 | [Markdown shortcuts can trigger when IME composition
commits](meta/lexical#8503). | No | - |
| 0.45 | [Rich/plain text automatically removes empty inline
elements](meta/lexical#8497). | No | - |
| 0.45 | [Triple-click selection normalization moves from core into
rich/plain text
registration](meta/lexical#8520). Editors
bypassing that registration must add
`NormalizeTripleClickSelectionExtension`. | No | - |
| 0.45 | [`DOMSlot` and the DOM rendering override APIs are
generalized](meta/lexical#8519); playground
`AutocompleteNode` is replaced by `VisibleLineBreakExtension`. | No | -
|
| 0.46 | [`static importJSON` widens its parameter to
`SerializedLexicalNode & Record<string,
unknown>`](#17554). Overrides
using serialized interfaces can fail TypeScript checks. | No | - |
| 0.46 | [Node lookups and traversal no longer infer unchecked subclass
types](meta/lexical#8661). Explicit
unchecked traversal generics are deprecated. | Yes | Removed the upload
drawer’s assumption that a node lookup returns `UploadNode`. Added
`$isUploadNode` before reading or updating upload data. |
| 0.46 | [DOM lookups of the editor root return `RootNode` instead of
`null`](meta/lexical#8588).
Selection-capture handling is also generalized beyond decorators. | No |
- |
| 0.46 | [`insertNodes` preserves leading line breaks, and managed line
breaks receive a DOM
marker](meta/lexical#8615). | No | - |
| 0.46 | [Experimental DOM-import rules register through node
extensions](meta/lexical#8662); the separate
per-package import extensions become deprecated aliases. | No | - |
| 0.46 | [Experimental named slots change the Yjs sync
signature](meta/lexical#8603):
`syncLexicalUpdateToYjsV2__EXPERIMENTAL` gains a `dirtyLeaves`
parameter. | No | - |
| 0.46 | [Copying an element through `NodeSelection` includes its
children](meta/lexical#8603), instead of
copying an empty shell. | No | - |
| 0.46 | [Previously deprecated exports are
removed](meta/lexical#8704): `toggleLink`,
`insertList`, `removeList`, and ContentEditable’s `Props` alias.
Replacements are `$toggleLink`, `$insertList`, `$removeList`, and
`ContentEditableProps`. | No | - |
| 0.47 | [React 17 support is
removed](meta/lexical#8782); React 18 or
later is required. | No | - |
| 0.47 | [Segmented text temporarily switches to normal mode during IME
composition while keeping its
DOM](meta/lexical#8784). Subclasses must
tolerate that temporary mode. | No | - |
| 0.49 | [Built-in nodes adopt
`$config()`](meta/lexical#8640). Direct
static `importJSON`, `importDOM`, `clone`, and `transform`
implementations change; `__type` becomes readonly and automatic node
construction needs zero-argument defaults. Custom static implementations
remain supported. | No | - |
| 0.49 | [`LexicalCommand<T>` checks its payload type more
strictly](meta/lexical#8877).
`LexicalCommand<unknown>` is no longer a safe catch-all. | Yes | Focus
and blur commands were incorrectly registered with `MouseEvent`. Removed
the explicit types so Lexical infers `FocusEvent`. |
| 0.50 | [Named-slot values cannot be replaced with
`LexicalNode.replace()`](meta/lexical#8938).
Block converters also [skip slot
values](meta/lexical#8905). | No | - |
| 0.50 | [Lexical JSON clipboard copying checks
`excludeFromCopy('clone')`, not
`'html'`](meta/lexical#8995). | No | - |

Also updated the copied table action menu for merged-cell bounds, header
toggles, and merge/unmerge selection, and fixed the upload alignment
test’s image selector. Formatting and diff checks pass for the upload
selector fix.

Verified all 15 new entry points through source and published export
maps with runtime imports and TypeScript checks. ESLint and formatting
checks pass.

## Previous Lexical upgrades

- [#15760](#15760) — 0.35.0 →
0.41.0
- [#13715](#13715) — 0.34.0 →
0.35.0
- [#13622](#13622) — 0.28.0 →
0.34.0

This branch was successfully deployed

2 active deployments
Preview – lexical — 9063d63c Deployed Jul 5, 2026 by vercel[bot]
Preview – lexical-playground — 9063d63c Deployed Jul 5, 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. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Composition breaks when composing on a segmented node

2 participants