Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(v2): extract scroll position detection into separate hook #2627

Merged
merged 1 commit into from
Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import {useState, useCallback, useEffect} from 'react';
import {useLocation} from '@docusaurus/router';
import useLocationHash from '@theme/hooks/useLocationHash';
import useScrollPosition from '@theme/hooks/useScrollPosition';

const useHideableNavbar = (hideOnScroll) => {
const [isNavbarVisible, setIsNavbarVisible] = useState(true);
Expand All @@ -22,47 +23,41 @@ const useHideableNavbar = (hideOnScroll) => {
const location = useLocation();
const [hash, setHash] = useLocationHash(location.hash);

const handleScroll = () => {
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
useScrollPosition(
({scrollY: scrollTop}) => {
if (!hideOnScroll) {
return;
}

if (scrollTop === 0) {
setIsNavbarVisible(true);
}

if (scrollTop < navbarHeight) {
return;
}

if (isFocusedAnchor) {
setIsFocusedAnchor(false);
setIsNavbarVisible(false);
setLastScrollTop(scrollTop);
return;
}

const documentHeight =
document.documentElement.scrollHeight - navbarHeight;
const windowHeight = window.innerHeight;

if (lastScrollTop && scrollTop >= lastScrollTop) {
setIsNavbarVisible(false);
} else if (scrollTop + windowHeight < documentHeight) {
setIsNavbarVisible(true);
}

if (scrollTop === 0) {
setIsNavbarVisible(true);
}

if (scrollTop < navbarHeight) {
return;
}

if (isFocusedAnchor) {
setIsFocusedAnchor(false);
setIsNavbarVisible(false);
setLastScrollTop(scrollTop);
return;
}

const documentHeight = document.documentElement.scrollHeight - navbarHeight;
const windowHeight = window.innerHeight;

if (lastScrollTop && scrollTop >= lastScrollTop) {
setIsNavbarVisible(false);
} else if (scrollTop + windowHeight < documentHeight) {
setIsNavbarVisible(true);
}

setLastScrollTop(scrollTop);
};

useEffect(() => {
if (!hideOnScroll) {
return undefined;
}

window.addEventListener('scroll', handleScroll);

return () => {
window.removeEventListener('scroll', handleScroll);
};
}, [lastScrollTop, navbarHeight]);
},
[lastScrollTop, navbarHeight],
);

useEffect(() => {
if (!hideOnScroll) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;