diff --git a/docs/src/modules/components/AppNavDrawer.js b/docs/src/modules/components/AppNavDrawer.js index 267ab9512ec5be..ee90c9a7bfea3b 100644 --- a/docs/src/modules/components/AppNavDrawer.js +++ b/docs/src/modules/components/AppNavDrawer.js @@ -16,13 +16,13 @@ import { pageToTitleI18n } from 'docs/src/modules/utils/helpers'; import PageContext from 'docs/src/modules/components/PageContext'; import { useUserLanguage, useTranslate } from 'docs/src/modules/utils/i18n'; -let savedScrollTop = null; +const savedScrollTop = {}; function PersistScroll(props) { - const { children, enabled } = props; + const { slot, children, enabled } = props; const rootRef = React.useRef(); - React.useEffect(() => { + React.useLayoutEffect(() => { const parent = rootRef.current ? rootRef.current.parentElement : null; const activeElement = parent.querySelector('.app-drawer-active'); @@ -30,26 +30,26 @@ function PersistScroll(props) { return undefined; } + parent.scrollTop = savedScrollTop[slot]; + const activeBox = activeElement.getBoundingClientRect(); - if (savedScrollTop === null || activeBox.top - savedScrollTop < 0) { - // Center the selected item in the list container. - activeElement.scrollIntoView(); - } else { - parent.scrollTop = savedScrollTop; + if (activeBox.top < 0 || activeBox.top > window.innerHeight) { + parent.scrollTop += activeBox.top - 8 - 32; } return () => { - savedScrollTop = parent.scrollTop; + savedScrollTop[slot] = parent.scrollTop; }; - }, [enabled]); + }, [enabled, slot]); return