Skip to content

Commit 94c4f43

Browse files
committed
Allow arbitrary bounds selector
1 parent bc321d8 commit 94c4f43

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

example/index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
<meta charset="utf-8"/>
55
<title>React Draggable</title>
66
<style type="text/css">
7+
html, body {
8+
height: 100%;
9+
}
10+
711
body {
812
color: #222;
913
font-family: "Helvetica Neue", sans-serif;
@@ -142,6 +146,11 @@ <h1>React Draggable</h1>
142146
</div>
143147
</Draggable>
144148
</div>
149+
<Draggable bounds="body" {...drags}>
150+
<div className="box">
151+
I can only be moved within the confines of the body element.
152+
</div>
153+
</Draggable>
145154
<Draggable>
146155
<div className="box" style={{position: 'absolute', bottom: '100px', right: '100px'}} {...drags}>
147156
I already have an absolute position.

lib/Draggable.es6

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ export default class Draggable extends DraggableCore {
6161
top: PropTypes.Number,
6262
bottom: PropTypes.Number
6363
}),
64-
PropTypes.oneOf(['parent', false])
64+
PropTypes.string,
65+
PropTypes.oneOf([false])
6566
]),
6667

6768
/**

lib/utils/positionFns.es6

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,25 @@ export function getBoundPosition(draggable, clientX, clientY) {
88

99
let bounds = JSON.parse(JSON.stringify(draggable.props.bounds));
1010
let node = ReactDOM.findDOMNode(draggable);
11-
let parent = node.parentNode;
1211

13-
if (bounds === 'parent') {
12+
if (typeof bounds === 'string') {
13+
let boundNode;
14+
if (bounds === 'parent') {
15+
boundNode = node.parentNode;
16+
} else {
17+
boundNode = document.querySelector(bounds);
18+
if (!boundNode) throw new Error('Bounds selector "' + bounds + '" could not find an element.');
19+
}
1420
let nodeStyle = window.getComputedStyle(node);
15-
let parentStyle = window.getComputedStyle(parent);
21+
let boundNodeStyle = window.getComputedStyle(boundNode);
1622
// Compute bounds. This is a pain with padding and offsets but this gets it exactly right.
1723
bounds = {
18-
left: -node.offsetLeft + int(parentStyle.paddingLeft) +
24+
left: -node.offsetLeft + int(boundNodeStyle.paddingLeft) +
1925
int(nodeStyle.borderLeftWidth) + int(nodeStyle.marginLeft),
20-
top: -node.offsetTop + int(parentStyle.paddingTop) +
26+
top: -node.offsetTop + int(boundNodeStyle.paddingTop) +
2127
int(nodeStyle.borderTopWidth) + int(nodeStyle.marginTop),
22-
right: innerWidth(parent) - outerWidth(node) - node.offsetLeft,
23-
bottom: innerHeight(parent) - outerHeight(node) - node.offsetTop
28+
right: innerWidth(boundNode) - outerWidth(node) - node.offsetLeft,
29+
bottom: innerHeight(boundNode) - outerHeight(node) - node.offsetTop
2430
};
2531
}
2632

0 commit comments

Comments
 (0)