react-draggable
React draggable component
Based on https://github.com/mzabriskie/react-draggable
Installing
$ npm install react react-draggable2
You will also need install React to your top-level project due to
#2.
Demo
http://mzabriskie.github.io/react-draggable/example/
Example
var React = require('react'),
Draggable = require('react-draggable');
var App = React.createClass({
handleStart: function (event, ui) {
console.log('Event: ', event);
console.log('Position: ', ui.position);
},
handleDrag: function (event, ui) {
console.log('Event: ', event);
console.log('Position: ', ui.position);
},
handleStop: function (event, ui) {
console.log('Event: ', event);
console.log('Position: ', ui.position);
},
render: function () {
return (
<Draggable
axis="x"
handle=".handle"
constrain={constrain(25)}
start={{x: 25, y: 25}}
bound="all box"
zIndex={100}
onStart={this.handleStart}
onDrag={this.handleDrag}
onStop={this.handleStop}>
<div>
<div className="handle">Drag from here</div>
<div>Lorem ipsum...</div>
</div>
</Draggable>
);
}
});
function constrain (snap) {
function constrainOffset (offset, prev) {
var delta = offset - prev;
if (Math.abs(delta) >= snap) {
return prev + parseInt(delta / snap, 10) * snap;
}
return prev;
}
return function (pos) {
return {
top: constrainOffset(pos.top, pos.prevTop),
left: constrainOffset(pos.left, pos.prevLeft)
};
};
}
React.renderComponent(<App/>, document.body);
Contributing
- Fork the project
$ npm install && npm run watch
- Make changes
- Add appropriate tests
$ npm test
- If tests don't pass, make them pass.
- Update README with appropriate docs.
- Commit and PR
License
MIT