Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ var App = React.createClass({
//
// `grid` specifies the x and y that dragging should snap to.
//
// `minBounds` specifies the minimum x and y values.
//
// `maxBounds` specifies the maximum x and y values.
//
// `start` specifies the x and y that the dragged item should start at
//
// `zIndex` specifies the zIndex to use while dragging.
Expand All @@ -68,6 +72,8 @@ var App = React.createClass({
axis="x"
handle=".handle"
grid={[25, 25]}
minBounds={[0, 0]}
maxBounds={[500, 500]}
start={{x: 25, y: 25}}
zIndex={100}
onStart={this.handleStart}
Expand Down Expand Up @@ -98,4 +104,4 @@ React.renderComponent(<App/>, document.body);

## License

MIT
MIT
7 changes: 6 additions & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ <h1>React Draggable</h1>
<Draggable grid={[50, 50]}>
<div className="box">I snap to a 50 x 50 grid</div>
</Draggable>
<Draggable start={[250, 250]} minBounds={[0, 0]} maxBounds={[500, 500]}>
<div className="box">
<div>I am bound to the left and top at 0 and right and bottom at 500 relative to my starting point of (250, 250).</div>
</div>
</Draggable>
</div>
);
}
Expand All @@ -117,4 +122,4 @@ <h1>React Draggable</h1>
React.renderComponent(<App/>, document.body);
</script>
</body>
</html>
</html>
12 changes: 12 additions & 0 deletions lib/draggable.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,18 @@ module.exports = React.createClass({
: this.state.clientY;
}

// prevent movement past minimum bounds
if (Array.isArray(this.props.minBounds)) {
clientX = Math.max(this.props.minBounds[0], clientX);
clientY = Math.max(this.props.minBounds[1], clientY);
}

// prevent movement past maximum bounds
if (Array.isArray(this.props.maxBounds)) {
clientX = Math.min(this.props.maxBounds[0], clientX);
clientY = Math.min(this.props.maxBounds[1], clientY);
}

// Update top and left
this.setState({
clientX: clientX,
Expand Down
6 changes: 5 additions & 1 deletion specs/draggable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ describe('react-draggable', function () {
handle=".handle"
cancel=".cancel"
grid={[10, 10]}
minBounds={[0, 0]}
maxBounds={[500, 500]}
zIndex={1000}
onStart={handleStart}
onDrag={handleDrag}
Expand All @@ -43,6 +45,8 @@ describe('react-draggable', function () {
expect(drag.props.handle).toEqual('.handle');
expect(drag.props.cancel).toEqual('.cancel');
expect(drag.props.grid).toEqual([10, 10]);
expect(drag.props.minBounds).toEqual([0, 0]);
expect(drag.props.maxBounds).toEqual([500, 500]);
expect(drag.props.zIndex).toEqual(1000);
expect(drag.props.onStart).toEqual(handleStart);
expect(drag.props.onDrag).toEqual(handleDrag);
Expand Down Expand Up @@ -155,4 +159,4 @@ describe('react-draggable', function () {
expect(error).toEqual(true);
});
});
});
});