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: cleanup scroll handlers #5709

Merged
merged 2 commits into from
Oct 15, 2021
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 @@ -7,11 +7,14 @@

import React, {useRef, useState} from 'react';
import clsx from 'clsx';
import {useLocation} from '@docusaurus/router';
import {translate} from '@docusaurus/Translate';

import styles from './styles.module.css';
import {ThemeClassNames, useScrollPosition} from '@docusaurus/theme-common';
import {
ThemeClassNames,
useScrollPosition,
useLocationChange,
} from '@docusaurus/theme-common';

const threshold = 300;

Expand Down Expand Up @@ -68,42 +71,52 @@ function useSmoothScrollToTop(): UseSmoothScrollTopReturn {
}

function BackToTopButton(): JSX.Element {
const location = useLocation();
const {smoothScrollTop, cancelScrollToTop} = useSmoothScrollToTop();
const [show, setShow] = useState(false);
const isFocusedAnchor = useRef(false);
const {smoothScrollTop, cancelScrollToTop} = useSmoothScrollToTop();

useScrollPosition(
({scrollY: scrollTop}, lastPosition) => {
// No lastPosition means component is just being mounted.
// Not really a scroll event from the user, so we ignore it
if (!lastPosition) {
return;
}
const lastScrollTop = lastPosition.scrollY;
useScrollPosition(({scrollY: scrollTop}, lastPosition) => {
const lastScrollTop = lastPosition?.scrollY;

const isScrollingUp = scrollTop < lastScrollTop;
// No lastScrollTop means component is just being mounted.
// Not really a scroll event from the user, so we ignore it
if (!lastScrollTop) {
return;
}

if (!isScrollingUp) {
cancelScrollToTop();
}
if (isFocusedAnchor.current) {
isFocusedAnchor.current = false;
return;
}

if (scrollTop < threshold) {
setShow(false);
return;
}
const isScrollingUp = scrollTop < lastScrollTop;

if (isScrollingUp) {
const documentHeight = document.documentElement.scrollHeight;
const windowHeight = window.innerHeight;
if (scrollTop + windowHeight < documentHeight) {
setShow(true);
}
} else {
setShow(false);
if (!isScrollingUp) {
cancelScrollToTop();
}

if (scrollTop < threshold) {
setShow(false);
return;
}

if (isScrollingUp) {
const documentHeight = document.documentElement.scrollHeight;
const windowHeight = window.innerHeight;
if (scrollTop + windowHeight < documentHeight) {
setShow(true);
}
},
[location],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To check location we now use useLocationChange hook everywhere, this will better distinguish between scroll and location event handlers.

);
} else {
setShow(false);
}
});

useLocationChange((locationChangeEvent) => {
if (locationChangeEvent.location.hash) {
isFocusedAnchor.current = true;
setShow(false);
}
});

return (
<button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,77 +5,56 @@
* LICENSE file in the root directory of this source tree.
*/

import {useState, useCallback, useEffect, useRef} from 'react';
import {useLocation} from '@docusaurus/router';
import {useState, useCallback, useRef} from 'react';
import {useLocationChange, useScrollPosition} from '@docusaurus/theme-common';
import type {useHideableNavbarReturns} from '@theme/hooks/useHideableNavbar';

const useHideableNavbar = (hideOnScroll: boolean): useHideableNavbarReturns => {
const location = useLocation();
const [isNavbarVisible, setIsNavbarVisible] = useState(hideOnScroll);
const isFocusedAnchor = useRef(false);
const [navbarHeight, setNavbarHeight] = useState(0);
const navbarHeight = useRef(0);
const navbarRef = useCallback((node: HTMLElement | null) => {
if (node !== null) {
setNavbarHeight(node.getBoundingClientRect().height);
navbarHeight.current = node.getBoundingClientRect().height;
}
}, []);

useScrollPosition(
(currentPosition, lastPosition) => {
const scrollTop = currentPosition.scrollY;
const lastScrollTop = lastPosition?.scrollY;
if (!hideOnScroll) {
return;
}

if (scrollTop < navbarHeight) {
setIsNavbarVisible(true);
return;
}

if (isFocusedAnchor.current) {
isFocusedAnchor.current = false;
setIsNavbarVisible(false);
return;
}

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

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

if (lastScrollTop && scrollTop >= lastScrollTop) {
setIsNavbarVisible(false);
} else if (scrollTop + windowHeight < documentHeight) {
setIsNavbarVisible(true);
}
},
[navbarHeight, isFocusedAnchor],
);
useScrollPosition((currentPosition, lastPosition) => {
if (!hideOnScroll) {
return;
}

useLocationChange((locationChangeEvent) => {
if (!hideOnScroll || locationChangeEvent.location.hash) {
if (isFocusedAnchor.current) {
isFocusedAnchor.current = false;
return;
}

setIsNavbarVisible(true);
const scrollTop = currentPosition.scrollY;
const lastScrollTop = lastPosition?.scrollY;
const documentHeight =
document.documentElement.scrollHeight - navbarHeight.current;
const windowHeight = window.innerHeight;

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

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

if (!location.hash) {
if (locationChangeEvent.location.hash) {
isFocusedAnchor.current = true;
setIsNavbarVisible(false);
return;
}

isFocusedAnchor.current = true;
}, [location.hash]);
setIsNavbarVisible(true);
});

return {
navbarRef,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {useEffect, useRef} from 'react';
import {useEffect} from 'react';
import {useLocation} from '@docusaurus/router';
import {Location} from '@docusaurus/history';
import {usePrevious} from './usePrevious';
Expand All @@ -20,15 +20,8 @@ type OnLocationChange = (locationChangeEvent: LocationChangeEvent) => void;
export function useLocationChange(onLocationChange: OnLocationChange): void {
const location = useLocation();
const previousLocation = usePrevious(location);
const isFirst = useRef<boolean>(true);

useEffect(() => {
// Prevent first effect to trigger the listener on mount
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unnecessary code, it used to be needed to fix #5020, but it is safe to remove now.

if (isFirst.current) {
isFirst.current = false;
return;
}

onLocationChange({
location,
previousLocation,
Expand Down