import React, { useState, useEffect } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; import { ArrowUpCircle } from 'lucide-react'; import { Button } from '@/components/ui/button'; const ScrollToTopButton = () => { const [isVisible, setIsVisible] = useState(false); const toggleVisibility = () => { if (window.pageYOffset > 300) { setIsVisible(true); } else { setIsVisible(false); } }; const scrollToTop = () => { // Get the current scroll position const startPosition = window.pageYOffset; // Duration of the scroll animation in milliseconds const duration = 800; // Improved easing function for smoother animation const easeOutCubic = t => 1 - Math.pow(1 - t, 3); let startTime = null; const animation = currentTime => { if (startTime === null) startTime = currentTime; const timeElapsed = currentTime - startTime; const progress = Math.min(timeElapsed / duration, 1); const easedProgress = easeOutCubic(progress); window.scrollTo(0, startPosition * (1 - easedProgress)); if (timeElapsed < duration) { requestAnimationFrame(animation); } }; requestAnimationFrame(animation); }; useEffect(() => { window.addEventListener('scroll', toggleVisibility); return () => { window.removeEventListener('scroll', toggleVisibility); }; }, []); return ( {isVisible && ( )} ); }; export default ScrollToTopButton;