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

Improve scroll-to-top button #1016

Merged
merged 1 commit into from
Jul 9, 2024
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
34 changes: 15 additions & 19 deletions src/app/components/site/ScrollToTop.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
import React, {useEffect, useRef} from "react";
import React, { useCallback, useEffect } from "react";
import classNames from "classnames";

export const ScrollToTop = ({mainContent}: {mainContent: React.RefObject<any>}) => {
const scrollButton = useRef(null);
export const ScrollToTop = ({mainContent}: {mainContent: React.MutableRefObject<HTMLElement | null>}) => {
const [sticky, setSticky] = React.useState(false);

const isSticky = () => {
const scrollTop = window.scrollY;
// @ts-ignore
scrollTop >= mainContent.current.offsetTop + 1 ? scrollButton.current.classList.add('is-sticky') : scrollButton.current.classList.remove('is-sticky');
}

const scroll = () => {
mainContent.current.scrollIntoView({behavior: 'smooth'});
}
const isSticky = useCallback(() => {
setSticky(!!mainContent.current && window.scrollY >= mainContent.current.offsetHeight * 0.2);
}, [mainContent]);

useEffect(() => {
window.addEventListener('scroll', isSticky);
return () => {
window.removeEventListener('scroll', isSticky);
};
});
}, [isSticky]);

return <div ref={scrollButton} onClick={scroll} className="scroll-btn d-print-none">
<button>
<img src="/assets/common/icons/chevron-up.svg" alt="Scroll to top of page"/>
</button>
</div>
}
return <button
onClick={() => mainContent.current?.scrollIntoView({behavior: 'smooth'})}
className={classNames("scroll-btn d-print-none", {"is-sticky": sticky})}
>
<img src="/assets/common/icons/chevron-up.svg" alt="Scroll to top of page"/>
</button>;
};
54 changes: 20 additions & 34 deletions src/scss/common/scroll-button.scss
Original file line number Diff line number Diff line change
@@ -1,40 +1,26 @@
$width: 50px;
$height: 50px;
$color: #ffffff;

.scroll-btn {
visibility: hidden; // If not in use hide
height: 0;
display: grid;
box-shadow: 0 2px 24px 0 rgb(0 0 0 / 15%);
background-color: $color !important;
justify-content: center;
align-content: center;
}

.scroll-btn img {
height: 0.75 * $height;
width: 0.75 * $width;
align-self: center;
}

.scroll-btn button {
// Alpha channel 0 to make transparent
background: #00000000;
}

.is-sticky {
visibility: visible;
position: fixed;
opacity: 0; // If not in use hide
height: 45px;
width: 45px;
display: flex;
background-color: #fff;
justify-content: center;
align-items: center;
bottom: 10px;
right: 10px;
z-index: 999;
box-shadow: 0 2px 24px 0 rgb(0 0 0 / 15%);
background-color: $color !important;
box-shadow: 2px 2px 10px 0 #0004;
background-color: #fff !important;
border-radius: 50%;
height: $height;
width: $width;
animation: 500ms ease-in-out 0s normal none 1 running fadeInDown;
padding-top: 0;
padding-bottom: 0;
transition: opacity 0.1s linear;
z-index: 999;
img {
margin-top: -4px;
height: 80%;
width: 80%;
}
}

.is-sticky {
opacity: 1;
}