@@ -24,11 +24,11 @@ export default function NativeScrollbar({ children, onSetScrollRef, divId }: Pro
24
24
25
25
useEffect ( ( ) => {
26
26
if ( config . featureToggles . bodyScrolling && onSetScrollRef ) {
27
- onSetScrollRef ( new WindowScrollElement ( ) ) ;
27
+ onSetScrollRef ( new DivScrollElement ( document . documentElement ) ) ;
28
28
}
29
29
30
30
if ( ! config . featureToggles . bodyScrolling && ref . current && onSetScrollRef ) {
31
- onSetScrollRef ( ref . current ) ;
31
+ onSetScrollRef ( new DivScrollElement ( ref . current ) ) ;
32
32
}
33
33
} , [ ref , onSetScrollRef ] ) ;
34
34
@@ -42,13 +42,23 @@ export default function NativeScrollbar({ children, onSetScrollRef, divId }: Pro
42
42
) ;
43
43
}
44
44
45
- class WindowScrollElement {
45
+ class DivScrollElement {
46
+ public constructor ( private element : HTMLElement ) { }
46
47
public get scrollTop ( ) {
47
- return window . scrollY ;
48
+ return this . element . scrollTop ;
48
49
}
49
50
50
- public scrollTo ( x : number , y : number ) {
51
- window . scrollTo ( x , y ) ;
51
+ public scrollTo ( x : number , y : number , retry = 0 ) {
52
+ // If the element does not have the height we wait a few frames and look again
53
+ // Gives the view time to render and get the correct height before we restore scroll position
54
+ const canScroll = this . element . scrollHeight - this . element . clientHeight - y >= 0 ;
55
+
56
+ if ( ! canScroll && retry < 10 ) {
57
+ requestAnimationFrame ( ( ) => this . scrollTo ( x , y , retry + 1 ) ) ;
58
+ return ;
59
+ }
60
+
61
+ this . element . scrollTo ( x , y ) ;
52
62
}
53
63
}
54
64
0 commit comments