lexical icon indicating copy to clipboard operation
lexical copied to clipboard

Bug: Drag and drop of text is broken in Firefox

Open medihack opened this issue 1 year ago • 4 comments

Lexical version: 0.38.2

Steps To Reproduce

  1. In Firefox, go tothe official Lexical Playground
  2. Select some text in the editor
  3. Drag and drop it somewhere else in the editor

Link to code example: No code example needed, as it can be easily reproduced in the official Playground.

The current behavior

Drag-and-drop of text does not work in Firefox. The text is not moved to the drop location.

The expected behavior

Drag-and-drop should work in Firefox (like it does in Chrome).

Impact of fix

The bug does affect all Firefox users.

medihack avatar Nov 27 '25 23:11 medihack

Just found out that this is not only a Firefox problem. Drag-and-drop is also broken in Chrome, just in a different way (more sporadic). I see the same issues in the official Playground as in the first video of https://github.com/facebook/lexical/pull/7749.

For example, try to drag-and-drop the word "toolbar" in front of the word "the" before it. "toolbar" then jumps to the beginning of the sentence (Chrome Version 142.0.7444.176 (Official Build) (arm64)).

medihack avatar Nov 28 '25 18:11 medihack

Root cause and fix for Firefox drag and drop

The HTML5 DnD spec requires event.preventDefault() in dragover to signal that an element accepts drops, without it, the browser never fires drop. There's a narrow native exception for <textarea> and <input type="text">, but contenteditable divs don't qualify. Chrome papers over this with internal contenteditable handling that fires drop regardless; Firefox enforces the spec strictly.

The gap is in the DRAGOVER_COMMAND handler in packages/lexical-rich-text/src/index.ts. Right now event.preventDefault() is only called when the drag target is a DecoratorNode, for plain text drags it's never called, so Firefox silently swallows the drop and $handleRichTextDrop never runs. The fix is one unconditional event.preventDefault() before the caretFromPoint block, guarded by the existing isFileTransfer early return. The decorator specific call can go, it's redundant once the unconditional one is in place.

Worth noting: the e2e test added in #8373 (TextDragDrop.spec.mjs) doesn't catch this because it synthesizes the drag by dispatching dragstart → drop → dragend directly, skipping dragover entirely. The drop event is forced rather than earned, so the browser gate never comes into play. The tests pass on all browsers in CI for this reason. Ideally a follow up adds coverage that goes through a real dragover dispatch and verifies drop actually fires.

The fix is a one line change to the DRAGOVER_COMMAND handler. Happy to raise a PR if that's useful.

sahiee-dev avatar Jun 09 '26 18:06 sahiee-dev

Yes, a PR is useful especially if you can write a test of some kind for it

etrepum avatar Jun 09 '26 18:06 etrepum

This was fixed by #8373 (merged 2026-04-23), which rewired the DROP_COMMAND handler to actually process the drop. #8663 further hardened the dragover layer for spec compliance. Closing as resolved.

mayrang avatar Jul 02 '26 08:07 mayrang