@@ -20,7 +20,8 @@ const isBoth = (overflow: Overflow, fn: (value: string) => boolean) =>
20
20
fn ( overflow . overflowX ) && fn ( overflow . overflowY ) ;
21
21
22
22
const isElementScrollable = ( el : Element ) : boolean => {
23
- const style : CSSStyleDeclaration = window . getComputedStyle ( el ) ;
23
+ const style : CSSStyleDeclaration =
24
+ el . ownerDocument . defaultView ! . getComputedStyle ( el ) ;
24
25
const overflow : Overflow = {
25
26
overflowX : style . overflowX ,
26
27
overflowY : style . overflowY ,
@@ -31,22 +32,23 @@ const isElementScrollable = (el: Element): boolean => {
31
32
32
33
// Special case for a body element
33
34
// Playground: https://codepen.io/alexreardon/pen/ZmyLgX?editors=1111
34
- const isBodyScrollable = ( ) : boolean => {
35
+ const isBodyScrollable = ( el : HTMLElement ) : boolean => {
35
36
// Because we always return false for now, we can skip any actual processing in production
36
37
if ( process . env . NODE_ENV === 'production' ) {
37
38
return false ;
38
39
}
39
40
40
41
const body : HTMLBodyElement = getBodyElement ( ) ;
41
- const html : HTMLElement | null = document . documentElement ;
42
+ const html : HTMLElement | null = el . ownerDocument . documentElement ;
42
43
invariant ( html ) ;
43
44
44
45
// 1. The `body` has `overflow-[x|y]: auto | scroll`
45
46
if ( ! isElementScrollable ( body ) ) {
46
47
return false ;
47
48
}
48
49
49
- const htmlStyle : CSSStyleDeclaration = window . getComputedStyle ( html ) ;
50
+ const htmlStyle : CSSStyleDeclaration =
51
+ el . ownerDocument . defaultView ! . getComputedStyle ( html ) ;
50
52
const htmlOverflow : Overflow = {
51
53
overflowX : htmlStyle . overflowX ,
52
54
overflowY : htmlStyle . overflowY ,
@@ -76,12 +78,20 @@ const getClosestScrollable = (el?: HTMLElement | null): HTMLElement | null => {
76
78
}
77
79
78
80
// not allowing us to go higher then body
79
- if ( el === document . body ) {
80
- return isBodyScrollable ( ) ? el : null ;
81
+ if ( el === el . ownerDocument . body ) {
82
+ if ( isBodyScrollable ( el ) ) {
83
+ return el ;
84
+ }
85
+
86
+ if ( el . ownerDocument . defaultView ?. frameElement ) {
87
+ return el . ownerDocument . defaultView ?. frameElement as HTMLElement ;
88
+ }
89
+
90
+ return null ;
81
91
}
82
92
83
93
// Should never get here, but just being safe
84
- if ( el === document . documentElement ) {
94
+ if ( el === el . ownerDocument . documentElement ) {
85
95
return null ;
86
96
}
87
97
0 commit comments