diff --git a/apps/infoalloggi/src/components/scroll_top.tsx b/apps/infoalloggi/src/components/scroll_top.tsx new file mode 100644 index 0000000..41ed3f1 --- /dev/null +++ b/apps/infoalloggi/src/components/scroll_top.tsx @@ -0,0 +1,69 @@ +"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 + + + + )} + + ); +} diff --git a/apps/infoalloggi/src/pages/annunci.tsx b/apps/infoalloggi/src/pages/annunci.tsx index f5458c6..57ab764 100644 --- a/apps/infoalloggi/src/pages/annunci.tsx +++ b/apps/infoalloggi/src/pages/annunci.tsx @@ -18,6 +18,7 @@ import { CodiceBox } from "~/components/codiceRicerca"; import { NumberInput } from "~/components/custom_ui/InputNumber"; import { Layout } from "~/components/Layout"; import { LoadingPage } from "~/components/loading"; +import ScrollToTop from "~/components/scroll_top"; import { Status500 } from "~/components/status-page"; import { Badge } from "~/components/ui/badge"; import { Button } from "~/components/ui/button"; @@ -75,7 +76,7 @@ const Annunci = ({ options }: AnnunciPageProps) => { {t.annunci.titolo} - +