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
50 changes: 45 additions & 5 deletions client/src/components/scrolltotop.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,56 @@
import { useEffect } from "react";
import { useEffect, useState } from "react";
import { useLocation } from "react-router-dom";
import "../pages/ScrollToTop.css";

export default function ScrollToTop() {
const { pathname } = useLocation();
const [isVisible, setIsVisible] = useState(false);

// Existing route-change behavior
useEffect(() => {
window.scrollTo({
top: 0,
left: 0,
behavior: "smooth"
top: 0,
left: 0,
behavior: "smooth",
});
}, [pathname]);

return null;
// Show button after scrolling 300px
useEffect(() => {
const toggleVisibility = () => {
if (window.scrollY > 300) {
setIsVisible(true);
} else {
setIsVisible(false);
}
};

window.addEventListener("scroll", toggleVisibility);

return () => {
window.removeEventListener("scroll", toggleVisibility);
};
}, []);

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

return (
<>
{isVisible && (
<button
className="scroll-to-top"
onClick={scrollToTop}
aria-label="Scroll to top"
>
</button>
)}
</>
);
}