react-rnd icon indicating copy to clipboard operation
react-rnd copied to clipboard

Is there a way to select multiple RnD div's together and drag them?

Open tulasinandan654 opened this issue 1 year ago • 1 comments

I have a parent RnD with child RnD div's in it. When a parent is moved, the child's in it should be moved together relative to parent.

tulasinandan654 avatar Aug 16 '23 12:08 tulasinandan654

Here's a working example I put together (everything you need is in src/App.tsx): CodeSandbox Link

https://github.com/bokuweb/react-rnd/assets/11688080/be5e9aa7-17c5-4460-b667-a4bb097933a5

To summarize my solution:

  • Use setState to track an array of selectedIds
  • When you drag an item, subtract its current position from the new position being passed into your onDrag handler. This will be our delta that we can use to calculate the new positions of all the other selected items.
  • Iterate through all items that need to be moved (either just the current item, or all items that have an id in selectedIds), adding the delta.x and delta.y to each item's position.x and position.y respectively.

You may need to create a context that tracks the selectedIds and handles the drag events, depending on how complicated your nesting structure is (ie, detecting if all selected ids are for items in the same row/layer). In my example, I just have one giant container component that handles everything, so YMMV.

aleciavogel avatar Mar 21 '24 20:03 aleciavogel