mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-06 21:57:14 +02:00
refactor(v2): extract scroll position detection into separate hook (#2627)
This commit is contained in:
parent
41b3c66f5e
commit
4367197c77
2 changed files with 73 additions and 37 deletions
|
@ -8,6 +8,7 @@
|
||||||
import {useState, useCallback, useEffect} from 'react';
|
import {useState, useCallback, useEffect} from 'react';
|
||||||
import {useLocation} from '@docusaurus/router';
|
import {useLocation} from '@docusaurus/router';
|
||||||
import useLocationHash from '@theme/hooks/useLocationHash';
|
import useLocationHash from '@theme/hooks/useLocationHash';
|
||||||
|
import useScrollPosition from '@theme/hooks/useScrollPosition';
|
||||||
|
|
||||||
const useHideableNavbar = (hideOnScroll) => {
|
const useHideableNavbar = (hideOnScroll) => {
|
||||||
const [isNavbarVisible, setIsNavbarVisible] = useState(true);
|
const [isNavbarVisible, setIsNavbarVisible] = useState(true);
|
||||||
|
@ -22,8 +23,11 @@ const useHideableNavbar = (hideOnScroll) => {
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const [hash, setHash] = useLocationHash(location.hash);
|
const [hash, setHash] = useLocationHash(location.hash);
|
||||||
|
|
||||||
const handleScroll = () => {
|
useScrollPosition(
|
||||||
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
|
({scrollY: scrollTop}) => {
|
||||||
|
if (!hideOnScroll) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (scrollTop === 0) {
|
if (scrollTop === 0) {
|
||||||
setIsNavbarVisible(true);
|
setIsNavbarVisible(true);
|
||||||
|
@ -40,7 +44,8 @@ const useHideableNavbar = (hideOnScroll) => {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const documentHeight = document.documentElement.scrollHeight - navbarHeight;
|
const documentHeight =
|
||||||
|
document.documentElement.scrollHeight - navbarHeight;
|
||||||
const windowHeight = window.innerHeight;
|
const windowHeight = window.innerHeight;
|
||||||
|
|
||||||
if (lastScrollTop && scrollTop >= lastScrollTop) {
|
if (lastScrollTop && scrollTop >= lastScrollTop) {
|
||||||
|
@ -50,19 +55,9 @@ const useHideableNavbar = (hideOnScroll) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
setLastScrollTop(scrollTop);
|
setLastScrollTop(scrollTop);
|
||||||
};
|
},
|
||||||
|
[lastScrollTop, navbarHeight],
|
||||||
useEffect(() => {
|
);
|
||||||
if (!hideOnScroll) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
window.addEventListener('scroll', handleScroll);
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
window.removeEventListener('scroll', handleScroll);
|
|
||||||
};
|
|
||||||
}, [lastScrollTop, navbarHeight]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!hideOnScroll) {
|
if (!hideOnScroll) {
|
||||||
|
|
|
@ -0,0 +1,41 @@
|
||||||
|
/**
|
||||||
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the MIT license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import {useState, useEffect} from 'react';
|
||||||
|
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
|
||||||
|
|
||||||
|
const getScrollPosition = () => ({
|
||||||
|
scrollX: ExecutionEnvironment.canUseDOM ? window.pageXOffset : 0,
|
||||||
|
scrollY: ExecutionEnvironment.canUseDOM ? window.pageYOffset : 0,
|
||||||
|
});
|
||||||
|
|
||||||
|
const useScrollPosition = (effect, deps = []) => {
|
||||||
|
const [scrollPosition, setScrollPosition] = useState(getScrollPosition());
|
||||||
|
|
||||||
|
const handleScroll = () => {
|
||||||
|
const currentScrollPosition = getScrollPosition();
|
||||||
|
|
||||||
|
setScrollPosition(currentScrollPosition);
|
||||||
|
|
||||||
|
if (effect) {
|
||||||
|
effect(currentScrollPosition);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
window.addEventListener('scroll', handleScroll);
|
||||||
|
|
||||||
|
return () =>
|
||||||
|
window.removeEventListener('scroll', handleScroll, {
|
||||||
|
passive: true,
|
||||||
|
});
|
||||||
|
}, deps);
|
||||||
|
|
||||||
|
return scrollPosition;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default useScrollPosition;
|
Loading…
Add table
Reference in a new issue