Skip to content
Open
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
29 changes: 29 additions & 0 deletions src/components/ScrollToTopButton/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React, { useState, useEffect } from "react";
import styles from "./styles.module.css";

export default function ScrollToTopButton() {
const [visible, setVisible] = useState(false);

useEffect(() => {
const onScroll = () => setVisible(window.scrollY > 300);
window.addEventListener("scroll", onScroll, { passive: true });
return () => window.removeEventListener("scroll", onScroll);
}, []);

const scrollToTop = () => {
window.scrollTo({ top: 0, behavior: "smooth" });
};

if (!visible) return null;

return (
<button
className={styles.scrollToTop}
onClick={scrollToTop}
aria-label="Scroll to top"
title="Scroll to top"
>
</button>
);
}
26 changes: 26 additions & 0 deletions src/components/ScrollToTopButton/styles.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
.scrollToTop {
position: fixed;
bottom: 2rem;
right: 2rem;
z-index: 999;
width: 2.5rem;
height: 2.5rem;
border-radius: 50%;
border: none;
background-color: var(--ifm-color-primary);
color: #fff;
font-size: 1.2rem;
line-height: 1;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
transition: background-color 0.2s, opacity 0.2s, transform 0.2s;
opacity: 0.85;
}

.scrollToTop:hover {
opacity: 1;
transform: translateY(-2px);
}
11 changes: 11 additions & 0 deletions src/theme/Root.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from "react";
import ScrollToTopButton from "@site/src/components/ScrollToTopButton";

export default function Root({ children }) {
return (
<>
{children}
<ScrollToTopButton />
</>
);
}