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

fix: various fixes back-to-top button #5419

Merged
merged 1 commit into from
Aug 26, 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,6 +7,7 @@

import React, {useRef, useState} from 'react';
import clsx from 'clsx';
import {useLocation} from '@docusaurus/router';
import useScrollPosition from '@theme/hooks/useScrollPosition';

import styles from './styles.module.css';
Expand Down Expand Up @@ -66,46 +67,49 @@ function useSmoothScrollToTop(): UseSmoothScrollTopReturn {
}

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

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) => {
// 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;

const isScrollingUp = scrollTop < lastScrollTop;
const isScrollingUp = scrollTop < lastScrollTop;

if (!isScrollingUp) {
cancelScrollToTop();
}
if (!isScrollingUp) {
cancelScrollToTop();
}

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

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

return (
<button
className={clsx('clean-btn', styles.backToTopButton, {
[styles.backToTopButtonShow]: show,
})}
type="button"
title="Scroll to top"
onClick={() => smoothScrollTop()}>
<svg viewBox="0 0 24 24" width="28">
<path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
transform: scale(0);
}

.backToTopButton:hover {
.backToTopButton:not(:focus):hover {
opacity: 0.8;
}

Expand Down