Repository navigation
Conversation
…already in the editor
## Description
`useCharacterLimit` derives the remaining-character count only from inside an update listener, and `registerUpdateListener` does not fire on registration:
```ts
useEffect(() => {
let text = editor.read('latest', $rootTextContent);
let lastComputedTextLength = 0;
return mergeRegister(
editor.registerTextContentListener((currentText: string) => { text = currentText; }),
editor.registerUpdateListener(({dirtyLeaves, dirtyElements}) => {
...
remainingCharacters(diff); // <- the only place the count is produced
```
`text` is read from the editor, then never used until something else changes. The consumer seeds its state from a constant, not from the editor:
```tsx
// LexicalCharacterLimitPlugin.tsx
const [remainingCharacters, setRemainingCharacters] = useState(maxLength);
```
So a `CharacterLimitPlugin` mounted over an editor that already has content reports `maxLength` no matter what is in it, and `$wrapOverflowedNodes` never runs, so overflowing text is not wrapped in `OverflowNode`s and not styled. It stays that way until the next edit.
That is the normal way this plugin is reached. The playground renders it behind a setting —
```tsx
{isCharLimit && <CharacterLimitPlugin charset="UTF-16" maxLength={5} />}
```
— so turning "Char Limit" on after typing shows the full budget over text that is already far past it, with no overflow highlight. Type one more character and the number jumps straight to the true value. The same applies to any editor loaded from saved content, and to switching `charset`: `strlen` changes identity, the effect re-subscribes, and the displayed number stays in the old charset's units.
The effect's own code shows the initial pass was intended: `lastComputedTextLength` is tested against `null` in two places,
```ts
(lastComputedTextLength !== null && lastComputedTextLength > maxCharacters);
...
if (lastComputedTextLength === null || textLengthAboveThreshold) {
```
but it is initialized to `0`, so neither branch can ever be taken and the "first pass, always reconcile the overflow wrapping" case is dead. That matters beyond the counter: a document deserialized with `OverflowNode`s under a larger limit never gets them unwrapped.
This restores `null` as the initial value, lifts the listener body into `$updateCharacterLimit()`, and calls it once before subscribing — the shape `useCanShowPlaceholder` in this same directory already uses:
```ts
useLayoutEffect(() => {
function resetCanShowPlaceholder() { ... }
resetCanShowPlaceholder();
return mergeRegister(...);
}, [editor]);
```
The listener behaviour is otherwise untouched; `git diff -w` is 33 insertions / 23 deletions, most of it the extraction. No API change.
## Test plan
New unit test `packages/lexical-react/src/__tests__/unit/LexicalCharacterLimitPlugin.test.tsx` mounts the plugin over an editor that already holds text, which is what toggling the setting on does.
### Before
```
❯ packages/lexical-react/src/__tests__/unit/LexicalCharacterLimitPlugin.test.tsx (3 tests | 3 failed)
× counts the text that is already in the editor when it mounts
AssertionError: expected '5' to be '-6' // Object.is equality
× reports a full budget for an editor that is under the limit
AssertionError: expected '5' to be '3' // Object.is equality
× counts in the charset it was given
AssertionError: expected '5' to be '-4' // Object.is equality
Test Files 1 failed (1)
Tests 3 failed (3)
```
(The counter reads `5` — the seeded `maxLength` — regardless of the content.)
### After
```
Test Files 1 passed (1)
Tests 3 passed (3)
```
Package suite is unchanged, including the existing `useLexicalCharacterLimit` helper tests:
```
$ npx vitest run packages/lexical-react
Test Files 32 passed (32)
Tests 185 passed (185)
```
LeSingh1
requested review from
acywatson,
etrepum,
fantactuka,
ivailop7,
potatowagon and
zurfyx
as code owners
August 9, 2026 05:54
|
@LeSingh1 is attempting to deploy a commit to the Meta Open Source Team on Vercel. A member of the Team first needs to authorize it. |
etrepum
pushed a commit
to etrepum/lexical
that referenced
this pull request
Aug 29, 2026
…ive when their inputs change ## Description A React plugin or hook that reads editor state once, on the first render, and then subscribes for future changes only reports a stale answer for everything that happened before it subscribed — and never re-derives when the prop or argument it was seeded from changes. `registerUpdateListener` and friends do not fire on registration, an effect whose dependency list omits the prop that changed never re-runs, and a value read off the editor at render time (`editor._editable`, `editor.isEditable()`) is not a subscription at all. The same shape shows up on the handler side: a command handler that claims the event when there is nothing to act on, and a state update that clobbers the selection it was supposed to leave alone. Each of the fixes below re-derives (or resubscribes) from the input that actually changed, and none of them changes behaviour when the inputs are stable. - `CollaborationPlugin` (`packages/lexical-react/src/LexicalCollaborationPlugin.tsx`) guarded provider creation with an `isProviderInitialized` ref, so a changed `providerFactory`, `id` or `yjsDocMap` kept the old provider forever. The ref now records the inputs that produced the current provider, so a real change creates a new one while a StrictMode/remount re-run with unchanged inputs still does not. - `useMenuAnchorRef` (`packages/lexical-react/src/shared/LexicalMenu.tsx`) positioned the absolutely-positioned anchor in document coordinates unconditionally. When a `parent` is passed it is normally positioned, so it — not the initial containing block — is the anchor's containing block, and the menu landed at the parent's own offset instead of at the caret. New `getContainingBlockOrigin()` resolves the real origin and the `toAnchorLeft`/ `toAnchorTop` helpers translate every viewport coordinate through it, including the two flip-into-view branches. Fixes meta#6989. - `DraggableBlockPlugin_EXPERIMENTAL` (`packages/lexical-react/src/LexicalDraggableBlockPlugin.tsx`) read `editor._editable` during render, which is not a subscription, so the handle did not appear or disappear on `setEditable()`. It now uses `useLexicalEditable()`. - `LexicalMenu` (`packages/lexical-react/src/shared/LexicalMenu.tsx`) returned `true` from its `KEY_ARROW_DOWN_COMMAND`/`KEY_ARROW_UP_COMMAND` handlers even when `options` was empty — no menu is rendered in that case, so the arrow keys were swallowed and the caret stopped moving. Both handlers now return `false` when there is nothing to move through. - `NodeContextMenuPlugin` (`packages/lexical-react/src/LexicalNodeContextMenuPlugin.tsx`) called `preventDefault()` and mounted its scroll-locking overlay before computing which items `$showOn` allows, so a node with no applicable items suppressed the browser's own context menu and showed an empty one. The filtering moved ahead of `preventDefault()` and the handler returns early when nothing is visible. - `useLexicalIsTextContentEmpty` (`packages/lexical-react/src/useLexicalIsTextContentEmpty.ts`) seeded its state on the first render only, so a new `editor` or a new `trim` kept reporting the previous answer until the next update. The layout effect now re-derives the value before subscribing (and the initial `useState` is lazy, so it no longer reads the editor on every render). - `useLexicalNodeSelection` (`packages/lexical-react/src/useLexicalNodeSelection.ts`) created an empty `NodeSelection` when asked to *de*select a node while the user held a `RangeSelection`, discarding their caret. Deselecting with no `NodeSelection` present is now a no-op. - The playground's `TextFormatFloatingToolbar` (`packages/lexical-playground/src/plugins/FloatingTextFormatToolbarPlugin/index.tsx`) gated its buttons on `editor.isEditable()` read during render, so the toolbar kept its format buttons after `setEditable(false)`. It now uses `useLexicalEditable()`. - `useCharacterLimit` (`packages/lexical-react/src/shared/useCharacterLimit.ts`) only ever counted inside `registerTextContentListener`, which does not fire on registration, so an editor that mounts with content reported the full budget remaining and left overflowing text unwrapped until the next keystroke. The body is extracted into `$updateCharacterLimit()` and called once before subscribing. - `TreeView` (`packages/lexical-react/src/LexicalTreeView.tsx`) seeded `editorCurrentState` on the first render only and its effect had `editor` in the deps but never re-read the state, so a changed `editor` prop kept rendering the previous editor's tree. The effect re-reads `editor.getEditorState()` before subscribing, and is a `useLayoutEffect` to match `useCanShowPlaceholder`/`ContentEditableElement` and avoid a cascading render. ## Test plan Nine new unit test files and one new browser test file, one per fix, each asserting the stale value is re-derived (or that the handler declines to act); `packages/lexical-react/src/__tests__/unit/LexicalCollaborationPlugin.test.tsx` gains a case for the provider swap. Every test fails on `main` with only the test files applied and passes with the fixes. ### Before Source fixes reverted, new tests kept: ``` $ npx vitest run --project unit packages/lexical-react packages/lexical-playground × leaves the native context menu alone when no item is shown 275ms × keeps a range selection when asked to deselect 88ms × hides its format buttons when the editor becomes read-only 206ms × counts the text that is already in the editor when it mounts 95ms × reports a full budget for an editor that is under the limit 8ms × counts in the charset it was given 6ms × follows setEditable 143ms × renders no drag handle for an editor that mounts read-only 17ms × lets ArrowDown through when there are no options 63ms × lets ArrowUp through when there are no options 4ms × provider is replaced when providerFactory changes 7ms × follows a change of the trim argument 19ms × follows a change of the editor 5ms × re-renders when the editor prop changes 14ms Test Files 9 failed | 49 passed (58) Tests 14 failed | 470 passed (484) $ npx vitest run --project browser packages/lexical-react/src/__tests__/browser/useMenuAnchorRefPosition.test.tsx × anchors the menu at the caret when parent is a positioned element 147ms × anchors the menu at the caret when the positioned parent is scrolled 99ms Test Files 1 failed (1) Tests 2 failed | 1 passed (3) ``` ### After ``` $ npx vitest run --project unit packages/lexical-react packages/lexical-playground Test Files 58 passed (58) Tests 484 passed (484) $ npx vitest run --project browser packages/lexical-react/src/__tests__/browser/useMenuAnchorRefPosition.test.tsx Test Files 1 passed (1) Tests 3 passed (3) $ npx tsc --noEmit -p . (clean) ``` Supersedes meta#8965, meta#8968, meta#9012, meta#9017, meta#9022, meta#9024, meta#9026, meta#9030, meta#9033, meta#9040, consolidated per the review feedback on meta#9027 and meta#9035.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
useCharacterLimitderives the remaining-character count only from inside an update listener, andregisterUpdateListenerdoes not fire on registration:textis read from the editor, then never used until something else changes. The consumer seeds its state from a constant, not from the editor:So a
CharacterLimitPluginmounted over an editor that already has content reportsmaxLengthno matter what is in it, and$wrapOverflowedNodesnever runs, so overflowing text is not wrapped inOverflowNodes and not styled. It stays that way until the next edit.That is the normal way this plugin is reached. The playground renders it behind a setting —
— so turning "Char Limit" on after typing shows the full budget over text that is already far past it, with no overflow highlight. Type one more character and the number jumps straight to the true value. The same applies to any editor loaded from saved content, and to switching
charset:strlenchanges identity, the effect re-subscribes, and the displayed number stays in the old charset's units.The effect's own code shows the initial pass was intended:
lastComputedTextLengthis tested againstnullin two places,but it is initialized to
0, so neither branch can ever be taken and the "first pass, always reconcile the overflow wrapping" case is dead. That matters beyond the counter: a document deserialized withOverflowNodes under a larger limit never gets them unwrapped.This restores
nullas the initial value, lifts the listener body into$updateCharacterLimit(), and calls it once before subscribing — the shapeuseCanShowPlaceholderin this same directory already uses:The listener behaviour is otherwise untouched;
git diff -wis 33 insertions / 23 deletions, most of it the extraction. No API change.Test plan
New unit test
packages/lexical-react/src/__tests__/unit/LexicalCharacterLimitPlugin.test.tsxmounts the plugin over an editor that already holds text, which is what toggling the setting on does.Before
(The counter reads
5— the seededmaxLength— regardless of the content.)After
Package suite is unchanged, including the existing
useLexicalCharacterLimithelper tests: