Skip to content

[lexical-rich-text] Bug Fix: Keep block cursor in editor when ArrowUp at start of root (#8886) - #8887

Merged
etrepum merged 1 commit into
meta:mainfrom
etrepum:claude/lexical-issue-8886-3ttish
Jul 29, 2026
Merged

etrepum merged 1 commit into
meta:mainfrom
etrepum:claude/lexical-issue-8886-3ttish

Conversation

@etrepum

@etrepum etrepum commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator

Description

Current behavior: when a non-inline decorator node is the first child of the document and the block cursor sits before it (a collapsed RangeSelection at root offset 0), pressing ArrowUp again fell through to the browser default. The native selection moved outside the editor, with browser-specific breakage — text entry and paste failing in Chrome, Firefox, and Safari.

The ArrowDown handler already guards the bottom boundary with $isSelectionAtEndOfRoot, but the ArrowUp handler had no symmetric top-boundary guard.

New behavior: this adds $isSelectionAtStartOfRoot (a mirror of $isSelectionAtEndOfRoot) and a guard at the top of the ArrowUp range-selection branch that calls preventDefault and returns true. At the top of root neither the native selection nor the lexical selection changes, keeping arrow navigation within the editor.

Closes #8886

Test plan

Before

Added a regression test that dispatches KEY_ARROW_UP_COMMAND from a block cursor before a leading decorator. Before the fix the event was not handled (event.defaultPrevented === false), so the browser default moved the native selection out of the editor.

After

New unit test RichTextBlockCursorRootBoundary.test.ts covers both root boundaries (ArrowUp at start, ArrowDown at end): the command is handled, event.defaultPrevented is true, and the block cursor stays in place. Full rich-text unit suite passes (114/114); prettier, eslint, and tsc are clean.

… at start of root (meta#8886)

## Description

Current behavior: when a non-inline decorator node is the first child of
the document and the block cursor sits before it (a collapsed
RangeSelection at root offset 0), pressing ArrowUp again fell through to
the browser default. The native selection moved outside the editor, with
browser-specific breakage — text entry and paste failing in Chrome,
Firefox, and Safari.

The ArrowDown handler already guards the bottom boundary with
$isSelectionAtEndOfRoot, but the ArrowUp handler had no symmetric
top-boundary guard.

New behavior: this adds $isSelectionAtStartOfRoot (a mirror of
$isSelectionAtEndOfRoot) and a guard at the top of the ArrowUp
range-selection branch that calls preventDefault and returns true. At the
top of root neither the native selection nor the lexical selection
changes, keeping arrow navigation within the editor.

Closes meta#8886

## Test plan

### Before

Added a regression test that dispatches KEY_ARROW_UP_COMMAND from a block
cursor before a leading decorator. Before the fix the event was not
handled (event.defaultPrevented === false), so the browser default moved
the native selection out of the editor.

### After

New unit test RichTextBlockCursorRootBoundary.test.ts covers both root
boundaries (ArrowUp at start, ArrowDown at end): the command is handled,
event.defaultPrevented is true, and the block cursor stays in place. Full
rich-text unit suite passes (114/114); prettier, eslint, and tsc are
clean.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@vercel

vercel Bot commented Jul 29, 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 Jul 29, 2026 4:02am
lexical-playground Ready Ready Preview Jul 29, 2026 4:02am

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 Jul 29, 2026
@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Jul 29, 2026
@etrepum
etrepum marked this pull request as ready for review July 29, 2026 04:17
@etrepum
etrepum added this pull request to the merge queue Jul 29, 2026
Merged via the queue into meta:main with commit 9f99510 Jul 29, 2026
48 checks passed
@etrepum
etrepum deleted the claude/lexical-issue-8886-3ttish branch July 29, 2026 15:58
@potatowagon

Copy link
Copy Markdown
Contributor

Fix looks correct and safe to ship — it's a clean mirror of the existing $isSelectionAtEndOfRoot guard, and since it's an internal helper + guard with no exported API change, there's no www-compat surface to worry about.

One behavioral nuance worth calling out, plus a few test cases I'd add:

shift+ArrowUp

The new guard runs before the shiftKey checks and doesn't test event.shiftKey, so shift+ArrowUp at root:0 now gets swallowed (preventDefault, no selection extension) rather than extending a selection upward. This is intentionally symmetric with the ArrowDown/$isSelectionAtEndOfRoot guard, which also ignores shiftKey — and there's nothing above root:0 to select — so it's defensible. But it's an implicit behavior change, so worth a short comment on why the guard is placed before the shiftKey block, so a future reader doesn't "fix" the asymmetry by moving it down.

Uncovered cases / test improvements

The two tests cover the happy path well. Gaps I'd close:

  • Negative case (most valuable): block cursor not at the boundary (e.g. root:1 between decorators) — assert ArrowUp is not swallowed and still falls through to shadow-root navigation. Proves the guard isn't over-eager.
  • shift+ArrowUp / shift+ArrowDown at the boundary — assert the current behavior so the shiftKey interaction is locked in (this is the ambiguous case above).
  • Non-collapsed range whose focus is at root:0 — the guard only checks focus.key/focus.offset, not isCollapsed(). Confirm a selection ending at root:0 behaves as intended.
  • Empty / single-leading-decorator root where offset 0 === childrenSize — both guards fire on the same selection; worth a test that both directions stay put.
  • The two tests are near-identical mirrors — could collapse into one test.each over {direction, index, offset} to cut duplication.

Nit: test filename is consistent with RichTextBlockCursorShadowRoot.test.ts 👍

This branch was successfully deployed

2 active deployments
Preview – lexical — 7e4012a5 Deployed Jul 29, 2026 by vercel[bot]
Preview – lexical-playground — 7e4012a5 Deployed Jul 29, 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: Selection is leaving beyond the editor from the top block cursor

3 participants