From 3af812456dd4dbb2af47b6a5870912c0d425f676 Mon Sep 17 00:00:00 2001 From: TuvalSimha Date: Thu, 13 Apr 2023 12:14:41 +0300 Subject: [PATCH 1/2] Back to top button Signed-off-by: TuvalSimha --- src/components/BackToTop/index.tsx | 54 ++++++++++++++++++++++++++++++ src/components/Layout/index.tsx | 2 ++ 2 files changed, 56 insertions(+) create mode 100644 src/components/BackToTop/index.tsx diff --git a/src/components/BackToTop/index.tsx b/src/components/BackToTop/index.tsx new file mode 100644 index 0000000000..dd97862035 --- /dev/null +++ b/src/components/BackToTop/index.tsx @@ -0,0 +1,54 @@ +import React, { useEffect, useState } from "react" + +interface Props { + scrollThreshold: number; +} + +const BackToTop: React.FC = ({ scrollThreshold }) => { + const [isVisible, setIsVisible] = useState(false); + + useEffect(() => { + const handleScroll = () => { + const pageHeight = document.body.scrollHeight; + const currentPosition = window.pageYOffset; + console.log(pageHeight, currentPosition); + if (pageHeight < 6000) { + setIsVisible(false); + } else { + if (currentPosition > scrollThreshold) { + setIsVisible(true); + } else { + setIsVisible(false); + } + } + }; + window.addEventListener('scroll', handleScroll); + return () => { + window.removeEventListener('scroll', handleScroll); + }; + }, [scrollThreshold]); + + const handleBackToTopClick = () => { + window.scrollTo({ + top: 0, + behavior: 'smooth', + }); + }; + + return ( + <> + {isVisible && ( + + )} + + ); +}; + + + +export default BackToTop diff --git a/src/components/Layout/index.tsx b/src/components/Layout/index.tsx index 255ec88d9c..0fde10e43e 100644 --- a/src/components/Layout/index.tsx +++ b/src/components/Layout/index.tsx @@ -4,6 +4,7 @@ import Header from "../Header" import "../../assets/css/style.less" import "../../assets/css/global.css" +import BackToTop from "../BackToTop" interface Props { children: React.ReactNode className?: string @@ -20,6 +21,7 @@ const IndexLayout = ({ {children}