Skip to content

[lexical][lexical-rich-text] Bug Fix: Place block cursor between decorators and shadow roots - #8758

Merged
etrepum merged 10 commits into
meta:mainfrom
mayrang:fix/8736-block-cursor-shadow-root
Jun 28, 2026
Merged

etrepum merged 10 commits into
meta:mainfrom
mayrang:fix/8736-block-cursor-shadow-root

Conversation

@mayrang

@mayrang mayrang commented Jun 27, 2026

Copy link
Copy Markdown
Collaborator

Description

needsBlockCursor only considered DecoratorNode and non-empty ElementNode, so the block cursor never rendered beside a shadow root. Arrow-key navigation jumped straight into or over shadow roots with no way to place the caret in between.

Fix

needsBlockCursor (rendering, in LexicalUtils.ts) and its navigation mirror $needsBlockCursorBeside (in lexical-rich-text) now return true for shadow roots whose parent is not itself a shadow root. The parent check avoids rendering block cursors between sibling shadow roots inside a shadow root parent (e.g. layout-items inside a layout-container, where CSS grid/flex would break).

Four navigation helpers handle the arrow-key transitions:

  • $tryBlockCursorShadowRootNavigation — combines exit and enter into one call, used by all four arrow handlers.
  • $tryExitShadowRootToBlockCursor — when the caret is at the edge of a shadow root and the adjacent sibling needs a block cursor, moves to the block cursor position. Walks up through nested shadow roots.
  • $tryEnterFromBlockCursor — from a block cursor position, enters the adjacent shadow root via selectStart()/selectEnd().
  • $exitNodeSelectionToward — from a NodeSelection on a decorator, stops at the block cursor beside an adjacent shadow root instead of jumping in.

Closes #8736

Test plan

  • pnpm vitest run --project unit — 3354 pass, no regression.
  • pnpm tsc --noEmit clean.
  • Manual playground (Chrome, Firefox, Safari) with layout-container (shadow root) between two horizontal rules (decorators):
    • ArrowDown from decorator → block cursor renders between decorator and layout → ArrowDown enters layout.
    • ArrowRight through layout text → exits to block cursor after layout → ArrowRight reaches next decorator.
    • ArrowLeft/ArrowUp reverse path works symmetrically.
    • Sibling layout-items inside layout-container: no spurious block cursor between columns.

mayrang added 4 commits June 27, 2026 13:23
…rators and shadow roots

Block cursor could not be placed between a DecoratorNode and an adjacent
shadow root (e.g. page-break next to a collapsible container), making it
impossible to position the caret there with keyboard navigation.

- Extend `needsBlockCursor` to include non-inline shadow root nodes, so
  the block cursor element renders beside them
- Skip shadow roots whose parent is also a shadow root (e.g. layout-items
  inside a layout-container) to avoid broken block cursor rendering in
  CSS grid/flex layouts
- Add `$tryExitShadowRootToBlockCursor` for ArrowLeft/Right to exit a
  shadow root when the adjacent sibling needs a block cursor
- Add `$tryEnterFromBlockCursor` for ArrowLeft/Right to enter a shadow
  root from a block cursor position
- Add `$exitNodeSelectionToward` so ArrowUp/Down/Left/Right from a
  NodeSelection (decorator) stops at the block cursor instead of jumping
  into the shadow root

Closes meta#8736
Unit tests for the four new navigation helpers:
- $exitNodeSelectionToward: decorator NodeSelection → block cursor
- $tryEnterFromBlockCursor: block cursor → shadow root entry
- $tryExitShadowRootToBlockCursor: shadow root edge → block cursor
- nested shadow root walk-up exit
- no block cursor between sibling shadow roots inside a shadow root parent
- full round-trip decorator → block cursor → shadow root → exit
…dow root navigation

Extract $tryBlockCursorShadowRootNavigation helper, add shadow root
handling to ArrowUp/Down, reorder cheap conditions first, convert
$exitNodeSelectionToward tests to test.for, fix test type narrowing.
@vercel

vercel Bot commented Jun 27, 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 Jun 28, 2026 5:40am
lexical-playground Ready Ready Preview Jun 28, 2026 5:40am

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 Jun 27, 2026
@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Jun 27, 2026

@potatowagon potatowagon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed by Navi (Tater Thoughts Bobblehead) on behalf of @potatowagon.

PR #8758: Block cursor between decorators and shadow roots

Summary: This PR fixes keyboard navigation (arrow keys) around shadow root elements when adjacent to block decorator nodes. It introduces block cursor placement between decorators and shadow roots, allowing users to navigate in/out of shadow roots (e.g., layout containers) via arrow keys.

What I checked:

1. Logic correctness ✅

  • $needsBlockCursorBeside() correctly mirrors the existing needsBlockCursor() in LexicalUtils.ts with the added shadow root awareness. The key guard — !($isElementNode(parent) && parent.isShadowRoot()) — prevents spurious block cursors between sibling shadow roots inside a shadow root parent (e.g., layout items within a layout container). This is the right behavior.
  • $tryEnterFromBlockCursor() correctly resolves the child at the cursor offset and calls selectStart()/selectEnd() depending on direction. Forward direction reads child = parent.getChildAtIndex(offset) (the node AT offset), backward reads offset - 1 (the node BEFORE offset) — this is correct for how element offsets work in Lexical.
  • $tryExitShadowRootToBlockCursor() walks up from the focus node to find the nearest shadow root ancestor, verifies the cursor is at the absolute start/end edge (walking the first/last-child chain), then traverses parent shadow roots until finding a sibling that $needsBlockCursorBeside() returns true for. The upward traversal handles nested shadow roots correctly.
  • $exitNodeSelectionToward() replaces the old selectPrevious()/selectNext() calls with shadow-root-aware placement.

2. Edge cases ✅

  • Nested shadow roots: the walk-up loop in $tryExitShadowRootToBlockCursor handles multiple nesting levels by continuing to walk up through parent shadow roots when at the edge.
  • Sibling shadow roots inside a shadow root parent: explicitly tested — the code does NOT produce a block cursor there (correct, since the parent is a shadow root).
  • Non-collapsed selections and shift+arrow: guarded by selection.isCollapsed() checks and !event.shiftKey in all command handlers, preserving selection-extend behavior.
  • RTL: handled via $isParentRTL() calls in LEFT/RIGHT handlers that flip the isBackward direction.

3. Test coverage ✅

  • 569-line comprehensive test file covering: decorator→block-cursor, block-cursor→enter-shadow-root, shadow-root→exit-to-block-cursor, nested shadow root traversal, sibling shadow roots (no spurious cursor), and full round-trip navigation.
  • Tests use buildEditorFromExtensions with TestDecoratorNode and TestShadowRootNode — proper unit-level testing without browser DOM.

4. Duplication concern (minor) ⚠️

  • $needsBlockCursorBeside() in rich-text and needsBlockCursor() in LexicalUtils.ts are intentionally kept in sync (comments note this). They serve different contexts: the Utils version is for DOM rendering of visual block cursor elements, while the rich-text version is for keyboard navigation logic. The duplication is documented and reasonable, though a future refactor could export a shared helper.

5. CI status ⚠️

  • Core tests (unit 22.x, 24.x), integrity, browser tests (all platforms), integration tests, CLA, Vercel deploys: ALL PASSING.
  • Several e2e tests are still pending (mac, collab, windows variants) — these are still running. No failures detected so far.

6. www compatibility ✅

  • No exports are removed or renamed. This is additive behavior (new functions, new code paths in existing command handlers). The arrow-key handlers add early-return paths before the existing $getAdjacentNode logic, so existing behavior for non-shadow-root scenarios is preserved. No breaking changes for internal www consumers.

Assessment: LGTM — ready to approve once e2e tests complete.

The logic is well-reasoned, handles edge cases properly, and includes excellent test coverage. The PR correctly identifies that shadow roots need special handling for block cursor navigation (they were being skipped entirely before). The implementation is clean and follows existing patterns in the codebase.

@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.

The caret APIs should make most of this a bit simpler, using them properly should let you do this without having to maintain all of the branching for forward/backward

mayrang added 2 commits June 28, 2026 03:40
…or walk

The Caret API refactor started the shadow root ancestor walk from
getParentCaret(), skipping the focus node itself. When the focus was
directly on a shadow root element (e.g. LayoutItem), the nearest
shadow root was missed, and the edge check failed because it only
searched descendants without considering focus at the shadow root.
@mayrang

mayrang commented Jun 27, 2026

Copy link
Copy Markdown
Collaborator Author

Done — rewrote $tryExitShadowRootToBlockCursor, $tryEnterFromBlockCursor, and $exitNodeSelectionToward with $caretFromPoint, $getSiblingCaret, $getChildCaret, and $setPointFromCaret.

@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.

I made some additional changes here to export $needsBlockCursorBeside from lexical to avoid code duplication, and to reduce code size by using more of the caret functionality

@mayrang

mayrang commented Jun 28, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks for the refactor — much leaner with $needsBlockCursorBeside exported and the caret simplification.

@etrepum
etrepum added this pull request to the merge queue Jun 28, 2026
Merged via the queue into meta:main with commit 9b4dc14 Jun 28, 2026
40 checks passed
@etrepum etrepum mentioned this pull request Jul 9, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — 0c1d8a28 Deployed Jun 28, 2026 by vercel[bot]
Preview – lexical-playground — 0c1d8a28 Deployed Jun 28, 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.

Bug: The block cursor cannot be placed between the decorator and the shadow root

3 participants