refactor(v2): disable scroll when sidebar menu is open (#2189)

This commit is contained in:
Alexey Pyltsyn 2020-01-08 18:06:07 +03:00 committed by Yangshun Tay
parent 26fa885ef6
commit 7e750de6fa
3 changed files with 26 additions and 2 deletions

View file

@ -9,6 +9,7 @@ import React, {useState, useCallback} from 'react';
import classnames from 'classnames';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import useBaseUrl from '@docusaurus/useBaseUrl';
import useLockBodyScroll from '@theme/hooks/useLockBodyScroll';
import Link from '@docusaurus/Link';
import isInternalUrl from '@docusaurus/utils'; // eslint-disable-line import/no-extraneous-dependencies
@ -125,6 +126,8 @@ function DocSidebar(props) {
sidebarCollapsible,
} = props;
useLockBodyScroll(showResponsiveSidebar);
if (!currentSidebar) {
return null;
}

View file

@ -17,6 +17,7 @@ import classnames from 'classnames';
import useThemeContext from '@theme/hooks/useThemeContext';
import useHideableNavbar from '@theme/hooks/useHideableNavbar';
import useLockBodyScroll from '@theme/hooks/useLockBodyScroll';
import styles from './styles.module.css';
@ -54,12 +55,12 @@ function Navbar() {
const {theme, setTheme} = useThemeContext();
const {navbarRef, isNavbarVisible} = useHideableNavbar(hideOnScroll);
useLockBodyScroll(sidebarShown);
const showSidebar = useCallback(() => {
document.body.style.overflow = 'hidden';
setSidebarShown(true);
}, [setSidebarShown]);
const hideSidebar = useCallback(() => {
document.body.style.overflow = 'visible';
setSidebarShown(false);
}, [setSidebarShown]);

View file

@ -0,0 +1,20 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {useEffect} from 'react';
function useLockBodyScroll(lock = true) {
useEffect(() => {
document.body.style.overflow = lock ? 'hidden' : 'visible';
return () => {
document.body.style.overflow = 'visible';
};
}, [lock]);
}
export default useLockBodyScroll;