question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support dragging multiple elements

See original GitHub issue

There is a common pattern that is hard to implement with native drag and drop: dragging multiple elements. While selection mechanics might vary from app to app (cmd+clicking, checkboxes, predefined groups), but it would be nice to at least make it possible to support this scenario.

screen shot 2014-10-26 at 1 07 07

Because multiple dragged items may not be siblings in DOM and setDragImage(element, x, y) is pretty much insane and hasn’t gotten any better, we won’t burden ourselves with trying to render several elements in drag preview at once.

How can we help implement this scenario, if we can’t show a “multiple” drag preview?

In a way, this scenario is already possible: consumers can manually keep track of selected elements, set dragPreview to some kind of generic placeholder Image and react appropriately to the dropping of (as far as business logic is concerned) several elements.

However there is currently no supported way for one element to know that it’s part of a group that’s being dragged. From react-dnd’s point of view, if we drag something, this component gets getDragState(type).isDragging = true, but other components don’t. From your app point of view, if you support multiple selection, you want all logically “selected” items to be aware that they’re being dragged, even if only one of them is being actually “DOM-dragged”.

What we need is a way for components to tell react-dnd that, “hey, although onDragStart was received by another component, I want to pretend that I’m being dragged too, and have my getDragState(type) mirror dragged component’s getDragState(type), and have my endDrag(didDrop) called too so I could do my stuff”.

How would components opt into that?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:19
  • Comments:32 (8 by maintainers)

github_iconTop GitHub Comments

17reactions
melvynhillscommented, Mar 1, 2018

If anyone’s interested, I implemented a multiple drag / grid view: https://codesandbox.io/s/9j897k0mwy. You can use cmd/ctrl and shift keys to select multiple items, then drag them around the grid and insert them anywhere.

This is still a demo, but I might abstract the code a little bit more and package it in a component later.

11reactions
danii1commented, Sep 8, 2016

I implemented it, solution is basically what is described in the first post:

  1. Keep track of your selected items
  2. Implement CustomDragLayer (see https://gaearon.github.io/react-dnd/docs-drag-layer.html) and render your selected items yourself
  3. Handle drop if dropped item is part of the selection
Read more comments on GitHub >

github_iconTop Results From Across the Web

Accessible Drag and Drop with Multiple Items - SitePoint
The basic approach to dragging multiple elements is actually pretty trivial — we simply need to remember the drag data for more than...
Read more >
How do I drag multiple elements at once with JavaScript or ...
Wrap the draggable elements in a container and use an event to drag them all at once, this allows for singles draggables and...
Read more >
Drag-and-Drop & Multiselect - Help Center - GitBook
The multiselect feature lets you select multiple elements with the drag of the mouse. Then you will be able to apply some formatting...
Read more >
How to drag and drop several/multiple items using dxSortable
Hi guys! Is it possible to implement dragging of the several items at same time using dxSortable? My clients want to check several...
Read more >
Move and copy items with drag and drop on iPad
With drag and drop, you can use a finger to move text and items within an app and copy items from one app...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found