Skip to content

[lexical] Feature: LexicalEditor RootListener and EditableListener can return unregister callbacks#8219

Merged
etrepum merged 5 commits intofacebook:mainfrom
etrepum:listener-nested-register
Mar 14, 2026
Merged

[lexical] Feature: LexicalEditor RootListener and EditableListener can return unregister callbacks#8219
etrepum merged 5 commits intofacebook:mainfrom
etrepum:listener-nested-register

Conversation

@etrepum
Copy link
Collaborator

@etrepum etrepum commented Mar 13, 2026

Description

LexicalEditor.registerRootListener and registerEditableListener can now optionally return an unregister callback that is called before each consecutive change and on unregister.

It's a common pattern to set up additional services that are only available based on the editor state, for example when it is attached to the DOM you may want to set up listeners on the root element or its containing document or window. It should also be easy to correctly clean up those listeners. The current pattern is something like this:

const listener = () => {};
let unregister = () => {};
return mergeRegister(
  editor.registerRootListener((nextRoot) => {
    unregister();
    unregister = () => {};
    if (nextRoot) {
      nextRoot.addEventListener('click', listener);
      unregister = () => nextRoot.removeEventListener('click', listener);
    }
  }),
  () => unregister(),
);

If the root listener is permitted to return an optional unregister directly, this becomes much less convoluted:

const listener = () => {};
return editor.registerRootListener((nextRoot) => {
  if (nextRoot) {
    nextRoot.addEventListener('click', listener);
    return () => nextRoot.removeEventListener('click', listener);
  }
});

The inspiration from this comes from how effect() works with preact signals

If the callback returns a function, this function will be run before the next value update.

Test plan

New unit tests, all existing unit and e2e tests pass with no change.

@vercel
Copy link

vercel bot commented Mar 13, 2026

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

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview, Comment Mar 13, 2026 9:16pm
lexical-playground Ready Ready Preview, Comment Mar 13, 2026 9:16pm

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 Mar 13, 2026
@etrepum etrepum added this pull request to the merge queue Mar 14, 2026
Merged via the queue into facebook:main with commit 446a534 Mar 14, 2026
37 checks passed
levensta pushed a commit to levensta/lexical that referenced this pull request Mar 14, 2026
etrepum added a commit to etrepum/lexical that referenced this pull request Mar 15, 2026
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.

2 participants