Skip to content

Commit 7aa0195

Browse files
acustiaeneasr
authored andcommitted
Use window/document relative to DOM element
Use the ownerDocument and window of the DOM element being manipulated rather than the global window and document objects. This makes react-draggable work with a tool like https://github.com/ryanseddon/react-frame-component
1 parent 510d27c commit 7aa0195

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

lib/DraggableCore.es6

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,11 @@ export default class DraggableCore extends React.Component {
171171
componentWillUnmount() {
172172
// Remove any leftover event handlers. Remove both touch and mouse handlers in case
173173
// some browser quirk caused a touch event to fire during a mouse move, or vice versa.
174-
removeEvent(document, eventsFor.mouse.move, this.handleDrag);
175-
removeEvent(document, eventsFor.touch.move, this.handleDrag);
176-
removeEvent(document, eventsFor.mouse.stop, this.handleDragStop);
177-
removeEvent(document, eventsFor.touch.stop, this.handleDragStop);
174+
const {ownerDocument} = ReactDOM.findDOMNode(this);
175+
removeEvent(ownerDocument, eventsFor.mouse.move, this.handleDrag);
176+
removeEvent(ownerDocument, eventsFor.touch.move, this.handleDrag);
177+
removeEvent(ownerDocument, eventsFor.mouse.stop, this.handleDragStop);
178+
removeEvent(ownerDocument, eventsFor.touch.stop, this.handleDragStop);
178179
if (this.props.enableUserSelectHack) removeUserSelectStyles();
179180
}
180181

@@ -185,11 +186,12 @@ export default class DraggableCore extends React.Component {
185186
// Only accept left-clicks.
186187
if (!this.props.allowAnyClick && typeof e.button === 'number' && e.button !== 0) return false;
187188

189+
const domNode = ReactDOM.findDOMNode(this);
188190
// Short circuit if handle or cancel prop was provided and selector doesn't match.
189191
if (this.props.disabled ||
190-
(!(e.target instanceof Node)) ||
191-
(this.props.handle && !matchesSelectorAndParentsTo(e.target, this.props.handle, ReactDOM.findDOMNode(this))) ||
192-
(this.props.cancel && matchesSelectorAndParentsTo(e.target, this.props.cancel, ReactDOM.findDOMNode(this)))) {
192+
(!(e.target instanceof domNode.ownerDocument.defaultView.Node)) ||
193+
(this.props.handle && !matchesSelectorAndParentsTo(e.target, this.props.handle, domNode)) ||
194+
(this.props.cancel && matchesSelectorAndParentsTo(e.target, this.props.cancel, domNode))) {
193195
return;
194196
}
195197

@@ -231,8 +233,8 @@ export default class DraggableCore extends React.Component {
231233
// Add events to the document directly so we catch when the user's mouse/touch moves outside of
232234
// this element. We use different events depending on whether or not we have detected that this
233235
// is a touch-capable device.
234-
addEvent(document, dragEventFor.move, this.handleDrag);
235-
addEvent(document, dragEventFor.stop, this.handleDragStop);
236+
addEvent(domNode.ownerDocument, dragEventFor.move, this.handleDrag);
237+
addEvent(domNode.ownerDocument, dragEventFor.stop, this.handleDragStop);
236238
};
237239

238240
handleDrag: EventHandler<MouseEvent> = (e) => {
@@ -302,9 +304,10 @@ export default class DraggableCore extends React.Component {
302304
this.props.onStop(e, coreEvent);
303305

304306
// Remove event handlers
307+
const {ownerDocument} = ReactDOM.findDOMNode(this);
305308
log('DraggableCore: Removing handlers');
306-
removeEvent(document, dragEventFor.move, this.handleDrag);
307-
removeEvent(document, dragEventFor.stop, this.handleDragStop);
309+
removeEvent(ownerDocument, dragEventFor.move, this.handleDrag);
310+
removeEvent(ownerDocument, dragEventFor.stop, this.handleDragStop);
308311
};
309312

310313
onMouseDown: EventHandler<MouseEvent> = (e) => {

lib/utils/domFns.es6

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export function outerHeight(node: HTMLElement): number {
6363
// This is deliberately excluding margin for our calculations, since we are using
6464
// offsetTop which is including margin. See getBoundPosition
6565
let height = node.clientHeight;
66-
const computedStyle = window.getComputedStyle(node);
66+
const computedStyle = node.ownerDocument.defaultView.getComputedStyle(node);
6767
height += int(computedStyle.borderTopWidth);
6868
height += int(computedStyle.borderBottomWidth);
6969
return height;
@@ -73,31 +73,31 @@ export function outerWidth(node: HTMLElement): number {
7373
// This is deliberately excluding margin for our calculations, since we are using
7474
// offsetLeft which is including margin. See getBoundPosition
7575
let width = node.clientWidth;
76-
const computedStyle = window.getComputedStyle(node);
76+
const computedStyle = node.ownerDocument.defaultView.getComputedStyle(node);
7777
width += int(computedStyle.borderLeftWidth);
7878
width += int(computedStyle.borderRightWidth);
7979
return width;
8080
}
8181
export function innerHeight(node: HTMLElement): number {
8282
let height = node.clientHeight;
83-
const computedStyle = window.getComputedStyle(node);
83+
const computedStyle = node.ownerDocument.defaultView.getComputedStyle(node);
8484
height -= int(computedStyle.paddingTop);
8585
height -= int(computedStyle.paddingBottom);
8686
return height;
8787
}
8888

8989
export function innerWidth(node: HTMLElement): number {
9090
let width = node.clientWidth;
91-
const computedStyle = window.getComputedStyle(node);
91+
const computedStyle = node.ownerDocument.defaultView.getComputedStyle(node);
9292
width -= int(computedStyle.paddingLeft);
9393
width -= int(computedStyle.paddingRight);
9494
return width;
9595
}
9696

9797
// Get from offsetParent
9898
export function offsetXYFromParentOf(evt: {clientX: number, clientY: number}, node: HTMLElement & {offsetParent: HTMLElement}): ControlPosition {
99-
const offsetParent = node.offsetParent || document.body;
100-
const offsetParentRect = node.offsetParent === document.body ? {left: 0, top: 0} : offsetParent.getBoundingClientRect();
99+
const offsetParent = node.offsetParent || node.ownerDocument.body;
100+
const offsetParentRect = node.offsetParent === node.ownerDocument.body ? {left: 0, top: 0} : offsetParent.getBoundingClientRect();
101101

102102
const x = evt.clientX + offsetParent.scrollLeft - offsetParentRect.left;
103103
const y = evt.clientY + offsetParent.scrollTop - offsetParentRect.top;

0 commit comments

Comments
 (0)