From 8ab96e780d2aaf5213029539f64962af68c0cb66 Mon Sep 17 00:00:00 2001 From: Arunarun530 Date: Wed, 24 Sep 2025 10:02:05 +0530 Subject: [PATCH] =?UTF-8?q?feat:=20add=20Scroll-to-Top=20button=20(smooth?= =?UTF-8?q?=20scroll,=20accessible)=20=E2=80=94=20closes=20#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/ScrollToTop.jsx | 38 ++++++++++++++++++++++++++++++++++++++ pages/_app.js | 4 +++- 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 components/ScrollToTop.jsx diff --git a/components/ScrollToTop.jsx b/components/ScrollToTop.jsx new file mode 100644 index 00000000..bd034641 --- /dev/null +++ b/components/ScrollToTop.jsx @@ -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 ( + + ); +}; + +export default ScrollToTop; diff --git a/pages/_app.js b/pages/_app.js index 21041ebf..fcc542f2 100644 --- a/pages/_app.js +++ b/pages/_app.js @@ -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"; @@ -33,8 +34,9 @@ function MyApp({ Component, pageProps: { session, ...pageProps } }) { )} - ; + + {/* <-- Add the scroll-to-top button here */} );