"use client"; import { useEffect, useState } from "react"; import { ChevronUp } from "lucide-react"; import { Button } from "@/components/ui/button"; import { cn } from "@/lib/utils"; const SCROLL_THRESHOLD = 0.4; // show after 40% scroll export function ScrollToTop({ className }: { className?: string }) { const [visible, setVisible] = useState(false); useEffect(() => { function onScroll() { const maxScroll = document.documentElement.scrollHeight - window.innerHeight; if (maxScroll <= 0) { setVisible(false); return; } const ratio = window.scrollY / maxScroll; setVisible(ratio >= SCROLL_THRESHOLD); } onScroll(); window.addEventListener("scroll", onScroll, { passive: true }); return () => window.removeEventListener("scroll", onScroll); }, []); const scrollToTop = () => { window.scrollTo({ top: 0, behavior: "smooth" }); }; if (!visible) return null; return (
); }