"use client"; import { AnimatePresence, motion } from "framer-motion"; import { useEffect, useState } from "react"; export default function ScrollToTop() { const [isVisible, setIsVisible] = useState(false); // 1. Logic to show/hide the button based on scroll position useEffect(() => { const toggleVisibility = () => { // Show button when page is scrolled down 300px if (window.scrollY > 300) { setIsVisible(true); } else { setIsVisible(false); } }; window.addEventListener("scroll", toggleVisibility); // Clean up the event listener on component unmount return () => window.removeEventListener("scroll", toggleVisibility); }, []); // 2. Function to scroll to the top smoothly const scrollToTop = () => { window.scrollTo({ top: 0, behavior: "smooth", }); }; return ( // AnimatePresence allows the component to animate out when removed from the DOM {isVisible && ( {/* Simple SVG Arrow Icon */} Scroll to top )} ); }