Repository navigation
[lexical][lexical-rich-text] Bug Fix: Place block cursor between decorators and shadow roots - #8758
Conversation
…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.
…k cursor navigation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
potatowagon
left a comment
There was a problem hiding this comment.
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 existingneedsBlockCursor()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 callsselectStart()/selectEnd()depending on direction. Forward direction readschild = parent.getChildAtIndex(offset)(the node AT offset), backward readsoffset - 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 oldselectPrevious()/selectNext()calls with shadow-root-aware placement.
2. Edge cases ✅
- Nested shadow roots: the walk-up loop in
$tryExitShadowRootToBlockCursorhandles 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.shiftKeyin all command handlers, preserving selection-extend behavior. - RTL: handled via
$isParentRTL()calls in LEFT/RIGHT handlers that flip theisBackwarddirection.
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
buildEditorFromExtensionswithTestDecoratorNodeandTestShadowRootNode— proper unit-level testing without browser DOM.
4. Duplication concern (minor)
$needsBlockCursorBeside()in rich-text andneedsBlockCursor()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
$getAdjacentNodelogic, 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
left a comment
There was a problem hiding this comment.
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
…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.
|
Done — rewrote |
etrepum
left a comment
There was a problem hiding this comment.
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
|
Thanks for the refactor — much leaner with $needsBlockCursorBeside exported and the caret simplification. |
Description
needsBlockCursoronly consideredDecoratorNodeand non-emptyElementNode, 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 returntruefor 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 viaselectStart()/selectEnd().$exitNodeSelectionToward— from aNodeSelectionon 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 --noEmitclean.