"use client"; import { useEffect, useState } from "react"; import { AnimatePresence, motion, useReducedMotion } from "motion/react"; import { ArrowUp } from "lucide-react"; export function BackToTopButton() { const [visible, setVisible] = useState(false); const prefersReducedMotion = useReducedMotion(); useEffect(() => { let frame = 0; const updateVisibility = () => { frame = 0; setVisible(window.scrollY > 520); }; const handleScroll = () => { if (frame) { return; } frame = window.requestAnimationFrame(updateVisibility); }; updateVisibility(); window.addEventListener("scroll", handleScroll, { passive: true }); return () => { window.removeEventListener("scroll", handleScroll); if (frame) { window.cancelAnimationFrame(frame); } }; }, []); const scrollToTop = () => { window.scrollTo({ top: 0, behavior: prefersReducedMotion ? "auto" : "smooth", }); }; return ( {visible ? ( ) : null} ); }