Skip to content

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

Open
Vedthakar wants to merge 4 commits into
meta:mainfrom
Vedthakar:fix/7758-hr-gap-click
Open

Vedthakar wants to merge 4 commits into
meta:mainfrom
Vedthakar:fix/7758-hr-gap-click

Conversation

@Vedthakar

Copy link
Copy Markdown
Contributor

Description

This solves issue #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. #8967 selected the rule from the caret the click produced. It was folded into #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 #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 #6775 test checks this now, as well as the original point of #6775 (the click gives a selection, not null).

This likely also fixes #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 #8967 by @LeSingh1.

Closes #7758

Test plan

New e2e tests in packages/lexical-playground/__tests__/e2e/HorizontalRule.spec.mjs, under "Clicking the empty space beside a horizontal rule (#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 (#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

On current main:


Author: Ved Thakar (@Vedthakar)

🤖 Generated with Claude Code

@vercel

vercel Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
lexical Ready Ready Preview Nov 17, 2026 12:56am UTC
lexical-playground Ready Ready Preview Nov 17, 2026 12:56am UTC

Request Review

@pkg-pr-new

pkg-pr-new Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
lexical

npm i https://pkg.pr.new/lexical@9422

@lexical/a11y

npm i https://pkg.pr.new/@lexical/a11y@9422

@lexical/clipboard

npm i https://pkg.pr.new/@lexical/clipboard@9422

@lexical/code

npm i https://pkg.pr.new/@lexical/code@9422

@lexical/code-core

npm i https://pkg.pr.new/@lexical/code-core@9422

@lexical/code-prism

npm i https://pkg.pr.new/@lexical/code-prism@9422

@lexical/code-shiki

npm i https://pkg.pr.new/@lexical/code-shiki@9422

@lexical/compiler

npm i https://pkg.pr.new/@lexical/compiler@9422

@lexical/devtools-core

npm i https://pkg.pr.new/@lexical/devtools-core@9422

@lexical/dragon

npm i https://pkg.pr.new/@lexical/dragon@9422

@lexical/eslint-plugin

npm i https://pkg.pr.new/@lexical/eslint-plugin@9422

@lexical/extension

npm i https://pkg.pr.new/@lexical/extension@9422

@lexical/fast-check

npm i https://pkg.pr.new/@lexical/fast-check@9422

@lexical/file

npm i https://pkg.pr.new/@lexical/file@9422

@lexical/hashtag

npm i https://pkg.pr.new/@lexical/hashtag@9422

@lexical/headless

npm i https://pkg.pr.new/@lexical/headless@9422

@lexical/history

npm i https://pkg.pr.new/@lexical/history@9422

@lexical/html

npm i https://pkg.pr.new/@lexical/html@9422

@lexical/internal

npm i https://pkg.pr.new/@lexical/internal@9422

@lexical/link

npm i https://pkg.pr.new/@lexical/link@9422

@lexical/list

npm i https://pkg.pr.new/@lexical/list@9422

@lexical/mark

npm i https://pkg.pr.new/@lexical/mark@9422

@lexical/markdown

npm i https://pkg.pr.new/@lexical/markdown@9422

@lexical/mdast

npm i https://pkg.pr.new/@lexical/mdast@9422

@lexical/offset

npm i https://pkg.pr.new/@lexical/offset@9422

@lexical/overflow

npm i https://pkg.pr.new/@lexical/overflow@9422

@lexical/plain-text

npm i https://pkg.pr.new/@lexical/plain-text@9422

@lexical/react

npm i https://pkg.pr.new/@lexical/react@9422

@lexical/rich-text

npm i https://pkg.pr.new/@lexical/rich-text@9422

@lexical/selection

npm i https://pkg.pr.new/@lexical/selection@9422

@lexical/table

npm i https://pkg.pr.new/@lexical/table@9422

@lexical/tailwind

npm i https://pkg.pr.new/@lexical/tailwind@9422

@lexical/text

npm i https://pkg.pr.new/@lexical/text@9422

@lexical/utils

npm i https://pkg.pr.new/@lexical/utils@9422

@lexical/y

npm i https://pkg.pr.new/@lexical/y@9422

@lexical/yjs

npm i https://pkg.pr.new/@lexical/yjs@9422

commit: 5e8bd7f

@rootnode-bot rootnode-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for picking this up! The hit-test approach is a good fit for #7758: it follows the direction on the issue (clicks around a rule should become a NodeSelection of the rule, the way the arrow keys do), and the new e2e cases fail on main and pass with the change in both Chromium and Firefox. One behaviour regression needs fixing before this can merge. The other two points are optional.

1. A drag released in the rule's margin loses its selection (packages/lexical-extension/src/HorizontalRuleExtension.ts, the new branch in the CLICK_COMMAND handler)

Browsers fire click on the common ancestor of the mousedown and mouseup targets. A drag that starts in text and ends in the gap above or below a rule therefore reaches this handler as a click on the root, with the mouseup's coordinates. The hit test finds the rule and replaces the range the user just dragged out with a NodeSelection of the rule.

To reproduce in the playground: type Above, insert a rule, type Below, then drag from the start of Above and let go in the gap just above the rule. On main, "Above" stays selected. With this change only the rule is selected, so pressing Backspace deletes the rule instead of "Above". The same happens for a drag from "Below" released above the rule, and for a drag released below it.

One guard fixes it. When the gesture already produced a non-collapsed range, leave the click alone:

const selection = $getSelection();
const ruleAtClick =
  event.shiftKey ||
  ($isRangeSelection(selection) && !selection.isCollapsed())
    ? null
    : $getHorizontalRuleAtClick(editor, event);

A plain click still works with this guard, even right after a range was selected, because mousedown collapses the selection before click fires. With it applied, the whole HorizontalRule.spec.mjs still passes in Chromium and Firefox. Please also add an e2e case for it in the new describe block: mouse.down() at the start of "Above", mouse.up() at getGapPoint(page, 'above'), then expect a non-collapsed RangeSelection and no hrSelected class.

2. Optional: assert that the shift-click range spans the rule (HorizontalRule.spec.mjs, "Shift-clicking the gap extends the range selection")

The test checks only that the selection is non-collapsed, so a shift-click that extended within "Above" would pass too. Checking that the focus lands past the rule, or that the selected text includes it, would make the test match its comment.

3. Optional: use getComposedEventTarget(event) ($getHorizontalRuleAtClick)

AGENTS.md's "Shadow DOM and iframe realm safety" table asks library code to read the event target through getComposedEventTarget(event) rather than event.target. Lint does not enforce that row. It works today because Lexical's click listener sits on the root, inside the same shadow tree, and the existing branch a few lines above reads event.target the same way, so switching both would be a small consistency fix.

The #6775 behaviour change (a click between two rules now selects the nearer rule, and Enter then adds a paragraph between them) reads as intentional and consistent with the issue, so I'm not raising it as a problem.

Comment on lines +870 to +877
.poll(() =>
getPageOrFrame(page).evaluate(() => {
const sel = window.lexicalEditor.getEditorState()._selection;
return sel !== null && 'anchor' in sel && !sel.isCollapsed();
}),
)
.toBe(true);
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Optional: this passes for any non-collapsed range, including one that only extends within "Above". Asserting that the focus lands past the rule (or the selected text includes it) would match the comment above. A sibling case that drags from "Above" and releases in the gap would cover the regression in HorizontalRuleExtension.ts.

editor: LexicalEditor,
event: MouseEvent,
): null | HorizontalRuleNode {
const parent = isDOMNode(event.target)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Optional: AGENTS.md's realm-safety table asks for getComposedEventTarget(event) instead of event.target (lint can't enforce it). It works today because the click listener is on the root inside the same shadow tree, so this is a consistency nit, same as the existing branch at line 233.

Vedthakar and others added 2 commits November 16, 2026 18:31
…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>
The browser fires the click of a drag on the common ancestor, at the
release point, so releasing a drag in the gap beside a rule replaced
the dragged range with a NodeSelection of the rule. Leave the click
alone when the gesture already made a non-collapsed range.

Add an e2e case for it, and assert that the shift-click range spans the
rule.

Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@Vedthakar

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Fixed in the latest push:

  • Drag released in the rule's margin: the click is left alone when the gesture already made a non-collapsed range, with your guard.
  • Added the e2e case (drag from the start of "Above", release in the gap above the rule). It fails without the guard. It asserts the selection is still a range anchored in the text. I did not assert that the rule has no hrSelected class: the browser's own range ends after the rule, so the rule is highlighted as part of the range, the same as on main.
  • The shift-click test now asserts the range contains the rule.

I left event.target as is, to match the existing branch just above it. Also rebased onto main and added a changeset. HorizontalRule.spec.mjs passes in Chromium (20 tests); I did not run Firefox or WebKit locally.

— Ved Thakar

@rootnode-bot rootnode-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks, this looks good. Hit-testing the rules' own margin boxes makes a click in the gap behave like the arrow keys, as suggested on #7758, and it no longer depends on where each browser puts the caret. The shift-click and drag guards keep range selections intact. The gap tests fail on main and pass here in Chromium and Firefox, and the drag test fails if the !selection.isCollapsed() guard is removed, so it covers that case.

Suggestions (non-blocking):

  1. The new shift-click test fails in Firefox (packages/lexical-playground/__tests__/e2e/HorizontalRule.spec.mjs:869-882). In Firefox the shift-click focus lands at root:1, just before the rule, so the range never includes the horizontalrule and the poll times out. This happens on main too, so it comes from Firefox's own caret placement, not from this PR. But the extended e2e workflow runs this spec on Firefox once a PR is approved, so it will need adjusting before it lands. The test's point, that a shift-click isn't replaced by a NodeSelection, holds in both browsers if the test asserts a non-collapsed RangeSelection instead ('anchor' in sel && !sel.isCollapsed()). The Before section of the description says Firefox shows the same 3 failures and 3 passes, but on main this test fails in Firefox too, so it's 4 and 2 there.
  2. Changeset Verify: paragraph (.changeset/hr-gap-click-selects-rule.md). CONTRIBUTING.md ("Changesets") and AGENTS.md ask a fix that changes editing behavior to include a Verify: paragraph, so it's listed under Behavior changes. Clicks beside a rule, including between two adjacent rules, now select the rule instead of placing a caret, which is worth flagging to people upgrading. A suggested line is inline.
  3. Zero-size rules (packages/lexical-extension/src/HorizontalRuleExtension.ts:169-178). A rule that isn't laid out has an all-zero rect, so a click at (0, 0) matches it. A user can't click there, but jsdom reports every rect as zero and element.click() sends (0, 0). With HorizontalRuleExtension, calling rootElement.click() in a unit test now selects the first rule. Skipping rules whose rect has zero width and height avoids surprising downstream jsdom tests.

The earlier optional notes still apply: getComposedEventTarget(event) at line 151, per the realm-safety table in AGENTS.md, and tightening the drag test's assertion.

Automated review (maintainer link)

Comment on lines +869 to +882
await expect
.poll(() =>
getPageOrFrame(page).evaluate(() =>
window.lexicalEditor.getEditorState().read(() => {
const sel = window.lexicalEditor.getEditorState()._selection;
return (
sel !== null &&
'anchor' in sel &&
sel.getNodes().some(n => n.getType() === 'horizontalrule')
);
}),
),
)
.toBe(true);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This assertion fails in Firefox, on main as well as here. Firefox puts the shift-click focus at root:1, just before the rule (anchor Above:5, focus root offset 1), so getNodes() never includes the horizontalrule and the poll times out. Chromium puts the focus at Below:0. The extended e2e workflow runs this spec in Firefox once the PR is approved.

The point of the test, that a shift-click isn't replaced by a NodeSelection, holds in both browsers if it checks for a non-collapsed range instead:

const sel = window.lexicalEditor.getEditorState()._selection;
return sel !== null && 'anchor' in sel && !sel.isCollapsed();

(and the comment above would then say the range is kept rather than that it spans the rule).

'@lexical/extension': patch
---

[fix] A click in the empty space above or below a horizontal rule selects the rule

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Optional: CONTRIBUTING.md ("Changesets") asks a fix that changes editing behavior to add a Verify: paragraph, so it lists under Behavior changes. Clicks between two adjacent rules no longer leave a caret between them, which is worth flagging:

Suggested change
[fix] A click in the empty space above or below a horizontal rule selects the rule
[fix] A click in the empty space above or below a horizontal rule selects the rule
Verify: clicks in the space above or below a horizontal rule, including between two adjacent rules, now select the rule instead of placing a caret; check tests that click beside a rule.

if (!dom || !view) {
continue;
}
const rect = dom.getBoundingClientRect();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Optional: a rule that isn't laid out has an all-zero rect, so a click at (0, 0) passes the bounds check. A user can't click there, but jsdom reports every rect as zero and element.click() sends (0, 0): with this extension, rootElement.click() in a jsdom test now selects the first rule instead of keeping the caret. Skipping empty rects avoids that:

Suggested change
const rect = dom.getBoundingClientRect();
const rect = dom.getBoundingClientRect();
if (rect.width === 0 && rect.height === 0) {
// Not laid out (display: none, or jsdom): nothing to click beside.
continue;
}

…tal rule e2e test

Firefox extends the range of a shift-click in the gap without the rule
among its nodes, so asserting that the range contains the rule failed
there. Assert a non-collapsed range again, as before.

Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@Vedthakar

Copy link
Copy Markdown
Contributor Author

Correction to my note above: I reverted the stricter shift-click assertion (that the range contains the rule). It passed in Chromium but failed in Firefox on CI, where a shift-click in the gap extends the range without the rule among its nodes, so the test asserts a non-collapsed range again, as it did when this was reviewed. The new drag-release test passed in Firefox on that CI run.

— Ved Thakar

This branch was successfully deployed

2 active deployments
Preview – lexical — 5e8bd7f1 Deployed Nov 17, 2026 by vercel[bot]
Preview – lexical-playground — 5e8bd7f1 Deployed Nov 17, 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.

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