@@ -2,6 +2,7 @@ import React, { useRef, useMemo, useEffect } from "react";
22
33import { useEventCallback } from "../../hooks/useEventCallback" ;
44import { clamp } from "../../utils/clamp" ;
5+
56export interface Interaction {
67 left : number ;
78 top : number ;
@@ -18,6 +19,11 @@ const getTouchPoint = (touches: TouchList, touchId: null | number): Touch => {
1819 return touches [ 0 ] ;
1920} ;
2021
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+
2127// Returns a relative position of the pointer inside the node's bounding box
2228const getRelativePosition = (
2329 node : HTMLDivElement ,
@@ -28,11 +34,10 @@ const getRelativePosition = (
2834
2935 // Get user's pointer position from `touches` array if it's a `TouchEvent`
3036 const pointer = isTouch ( event ) ? getTouchPoint ( event . touches , touchId ) : ( event as MouseEvent ) ;
31- const nodeWindow = node . ownerDocument . defaultView || window ;
3237
3338 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 ) ,
3641 } ;
3742} ;
3843
@@ -122,12 +127,10 @@ const InteractiveBase = ({ onMove, onKey, ...rest }: Props) => {
122127 function toggleDocumentEvents ( state ?: boolean ) {
123128 const touch = hasTouch . current ;
124129 const el = container . current ;
125- const containerWindow = ( el && el . ownerDocument . defaultView ) || self ;
130+ const parentWindow = getParentWindow ( el ) ;
126131
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 ;
131134 toggleEvent ( touch ? "touchmove" : "mousemove" , handleMove ) ;
132135 toggleEvent ( touch ? "touchend" : "mouseup" , handleMoveEnd ) ;
133136 }
0 commit comments