react-rnd
react-rnd copied to clipboard
Is there a way to select multiple RnD div's together and drag them?
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.
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 ofselectedIds
- When you drag an item, subtract its current position from the new position being passed into your
onDrag
handler. This will be ourdelta
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 thedelta.x
anddelta.y
to each item'sposition.x
andposition.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.