@@ -2,6 +2,7 @@ import React, { useRef, useMemo, useEffect } from "react";
2
2
3
3
import { useEventCallback } from "../../hooks/useEventCallback" ;
4
4
import { clamp } from "../../utils/clamp" ;
5
+
5
6
export interface Interaction {
6
7
left : number ;
7
8
top : number ;
@@ -18,6 +19,11 @@ const getTouchPoint = (touches: TouchList, touchId: null | number): Touch => {
18
19
return touches [ 0 ] ;
19
20
} ;
20
21
22
+ // Finds the proper window object to fix iframe embedding issues
23
+ const getParentWindow = ( node ?: HTMLDivElement | null ) : Window => {
24
+ return ( node && node . ownerDocument . defaultView ) || self ;
25
+ } ;
26
+
21
27
// Returns a relative position of the pointer inside the node's bounding box
22
28
const getRelativePosition = (
23
29
node : HTMLDivElement ,
@@ -28,11 +34,10 @@ const getRelativePosition = (
28
34
29
35
// Get user's pointer position from `touches` array if it's a `TouchEvent`
30
36
const pointer = isTouch ( event ) ? getTouchPoint ( event . touches , touchId ) : ( event as MouseEvent ) ;
31
- const nodeWindow = node . ownerDocument . defaultView || window ;
32
37
33
38
return {
34
- left : clamp ( ( pointer . pageX - ( rect . left + nodeWindow . pageXOffset ) ) / rect . width ) ,
35
- top : clamp ( ( pointer . pageY - ( rect . top + nodeWindow . pageYOffset ) ) / rect . height ) ,
39
+ left : clamp ( ( pointer . pageX - ( rect . left + getParentWindow ( node ) . pageXOffset ) ) / rect . width ) ,
40
+ top : clamp ( ( pointer . pageY - ( rect . top + getParentWindow ( node ) . pageYOffset ) ) / rect . height ) ,
36
41
} ;
37
42
} ;
38
43
@@ -122,12 +127,10 @@ const InteractiveBase = ({ onMove, onKey, ...rest }: Props) => {
122
127
function toggleDocumentEvents ( state ?: boolean ) {
123
128
const touch = hasTouch . current ;
124
129
const el = container . current ;
125
- const containerWindow = ( el && el . ownerDocument . defaultView ) || self ;
130
+ const parentWindow = getParentWindow ( el ) ;
126
131
127
- // add or remove additional pointer event listeners
128
- const toggleEvent = state
129
- ? containerWindow . addEventListener
130
- : containerWindow . removeEventListener ;
132
+ // Add or remove additional pointer event listeners
133
+ const toggleEvent = state ? parentWindow . addEventListener : parentWindow . removeEventListener ;
131
134
toggleEvent ( touch ? "touchmove" : "mousemove" , handleMove ) ;
132
135
toggleEvent ( touch ? "touchend" : "mouseup" , handleMoveEnd ) ;
133
136
}
0 commit comments