Skip to content

[lexical] Bug Fix: Create selection when clicking between block decorators on Firefox - #8862

Merged
etrepum merged 4 commits into
meta:mainfrom
mayrang:fix/firefox-block-decorator-click
Jul 21, 2026
Merged

etrepum merged 4 commits into
meta:mainfrom
mayrang:fix/firefox-block-decorator-click

Conversation

@mayrang

@mayrang mayrang commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

Description

Firefox does not produce a DOM selection when clicking in the gap between block-level decorator elements (e.g. consecutive <hr> nodes). Chrome and Safari handle this by placing the selection on rootElement at the appropriate child offset, but Firefox leaves rangeCount === 0 and does not fire a selectionchange event (since from its perspective nothing changed). This means Lexical never creates a selection, and the block cursor that normally appears between decorators is never shown.

The fix adds two coordinating branches in LexicalEvents.ts:

  1. In onDocumentSelectionChange (mousedown path): when Firefox reports an empty DOM selection (domAnchorNode === null), clear Lexical's selection so the committed state reflects the empty DOM.

  2. In onClick: when selection === null and domSelection.rangeCount === 0, iterate rootElement.childNodes comparing event.clientY against each child's vertical midpoint to compute the correct offset, then call setBaseAndExtent to establish the DOM selection. $internalCreateRangeSelection picks it up from there, and the existing block cursor mechanism ($updateDOMBlockCursorElement) renders the cursor at the gap.

Closes #6775

Design notes

Why two sites instead of onClick alone? — For click events with detail !== 3, $internalCreateRangeSelection clones the committed RangeSelection without re-reading the DOM. If a selection already exists when the user clicks between decorators, the clone makes $getSelection() return non-null, preventing the onClick branch from entering. The selectionchange branch nulls the committed selection first, so the subsequent onClick sees null and fires.

caretPositionFromPoint — Tested on Firefox; it returns the decorator element itself ({offsetNode: hr, offset: 0}) rather than a root-level child offset. It can't distinguish "between child 1 and child 2 of rootElement," so the manual midpoint loop is necessary.

Why core, not an extension? — The block cursor mechanism itself lives in LexicalUtils.ts (core). This fix establishes the selection that the block cursor relies on — it's event-layer plumbing, not a feature-specific behavior.

Test plan

  • Manual (Firefox): playground with 3 consecutive horizontal rules — clicking in each gap shows the block cursor at the correct position
  • Manual (Firefox): existing selection in a paragraph, then click between decorators — cursor moves to the gap
  • Manual (Chrome/Safari): same scenarios unchanged (the IS_FIREFOX guard prevents the new code from running)
  • pnpm tsc --noEmit — clean

@vercel

vercel Bot commented Jul 20, 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 21, 2026 5:44am
lexical-playground Ready Ready Preview Jul 21, 2026 5:44am

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 20, 2026

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

Is it possible to add a browser unit or e2e that covers this?

…ators on Firefox

Firefox does not produce a DOM selection when clicking in the gap between
block-level decorator elements (rangeCount === 0) and does not fire a
selectionchange event. This leaves Lexical without a selection, preventing
the block cursor from appearing.

Add two coordinating branches in LexicalEvents.ts:
1. onDocumentSelectionChange (mousedown path): clear Lexical's selection
   when Firefox reports domAnchorNode === null.
2. onClick: compute the correct rootElement child offset from event.clientY
   via midpoint comparison, then setBaseAndExtent to establish the DOM
   selection that $internalCreateRangeSelection picks up.

Closes meta#6775
@mayrang

mayrang commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator Author

Sure, pushed one — two adjacent HRs (paragraphs removed), click the gap, verify a RangeSelection lands on root. All three browsers pass.

@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Jul 20, 2026
@etrepum

etrepum commented Jul 21, 2026

Copy link
Copy Markdown
Collaborator

Something in here seems to be causing a lot of firefox e2e tests to fail

The previous approach cleared Lexical's selection in onDocumentSelectionChange
whenever Firefox reported an empty DOM selection (domAnchorNode === null).
This was too aggressive — Firefox can temporarily empty the DOM selection
during table operations (drag-select, merge/unmerge, copy/paste), causing
52 Firefox e2e test failures.

Instead, the onClick handler now directly checks domSelection.rangeCount === 0
without depending on selection being pre-cleared. The selectionchange guard
is removed entirely.
@mayrang

mayrang commented Jul 21, 2026 •

Copy link
Copy Markdown
Collaborator Author

(Updated) Yeah, the selectionchange guard I added was too broad — it cleared Lexical's selection whenever Firefox reported domAnchorNode === null, but Firefox can empty the DOM selection temporarily during table operations too.

Pushed another fix on top: removed the selectionchange guard, and narrowed the onClick fallback to only fire when event.target is the root element itself. The coordinate-based selection was also triggering inside table cells and other nested elements where rangeCount === 0 can happen on Firefox.

The coordinate-based selection was firing for any Firefox click with
rangeCount === 0, including clicks inside table cells during drag-select
and merge/unmerge operations. Restrict to event.target === rootElement
so only clicks directly on the editor root (between block decorators)
trigger the fallback.
@etrepum
etrepum added this pull request to the merge queue Jul 21, 2026
Merged via the queue into meta:main with commit 443012c Jul 21, 2026
44 checks passed
@etrepum etrepum mentioned this pull request Jul 29, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — f855f52c Deployed Jul 21, 2026 by vercel[bot]
Preview – lexical-playground — f855f52c Deployed Jul 21, 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: Can't continue typing in the editor when it has only horizontal rules nodes.

2 participants