#9279·lexical

Bug: [lexical] A caret in a named slot or another editable island is never scrolled into view

Author: rinarakakiCreated Sep 30, 2026Updated Sep 30, 2026

Lexical version: 0.52.0 (the current latest), Chromium 153 on Linux. It reproduces on playground.lexical.dev (0.52.0+dev.esm), and the code is unchanged on main at f0fefed.

Steps To Reproduce

  1. Open https://playground.lexical.dev/.
  2. Press Enter until the caret reaches the bottom of the window. The page scrolls to keep the caret in view.
  3. Type /pull quote and choose Pull Quote.
  4. Click at the end of the quote's text, and scroll the page so that this line sits at the bottom of the window.
  5. Press Enter five or six times.

Link to code example: this moves the caret into a named slot in an editor update, as a key handler or an app might:

typescript
import {registerRichText} from '@lexical/rich-text';
import {
  $create,
  $createParagraphNode,
  $createTextNode,
  $getRoot,
  $getSlot,
  $setSlot,
  createEditor,
  DecoratorNode,
  ElementNode,
} from 'lexical';

// A block decorator with one named slot, like the playground's PullQuoteNode.
class QuoteHostNode extends DecoratorNode<null> {
  $config() {
    return this.config('quote-host', {extends: DecoratorNode, slots: ['quote']});
  }
  createDOM() {
    return document.createElement('div');
  }
  updateDOM() {
    return false;
  }
  isInline() {
    return false;
  }
  decorate() {
    return null;
  }
}

// A multi-paragraph slot value, like the playground's SlotContainerNode.
class SlotContainerNode extends ElementNode {
  $config() {
    return this.config('slot-container', {extends: ElementNode});
  }
  createDOM() {
    return document.createElement('div');
  }
  updateDOM() {
    return false;
  }
  isShadowRoot() {
    return true;
  }
}

const rootElement = document.createElement('div');
rootElement.contentEditable = 'true';
rootElement.style.cssText = 'height: 150px; overflow-y: auto';
document.body.append(rootElement);

const editor = createEditor({
  nodes: [QuoteHostNode, SlotContainerNode],
  onError: (error) => {
    throw error;
  },
  // Reveal the slot's container in place, inside the host's DOM.
  dom: {
    $getSlotTargetElement: (node, _name, hostDom) =>
      node instanceof QuoteHostNode ? hostDom : null,
  },
});
editor.setRootElement(rootElement);
registerRichText(editor);

editor.update(
  () => {
    const lines = Array.from({length: 30}, (_, i) =>
      $createParagraphNode().append($createTextNode(`line ${i + 1}`)),
    );
    const host = $create(QuoteHostNode);
    $setSlot(
      host,
      'quote',
      $create(SlotContainerNode).append(
        $createParagraphNode().append($createTextNode('quoted')),
      ),
    );
    $getRoot().append(...lines, host);
    lines[0].selectStart();
  },
  {discrete: true},
);
rootElement.focus();

// 1. The caret into the root's own text far below: the root scrolls to it.
editor.update(
  () => {
    $getRoot().getChildAtIndex<ElementNode>(29)!.selectEnd();
  },
  {discrete: true},
);
console.log('root text:', rootElement.scrollTop); // > 0

// 2. The caret into the slot's text far below: nothing scrolls.
rootElement.scrollTop = 0;
editor.update(
  () => {
    const host = $getRoot().getLastChild<QuoteHostNode>()!;
    const container = $getSlot(host, 'quote') as SlotContainerNode;
    container.getFirstChild<ElementNode>()!.selectEnd();
  },
  {discrete: true},
);
console.log('slot text:', rootElement.scrollTop); // 0
console.log(document.activeElement); // <div data-lexical-slot="quote" contenteditable="true">

The current behavior

In step 5 the caret moves below the bottom of the window, and the page doesn't scroll. The same happens in a Review's body (/review), which is an island marked with $markSlotEditable (ReviewNode.tsx#L97) rather than a named slot. In the code example, the root scrolls to a caret in its own text, but not to one in the slot.

Here is why:

  1. $updateDOMSelection scrolls a collapsed selection into view only while the root element itself has the focus (LexicalSelection.ts#L4080-L4090):

    typescript
    rootElement === getActiveElement(rootElement)
  2. A slot container in a decorator host, or in a contentEditable=false shell, is an editing host of its own. $applySlotEditable makes it contentEditable="true" through $markSlotEditable (LexicalReconciler.ts#L493-L503, LexicalUtils.ts#L3318-L3329).

  3. Setting the DOM selection inside the container focuses the container, as the comment above that check says it can. document.activeElement is then the container, never the root, so the check fails and nothing scrolls.

So Enter in a slot, and every caret move Lexical makes into one, leaves the caret wherever it lands, even off screen.

The expected behavior

A caret in a slot, or in any other editable island of the editor, is scrolled into view the way a caret in the root is.

The root and every island that $markSlotEditable marks carry the editor on __lexicalEditor (LexicalEvents.ts#L2068, LexicalUtils.ts#L3324-L3325). So the check can accept any focused editing host of this editor inside the root:

diff
--- a/packages/lexical/src/LexicalUtils.ts
+++ b/packages/lexical/src/LexicalUtils.ts
@@ -317,6 +317,25 @@
   return node ? node.__lexicalEditor : null;
 }
 
+/**
+ * @internal
+ *
+ * Whether `element` is one of `editor`'s own editing hosts: its root element,
+ * or an editable island inside it, such as a named-slot container or any other
+ * element marked with {@link $markSlotEditable}.
+ */
+export function isEditorFocusHost(
+  editor: LexicalEditor,
+  rootElement: HTMLElement,
+  element: Element | null,
+): boolean {
+  return (
+    element !== null &&
+    rootElement.contains(element) &&
+    getEditorPropertyFromDOMNode(element) === editor
+  );
+}
+
 /** Returns the text direction ('ltr' or 'rtl') of the given string, or null if it contains no strong directional characters. */
 export function getTextDirection(text: string): 'ltr' | 'rtl' | null {
   if (RTL_REGEX.test(text)) {
--- a/packages/lexical/src/LexicalSelection.ts
+++ b/packages/lexical/src/LexicalSelection.ts
@@ -114,6 +114,7 @@
   isDOMDocumentNode,
   isDOMShadowRoot,
   isDOMTextNode,
+  isEditorFocusHost,
   isHTMLElement,
   isSelectionWithinEditor,
   removeDOMBlockCursorElement,
@@ -4085,8 +4086,9 @@
     // restore / selection mutation above, which can become stale (e.g. when
     // setting the DOM selection focuses the contentEditable as a side effect).
     // Shallow is sufficient here for the same reason as the Firefox branch
-    // above: the equality check doesn't cross the shadow boundary.
-    rootElement === getActiveElement(rootElement)
+    // above: the check doesn't cross the shadow boundary. A caret in a named
+    // slot or another editable island focuses that island, not the root.
+    isEditorFocusHost(editor, rootElement, getActiveElement(rootElement))
   ) {
     const selectionTarget: null | Range | HTMLElement | Text =
       $isRangeSelection(nextSelection) &&

I checked this against a copy of 0.52.0's Lexical.dev.js patched the same way, in Chromium. A caret moved in an update into a named slot, or into an island marked with $markSlotEditable, is scrolled into view, and so is the caret after Enter in a slot. A caret in the root's own text scrolls as before. The rootElement.contains() check keeps the scroll walk valid, since it starts at the root; a slot container mounted outside the root still isn't scrolled.

Impact of fix

Named slots and $markSlotEditable islands are how Lexical offers editable fields inside a node without nested editors. Wherever such a field is an island of its own (a slot in a decorator host or in a contentEditable=false shell, like the playground's Pull Quote and Review), Enter near the bottom of the window, or a program moving the caret in, leaves the caret out of sight until the user scrolls by hand. We hit it in an editor with inline math built on Lexical, whose formulas have slots that Tab moves the caret between.