Skip to content

[lexical-react] Bug Fix: Keep the selection under the pointer when drag-selecting over the DraggableBlock menu - #9463

Open
Vedthakar wants to merge 2 commits into
meta:mainfrom
Vedthakar:fix/9389-drag-select-over-menu
Open

Vedthakar wants to merge 2 commits into
meta:mainfrom
Vedthakar:fix/9389-drag-select-over-menu

Conversation

@Vedthakar

Copy link
Copy Markdown
Contributor

Description

This solves issue #9389: while drag-selecting text, the selection jumps when the pointer passes over the DraggableBlockPlugin menu (+ button or drag handle) in Chrome and Safari.

Current behavior. During a drag-select the browser extends the selection to the position under the pointer. Over the menu, that position is inside the menu's own elements, outside the contenteditable. Chrome and Safari then extend the selection to the start of the editor: drag right-to-left from the end of paragraph 3 onto the menu, and the focus lands at the start of paragraph 1. Firefox doesn't do this. The issue's measurements show caretPositionFromPoint() resolves into the editor once the menu has pointer-events: none.

What this changes.

  • When the primary button is pressed in the editor, DraggableBlockPlugin_EXPERIMENTAL sets pointer-events: none on the menu (menuRef). On pointerup or pointercancel anywhere in the document, or on blur of the window (where the release can be lost), it restores the menu's previous inline value, so the selection follows the text under the pointer.
  • The menu is portalled outside the root, so pressing the handle or + itself isn't affected, and block dragging works as before.
  • Nothing changes for plugin users: no new props, and custom menuComponents get the fix too.

Closes #9389

Test plan

New e2e test in packages/lexical-playground/__tests__/e2e/DraggableBlock.spec.mjs, "Drag-selecting text over the block menu does not extend the selection past the pointer (#9389)". It types 3 paragraphs, presses at the end of paragraph 3, drags right-to-left onto the menu, and checks that the selection's focus is still in paragraph 3. It also runs in Firefox, which doesn't have the bug, as a control: it would catch the fix breaking selection over the menu there.

Before

On main with the test applied (chromium):

  ✘ DraggableBlock › Drag-selecting text over the block menu does not extend the selection past the pointer (#9389)
    Expected: "Paragraph 3"
    Received: "Paragraph 1"

After

On current main (tested together with my other open fixes):

  • Chromium e2e: DraggableBlock.spec.mjs (the new test fails on main, with the focus in "Paragraph 1") and TextDragDrop.spec.mjs pass.
  • pnpm run test-unit: all 10,080 tests pass
  • tsc, pnpm run flow (0 errors), and eslint/prettier on the changed files: clean

Author: Ved Thakar (@Vedthakar)

🤖 Generated with Claude Code

@vercel

vercel Bot commented Oct 25, 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 16, 2026 11:58pm UTC
lexical-playground Ready Ready Preview Nov 16, 2026 11:58pm UTC

Request Review

@pkg-pr-new

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

Copy link
Copy Markdown
lexical

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

@lexical/a11y

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

@lexical/clipboard

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

@lexical/code

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

@lexical/code-core

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

@lexical/code-prism

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

@lexical/code-shiki

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

@lexical/compiler

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

@lexical/devtools-core

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

@lexical/dragon

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

@lexical/eslint-plugin

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

@lexical/extension

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

@lexical/fast-check

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

@lexical/file

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

@lexical/hashtag

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

@lexical/headless

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

@lexical/history

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

@lexical/html

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

@lexical/internal

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

@lexical/link

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

@lexical/list

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

@lexical/mark

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

@lexical/markdown

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

@lexical/mdast

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

@lexical/offset

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

@lexical/overflow

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

@lexical/plain-text

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

@lexical/react

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

@lexical/rich-text

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

@lexical/selection

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

@lexical/table

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

@lexical/tailwind

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

@lexical/text

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

@lexical/utils

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

@lexical/y

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

@lexical/yjs

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

commit: 8680743

@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Oct 27, 2026

@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 the fix, @Vedthakar, this looks good. I reproduced #9389 on main in Chromium and WebKit: dragging from the end of paragraph 3 onto the + button or the handle puts the focus at the start of paragraph 1. On this branch the focus stays at the start of paragraph 3 in Chromium, WebKit and Firefox, block dragging still works, and the new e2e test fails on main and passes here in Chromium.

A few optional, non-blocking suggestions:

  1. Custom menus whose buttons set pointer-events themselves (packages/lexical-react/src/LexicalDraggableBlockPlugin.tsx L383-384). pointer-events: none on the menuRef element is inherited, so a child that declares pointer-events: auto stays hit-testable. That's a common pattern when the menu container is a pass-through overlay. With .draggable-block-menu > * { pointer-events: auto; } added to the playground, this branch still jumps to paragraph 1 over the + button in Chromium and WebKit. Setting menuElem.inert = true for the press (and restoring the previous value) takes the whole subtree out of hit testing and fixed that case in both engines when I tried it. If you'd rather keep pointer-events, consider noting the limitation in the DraggableBlockPlugin_EXPERIMENTAL JSDoc, since the description says custom menuComponents get the fix too.
  2. Capture the release listeners (L387-390). Lexical core registers its own drag-release listeners on the document with capture, so a listener that stops pointerup can't hide the release (packages/lexical/src/LexicalEvents.ts L2537-2539 and L2626-2633). Passing true as the third argument to registerEventListeners here would do the same. The window blur listener (L392-400) could also go through registerEventListener, as the core does at L2636.
  3. Test target and assertion (packages/lexical-playground/__tests__/e2e/DraggableBlock.spec.mjs L246-257). In the playground menu, menu.x + menu.width / 2 lands in the 2px gap between the + button and the handle, so neither button is actually hovered. Aiming at each button's centre, and asserting focusOffset === 0 (or selection.toString() === 'Paragraph 3'), would cover the two buttons the issue names and would fail if the selection merely collapsed.
  4. Test plan. AGENTS.md (Commits and Pull Requests) asks for the real output of the command under ### After, and for an explicit note of any browser that wasn't exercised. WebKit, where the issue also reproduces, isn't mentioned.

Comment on lines +383 to +384
const pointerEvents = menuElem.style.pointerEvents;
menuElem.style.pointerEvents = 'none';

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: pointer-events is inherited, so a custom menu whose buttons declare pointer-events: auto (a common pattern when the container is a pass-through overlay) keeps them hit-testable, and the selection still jumps to paragraph 1 in Chromium and WebKit. Setting menuElem.inert = true for the press (and restoring the previous value) takes the whole subtree out of hit testing and fixed that case in both engines when I tried it. Alternatively, note the limitation in the JSDoc.

Comment on lines +387 to +390
const removeListeners = registerEventListeners(doc, {
pointercancel: onPointerUp,
pointerup: onPointerUp,
});

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: the core registers its drag-release listeners on the document with capture so a listener that stops pointerup can't hide the release (packages/lexical/src/LexicalEvents.ts L2626-2633). Passing true as the third argument here would do the same; otherwise a stopped release leaves the menu non-interactive until the next one reaches the document.

steps: 5,
},
);
await page.mouse.move(menu.x + menu.width / 2, menu.y + menu.height / 2, {

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: in the playground menu this point is the 2px gap between the + button and the handle, so neither button is hovered. Aiming at each button's centre (for example .draggable-block-menu > :nth-child(1) and (2)) would cover the two targets the issue names.

});
await page.mouse.up();

expect(focus).toBe('Paragraph 3');

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 also passes if the selection collapsed at the anchor. Asserting the focus offset (0) or selection.toString() === 'Paragraph 3' would pin the expected result.

Vedthakar and others added 2 commits November 16, 2026 18:54
…ag-selecting over the DraggableBlock menu

While text is drag-selected, Chrome and Safari resolve a pointer over the
draggable block menu to a position inside the menu, outside the editor,
and extend the selection to the start of the document. Let the pointer
pass through the menu while the primary button is held after a press in
the editor, so the selection follows the text beneath it.

Fixes meta#9389

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>
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. I rebased onto main and added a changeset; the code is unchanged. I left the optional suggestions (inert, capturing the release listeners, the test targets) out to keep the approved change as it is, and am happy to take any of them if a maintainer prefers.

— Ved Thakar

This branch was successfully deployed

2 active deployments
Preview – lexical — 86807433 Deployed Nov 16, 2026 by vercel[bot]
Preview – lexical-playground — 86807433 Deployed Nov 16, 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: Text selection jumps when the pointer hits the DraggableBlockPlugin menu (+ button / drag handle) during a drag-select

2 participants