Cancel drag-and-drop with the Esc key (#1301)#1305
Merged
Conversation
A drag-and-drop operation could only be ended by dropping. Pressing Esc during a drag now cancels it: nothing is dropped, drop zones reset, the drag clone is removed, and the mouse/touch capture is released. - ops: register a global keydown listener during a drag; on Escape run the end sequence without invoking onDrop, and tear down the capture. - captureMouse: the capture helpers return a teardown function so the capture can be released programmatically (additive; callers may ignore). - Introduce a dedicated `DragEndEvent` with a `cancelled` flag so onDragEnd handlers can tell a cancel from a drop. On a keyboard cancel its `event`/`cursor` are null. `onDragEnd` now receives it on DropZone, DragSource and Grid; onDrop/onDragAway keep using DragEvent. Closes #1301
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1301.
Summary
Pressing Esc during a drag-and-drop operation now cancels it: nothing is dropped, drop zones reset their state, the drag clone is removed, and the mouse/touch capture is released.
Changes
drag-drop/ops.tsxkeydownlistener is registered for the duration of a drag. On Escape it runs the end sequence vianotifyDragDrop(null, /*cancelled*/ true)— skippingonDrop— and tears down the capture.DragEndEventwith acancelledflag, soonDragEndhandlers can distinguish a cancel from a drop (and still readresult). On a keyboard cancel there is no pointer event, so itseventandcursorarenull.overlay/captureMouse.tscaptureMouse/captureMouseOrTouchand the*2variants) now return a teardown function so the capture can be released programmatically. Additive — existing callers ignore the return value.DropZone/DragSource/GridonDragEndnow receives aDragEndEvent.onDropandonDragAwaycontinue to useDragEvent(they only fire on a real pointer drop).Behavior notes
onDrop/onDragAway/onDragEndfire as before withcancelled: false.onDropand the end-of-dragonDragAwaydo not fire (no pointer event);onDragEndfires for every started zone, which fully resets zone state, withcancelled: true.Tests
yarn check-typesclean,yarn test525 passing. Drag-and-drop is event/DOM-driven with no unit coverage, so this was verified by type-checking and manual reasoning through both the drop and cancel paths.Notes