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
38 changes: 38 additions & 0 deletions components/ScrollToTop.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { useState, useEffect } from "react";

const ScrollToTop = () => {
const [visible, setVisible] = useState(false);

// Show button after scrolling 300px
const toggleVisible = () => {
if (window.scrollY > 300) setVisible(true);
else setVisible(false);
};

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

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

return (
<button
onClick={scrollToTop}
className={`fixed bottom-10 right-10 p-3 text-white bg-blue-600 rounded-full shadow-lg transition-opacity duration-300 ${
visible ? "opacity-100" : "opacity-0 pointer-events-none"
}`}
aria-label="Scroll to top"
>
</button>
);
};

export default ScrollToTop;
4 changes: 3 additions & 1 deletion pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Analytics } from "@vercel/analytics/react";
import { SessionProvider } from "next-auth/react";

import Layout from "../components/Layout/Layout";
import ScrollToTop from "../components/ScrollToTop"; // <-- Import here

import "../styles/external.css";
import "../styles/globals.css";
Expand Down Expand Up @@ -33,8 +34,9 @@ function MyApp({ Component, pageProps: { session, ...pageProps } }) {
</Script>
</>
)}
<Component {...pageProps} />;
<Component {...pageProps} />
<Analytics />
<ScrollToTop /> {/* <-- Add the scroll-to-top button here */}
</Layout>
</SessionProvider>
);
Expand Down