Files
public-blog/components/ui/ScrollToTop.tsx

29 lines
928 B
TypeScript

"use client";
import { useEffect, useState } from "react";
import { motion } from "motion/react";
export function ScrollToTop() {
const [visible, setVisible] = useState(false);
useEffect(() => {
const toggle = () => setVisible(window.scrollY > 400);
toggle();
window.addEventListener("scroll", toggle, { passive: true });
return () => window.removeEventListener("scroll", toggle);
}, []);
return (
<motion.button
type="button"
onClick={() => window.scrollTo({ top: 0, behavior: "smooth" })}
whileHover={{ scale: 1.1 }}
whileTap={{ scale: 0.95 }}
className="scroll-to-top"
aria-label="Scroll to top"
>
<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><polyline points="18 15 12 9 6 15"/></svg>
</motion.button>
);
}