@@ -16,40 +16,40 @@ import { pageToTitleI18n } from 'docs/src/modules/utils/helpers';
16
16
import PageContext from 'docs/src/modules/components/PageContext' ;
17
17
import { useUserLanguage , useTranslate } from 'docs/src/modules/utils/i18n' ;
18
18
19
- let savedScrollTop = null ;
19
+ const savedScrollTop = { } ;
20
20
21
21
function PersistScroll ( props ) {
22
- const { children, enabled } = props ;
22
+ const { slot , children, enabled } = props ;
23
23
const rootRef = React . useRef ( ) ;
24
24
25
- React . useEffect ( ( ) => {
25
+ React . useLayoutEffect ( ( ) => {
26
26
const parent = rootRef . current ? rootRef . current . parentElement : null ;
27
27
const activeElement = parent . querySelector ( '.app-drawer-active' ) ;
28
28
29
29
if ( ! enabled || ! parent || ! activeElement || ! activeElement . scrollIntoView ) {
30
30
return undefined ;
31
31
}
32
32
33
+ parent . scrollTop = savedScrollTop [ slot ] ;
34
+
33
35
const activeBox = activeElement . getBoundingClientRect ( ) ;
34
36
35
- if ( savedScrollTop === null || activeBox . top - savedScrollTop < 0 ) {
36
- // Center the selected item in the list container.
37
- activeElement . scrollIntoView ( ) ;
38
- } else {
39
- parent . scrollTop = savedScrollTop ;
37
+ if ( activeBox . top < 0 || activeBox . top > window . innerHeight ) {
38
+ parent . scrollTop += activeBox . top - 8 - 32 ;
40
39
}
41
40
42
41
return ( ) => {
43
- savedScrollTop = parent . scrollTop ;
42
+ savedScrollTop [ slot ] = parent . scrollTop ;
44
43
} ;
45
- } , [ enabled ] ) ;
44
+ } , [ enabled , slot ] ) ;
46
45
47
46
return < div ref = { rootRef } > { children } </ div > ;
48
47
}
49
48
50
49
PersistScroll . propTypes = {
51
- children : PropTypes . node ,
52
- enabled : PropTypes . bool ,
50
+ children : PropTypes . node . isRequired ,
51
+ enabled : PropTypes . bool . isRequired ,
52
+ slot : PropTypes . string . isRequired ,
53
53
} ;
54
54
55
55
const styles = ( theme ) => ( {
@@ -200,10 +200,12 @@ function AppNavDrawer(props) {
200
200
keepMounted : true ,
201
201
} }
202
202
>
203
- { drawer }
203
+ < PersistScroll slot = "swipeable" enabled = { mobileOpen } >
204
+ { drawer }
205
+ </ PersistScroll >
204
206
</ SwipeableDrawer >
205
207
) : null }
206
- { disablePermanent ? null : (
208
+ { disablePermanent || mobile ? null : (
207
209
< Hidden lgDown implementation = "css" >
208
210
< Drawer
209
211
classes = { {
@@ -212,7 +214,9 @@ function AppNavDrawer(props) {
212
214
variant = "permanent"
213
215
open
214
216
>
215
- < PersistScroll enabled = { ! mobile } > { drawer } </ PersistScroll >
217
+ < PersistScroll slot = "side" enabled >
218
+ { drawer }
219
+ </ PersistScroll >
216
220
</ Drawer >
217
221
</ Hidden >
218
222
) }
0 commit comments