Skip to content
Open
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
21 changes: 15 additions & 6 deletions frontend/src/ts/components/layout/footer/ScrollToTop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@ import { Fa } from "../../common/Fa";

export function ScrollToTop(): JSXElement {
const [visible, setVisible] = createSignal(false);
const [scrolling, setScrolling] = createSignal(false);

const handleScroll = (): void => {
if (getActivePage() === "test") return;
if (getActivePage() === "test" || scrolling()) return;

const scroll = window.scrollY;
setVisible(scroll > 100);
};

const scrollUp = async (): Promise<void> => {
const scrollEnded = new Promise<void>((resolve) => {
if (window.scrollY === 0) return resolve();
window.addEventListener("scrollend", () => resolve(), { once: true });
});
window.scrollTo({ top: 0, behavior: "smooth" });
await scrollEnded;
};

onMount(() => {
window.addEventListener("scroll", handleScroll, { passive: true });
handleScroll();
Expand All @@ -35,12 +45,11 @@ export function ScrollToTop(): JSXElement {
"opacity-0": getActivePage() === "test" || !visible(),
"pointer-events-none": getActivePage() === "test" || !visible(),
}}
onClick={() => {
onClick={async () => {
setVisible(false);
window.scrollTo({
top: 0,
behavior: "smooth",
});
setScrolling(true);
await scrollUp();
setScrolling(false);
}}
>
<Fa icon="fa-angle-double-up" />
Expand Down
Loading