Skip to content

[lexical-extension][lexical-playground] Bug Fix: Select a horizontal rule when clicking the empty space above or below it - #2

Closed
Vedthakar wants to merge 1 commit into
mainfrom
fix/7758-hr-gap-click
Closed

Vedthakar wants to merge 1 commit into
mainfrom
fix/7758-hr-gap-click

Conversation

@Vedthakar

@Vedthakar Vedthakar commented Oct 16, 2026 •

Copy link
Copy Markdown
Owner

Description

This solves issue meta#7758: clicking the empty space above or below a horizontal rule now selects the rule, which is what the arrow keys already do from those spots.

Current behavior. A horizontal rule is a block decorator with vertical margins. A click in the margin above or below it doesn't land on the <hr>. It lands on the parent element (the root in the playground), and the caret ends up wherever the browser decides. On main in Chromium and Firefox:

@etrepum's expected behaviour on the issue is that clicks should be handled "in the basically same way that the arrow keys are… there are just 'empty' locations between the divider and any adjacent block element node". That covers both sides of the rule.

Root cause. The CLICK_COMMAND handler in HorizontalRuleExtension only selects the rule when event.target resolves to the HorizontalRuleNode. A click in the gap targets the parent element, so the handler returns false.

Why a hit test, not the caret. meta#8967 selected the rule from the caret the click produced. It was folded into meta#9053, and its HR part was dropped with this note: the caret offset in that blank space "is precisely what browsers disagree about… so selecting the rule reliably needs a hit test against the rules' own geometry, which is a different change and belongs in its own PR." This is that PR. The handler doesn't look at the caret at all, so the result doesn't depend on where Chromium, Firefox or WebKit put it.

What this changes.

  • When the click target is an element node (not the <hr>), $getHorizontalRuleAtClick checks that element's child HorizontalRuleNodes. It takes each rule's getBoundingClientRect(), extends it by the rule's computed margins, and selects the rule whose box contains the click. If two rules' margins overlap, the one nearer to the click wins.
  • A shift-click in the gap is left alone, so it extends the range as before.
  • A click below a rule that is the last block inserts a paragraph only when the opt-in ClickAfterLastBlockExtension is enabled, as it is in the playground. That extension claims the click and stops Lexical's click handling. Without it, the click selects the rule.
  • A direct click on the <hr> and shift-click toggling are unchanged, and so are clicks inside text.

Behaviour change for meta#6775 (intentional). A click between two adjacent rules used to leave a root caret between them. It now selects the nearer rule, which is consistent with the gap next to any other block. Adding content between two rules still works: with the rule selected, Enter inserts a paragraph after it. The meta#6775 test checks this now, as well as the original point of meta#6775 (the click gives a selection, not null).

This likely also fixes meta#6689 (clicking just above the left part of a rule selects root), which is the same click as the "above" case here. I haven't marked it as closed.

The deprecated @lexical/react/LexicalHorizontalRuleNode is not changed. The playground and HorizontalRuleExtension users get the fix.

Credit. Selecting the rule from the click handler, and the e2e test shape, build on meta#8967 by @LeSingh1.

Closes meta#7758

Test plan

New e2e tests in packages/lexical-playground/__tests__/e2e/HorizontalRule.spec.mjs, under "Clicking the empty space beside a horizontal rule (meta#7758)". Each click point is computed from the middle of the gap itself, not a fixed pixel offset:

  • the gap above the rule selects it (NodeSelection of the horizontalrule, plus the hrSelected class)
  • the gap below the rule (rule followed by a paragraph) selects it
  • clicking text next to the rule places a text caret and doesn't select the rule
  • shift-clicking the gap extends the range (non-collapsed RangeSelection across the rule)
  • clicking below a rule that is the last block still inserts a paragraph after it, with the caret in it

Updated "Clicking between consecutive block decorators creates selection (meta#6775)": it now expects the nearer rule to be selected, then presses Enter and checks that a paragraph sits between the two rules (hr + p + hr).

Before

On main with the tests applied and the source change reverted. I ran this locally in Chromium and Firefox:

$ pnpm exec playwright test --project=chromium HorizontalRule.spec.mjs -g "7758|6775"
  ✘ Clicking the gap above the rule selects it
  ✘ Clicking the gap below the rule selects it
  ✓ Clicking text next to the rule places a caret
  ✓ Shift-clicking the gap extends the range selection
  ✓ Clicking below a rule that is the last block inserts a paragraph
  ✘ Clicking between consecutive block decorators creates selection (#6775)
  3 failed, 3 passed

Firefox gives the same 3 failures and 3 passes. The 3 passing cases are behaviour this PR keeps.

After

$ pnpm exec playwright test --project=chromium HorizontalRule.spec.mjs   # 19 passed
$ pnpm exec playwright test --project=firefox  HorizontalRule.spec.mjs   # 19 passed
$ pnpm exec playwright test --project=chromium Selection.spec.mjs        # 38 passed, 11 skipped

$ pnpm run test-unit   # Tests 9286 passed | 1 skipped (9287)
$ pnpm run tsc         # no errors
$ pnpm run flow        # Found 0 errors
$ eslint and prettier --check on the changed files   # clean

On browser coverage: I checked the Firefox "before" and "after" results locally only. CI on my fork runs only the Chromium canary e2e job, because the extended Firefox/WebKit workflow only runs in the facebook org. I couldn't run WebKit locally because its system dependencies aren't installed on my machine, so WebKit is left to upstream extended CI. The hit test doesn't depend on browser caret placement, which is what differed between browsers before.


Author: Ved Thakar (@Vedthakar)

@Vedthakar Vedthakar changed the title [lexical-extension][lexical-playground] Bug Fix: Select a horizontal rule when clicking the empty space beside it [lexical-extension][lexical-playground] Bug Fix: Select a horizontal rule when clicking the empty space above or below it Oct 16, 2026
@Vedthakar
Vedthakar force-pushed the fix/7758-hr-gap-click branch 2 times, most recently from cb65a06 to 63ef73a Compare October 16, 2026 03:30
…rule when clicking the empty space above or below it

A click in the gap above or below a horizontal rule (its margins) lands
on the parent element, so the CLICK_COMMAND handler, which only checked
for the <hr> itself, ignored it. Where the browser then puts the caret
differs between Chromium and Firefox, so the handler now hit tests the
click against each child rule's box extended by its margins and selects
the nearest one, matching the arrow keys. Shift-click still extends the
range, and a click below a rule that is the last block still inserts a
paragraph.

Selecting the rule from the click handler builds on meta#8967 by @LeSingh1.

Signed-off-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com>
Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Vedthakar
Vedthakar force-pushed the fix/7758-hr-gap-click branch from 63ef73a to e0fa25f Compare October 21, 2026 21:37
@Vedthakar

Copy link
Copy Markdown
Owner Author

Superseded by meta#9422.

@Vedthakar Vedthakar closed this Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: HorizontalRule lexical playground Bug: Range selection selects root node while trying to click on the Horizontal Rule

1 participant