react-draggable
react-draggable copied to clipboard
Adding the option for percentag based dragging
This is an initial attempt at adding percentage based offsets to address #63.
One of the odd things in this plugin is the ability to cancel the drag events by returning false from onDrag. I don't think this is healthy for the API. This change initially ignore the return value in the the drag callback for code convenience.
If this works for you I can clean up the code a bit, make sure it matches style, and update the documentation.
Coincidentally was after this functionality myself this evening and stumbled across your PR! :smile:
Have tested this locally, seems to work ok with a few issues (that you're possibly already aware of):
- You'll probably want to initialize
percentXandpercentYingetInitialStateto preserve thestart=method's functionality in percent mode:
--- a/lib/draggable.js
@@ -515,7 +514,8 @@ module.exports = React.createClass({
offsetX: 0, offsetY: 0,
// Current transform x and y.
- clientX: props.start.x, clientY: props.start.y
+ clientX: props.start.x, clientY: props.start.y,
+ percentX: props.start.x, percentY: props.start.y
};
},
- The element's offset to the dragpoint isn't being correctly determined in your positioning calculation, so when you click to drag an element, it snaps the elements top left corner (
0,0) to match your cursor, like so:

Had a bash at trying to fix that but couldn't wrap my head around the right numbers and ran out of time.
I like this idea, but it will be much easier to implement on top of <DraggableCore>. Expect the refactor to hit in the next few days.
https://github.com/mzabriskie/react-draggable/tree/coreRefactor
This is a TODO for me now that v1.0.0 is out.