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

Call updatePosition in onDrag

Open mididev opened this issue 8 years ago • 17 comments

I want to implement a custom snapping to certain element borders, for resizing as well as for dragging.

Resizing works like a charm since I just need to call...

newWidth = this.snapX(x) - position.x;
this.rnd.updateSize({ width: newWidth, height: newHeight });

...in onResize and the size gets overwritten.

But I can't achieve the same thing for dragging, because this.rnd.updatePosition(...) in onDrag has no effect.

I tried several (dirty) things (e.preventDefault(), calling updatePosition after time interval) and looked into the Draggable code, but I couldn't find a way that works.

Does anybody have a solution for this?

mididev avatar Aug 13 '17 10:08 mididev

i did the same thing, and had simliar problems. I solved it by using:

onDragStop = (e, ui) => {

}

You can use this.rnd.updatePosition in there without a problem.

sajadghawami avatar Aug 23 '17 07:08 sajadghawami

Thanks for your answer!

Yes, that's true and works quite well. But like that the UI feeling isn't really intuitive because like that for resizing I get an instant snap-to-the-grid feedback while dragging (resizing), but for dragging (onDrag) it only snaps when dragging is over :-/

I'm sure there is a way to do it but haven't found it so far. Would be very grateful if someone had an idea!

mididev avatar Aug 23 '17 07:08 mididev

i get your point, and had the same issue, because i needed customizable and multiple grids. So, i ended up doing the same for resize, keeping the logic in

onResizeStop = (e, ui) => {

}

Thus having the same UX for both dragging and resizing.

hope this helps :)

sajadghawami avatar Aug 23 '17 09:08 sajadghawami

It's a possibility, but I definitely need live snapping.

Now I found a way that works (although it's a nasty hack) great so far:

I dived into react-draggable (which react-rnd uses for dragging) and injected this code in the onDrag method:

if (this.snapX) {
    newState.x = this.snapX(newState.x);
}

Now I can go to my componentDidMount method and write this:

this.rnd.draggable.snapX = this.snapX.bind(this); // or without binding if using arrow function

Thereby I can now write a custom snap calculation function that allows setting the position while dragging.

I know this is not a clean solution, but it's the only way I found to make it work. Maybe it's worth making a feature request or a PR at react-draggable @github to add a prop for a custom snapping callback. Then react-rnd could add it and everything would be fine...?

mididev avatar Aug 23 '17 10:08 mididev

@mididev Could you please try position props on v7.1.0? If you can not solve this issue, please provide example on webpackbin https://www.webpackbin.com/bins/-Ku4nRhImIfnt9N08lGu.

thanks

bokuweb avatar Sep 15 '17 22:09 bokuweb

@bokuweb I tried it with position property and it doesn't work. Looks like this.rnd.updatePosition() doesn't work properly in onDrag() but it works in onDragStop(). https://www.webpackbin.com/bins/-KvSorX3JvYyXLcsYCVO Thanks

dubravcik avatar Oct 03 '17 07:10 dubravcik

Any news ?

arnauddb avatar Apr 10 '18 20:04 arnauddb

+1 for snapping functionality described above. Would be great!

vikttorr avatar Jun 11 '18 22:06 vikttorr

same problem

postgetme avatar Jul 17 '18 12:07 postgetme

Really need this as well. Any update here @bokuweb ?

justswim avatar Aug 29 '18 22:08 justswim

Would love to see this working too! :)

pahen avatar Sep 28 '18 07:09 pahen

For anyone who is stuck on this ... here's a UX work around that worked for me:

Instead of trying to get my layer to snap while dragging, what I did was show snap-to guides whenever the layer was within the snap area. Then, when the drag finished, I updated the position of the layer to the snap guides. So, the layer still doesn't snap while dragging, but it snaps immediately after dragging, and the user is aware of this because of the guides that show up.

I realize this doesn't actually solve this bug but it might work as a UX approach to solve this problem.

justswim avatar Sep 28 '18 18:09 justswim

+1 Basically this component doesn't seem to facilitate certain domain-specific behaviors (and fixes) that some of us need to implement. Currently stuck with UX workarounds similar to @justswim has described.

mhsdevus avatar Feb 06 '19 06:02 mhsdevus

I have a different use case, but my solution might be useful to someone.

Dror-Bar avatar Feb 15 '21 14:02 Dror-Bar

I'm trying to achieve the same behavior than @mididev Indeed, in order to be able to build some custom behavior it would require updatePosition() to work in onDrag()

llccrr avatar Feb 26 '21 14:02 llccrr

is it still relevant? me is too faced this problem when trying to handle custom position via updatePosition on onDrag event.

excal1bu7 avatar Dec 31 '22 21:12 excal1bu7

i've found a little solution. in my case i have to stop Drag when drag reached specific coordinates to reach this behavior i return false in onDrag callback.

Don't know why it's not described in docs... 🤔

excal1bu7 avatar Jan 01 '23 17:01 excal1bu7