Skip to content

Commit 5ae7dbb

Browse files
authored
fix: correct document when inside new window or iframe (#45)
1 parent 721b07a commit 5ae7dbb

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

src/main.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ export class Draggable {
4949
return (typeof window !== 'undefined') && window.PointerEvent;
5050
}
5151

52+
get document() {
53+
return this._element
54+
? this._element.ownerDocument
55+
: document;
56+
}
57+
5258
constructor({ press = noop, drag = noop, release = noop, mouseOnly = false }) {
5359
this._pressHandler = proxy(normalizeEvent, press);
5460
this._dragHandler = proxy(normalizeEvent, drag);
@@ -89,8 +95,8 @@ export class Draggable {
8995
return;
9096
}
9197

92-
bind(document, "mousemove", this._mousemove);
93-
bind(document, "mouseup", this._mouseup);
98+
bind(this.document, "mousemove", this._mousemove);
99+
bind(this.document, "mouseup", this._mouseup);
94100
this._pressHandler(e);
95101
};
96102

@@ -99,17 +105,17 @@ export class Draggable {
99105
};
100106

101107
this._mouseup = (e) => {
102-
unbind(document, "mousemove", this._mousemove);
103-
unbind(document, "mouseup", this._mouseup);
108+
unbind(this.document, "mousemove", this._mousemove);
109+
unbind(this.document, "mouseup", this._mouseup);
104110
this._releaseHandler(e);
105111
};
106112

107113
this._pointerdown = (e) => {
108114
if (e.isPrimary && e.button === 0) {
109-
bind(document, "pointermove", this._pointermove);
110-
bind(document, "pointerup", this._pointerup);
111-
bind(document, "pointercancel", this._pointerup);
112-
bind(document, "contextmenu", preventDefault);
115+
bind(this.document, "pointermove", this._pointermove);
116+
bind(this.document, "pointerup", this._pointerup);
117+
bind(this.document, "pointercancel", this._pointerup);
118+
bind(this.document, "contextmenu", preventDefault);
113119

114120
this._pressHandler(e);
115121
}
@@ -123,10 +129,10 @@ export class Draggable {
123129

124130
this._pointerup = (e) => {
125131
if (e.isPrimary) {
126-
unbind(document, "pointermove", this._pointermove);
127-
unbind(document, "pointerup", this._pointerup);
128-
unbind(document, "pointercancel", this._pointerup);
129-
unbind(document, "contextmenu", preventDefault);
132+
unbind(this.document, "pointermove", this._pointermove);
133+
unbind(this.document, "pointerup", this._pointerup);
134+
unbind(this.document, "pointercancel", this._pointerup);
135+
unbind(this.document, "contextmenu", preventDefault);
130136

131137
this._releaseHandler(e);
132138
}
@@ -168,10 +174,10 @@ export class Draggable {
168174

169175
if (this._usePointers()) {
170176
unbind(element, "pointerdown", this._pointerdown);
171-
unbind(document, "pointermove", this._pointermove);
172-
unbind(document, "pointerup", this._pointerup);
173-
unbind(document, "contextmenu", preventDefault);
174-
unbind(document, "pointercancel", this._pointerup);
177+
unbind(this.document, "pointermove", this._pointermove);
178+
unbind(this.document, "pointerup", this._pointerup);
179+
unbind(this.document, "contextmenu", preventDefault);
180+
unbind(this.document, "pointercancel", this._pointerup);
175181
return;
176182
}
177183

0 commit comments

Comments
 (0)