diff options
| author | Armand Philippot <git@armandphilippot.com> | 2022-05-17 18:31:21 +0200 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2022-05-17 18:31:21 +0200 |
| commit | 9a186b357b32325cf77fdce39a6c6c9aff76d8a5 (patch) | |
| tree | bfeb790f3cc82f753ac74c9b0130f43189502803 /src/components/templates/layout/layout.tsx | |
| parent | c77c58e18143233be042c4980a6ed08ae9beac52 (diff) | |
chore: listen scroll to hide/show back to top button
Diffstat (limited to 'src/components/templates/layout/layout.tsx')
| -rw-r--r-- | src/components/templates/layout/layout.tsx | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/src/components/templates/layout/layout.tsx b/src/components/templates/layout/layout.tsx index 9e9282b..3eb02ee 100644 --- a/src/components/templates/layout/layout.tsx +++ b/src/components/templates/layout/layout.tsx @@ -7,11 +7,12 @@ import Home from '@components/atoms/icons/home'; import PostsStack from '@components/atoms/icons/posts-stack'; import Main from '@components/atoms/layout/main'; import NoScript from '@components/atoms/layout/no-script'; -import Footer, { FooterProps } from '@components/organisms/layout/footer'; +import Footer, { type FooterProps } from '@components/organisms/layout/footer'; import Header, { type HeaderProps } from '@components/organisms/layout/header'; +import useScrollPosition from '@utils/hooks/use-scroll-position'; import useSettings from '@utils/hooks/use-settings'; import Script from 'next/script'; -import { FC, ReactNode } from 'react'; +import { FC, ReactNode, useState } from 'react'; import { useIntl } from 'react-intl'; import { BreadcrumbList, @@ -162,6 +163,19 @@ const Layout: FC<LayoutProps> = ({ subjectOf: { '@id': `${url}` }, }; + const [backToTopClassName, setBackToTopClassName] = useState<string>( + styles['back-to-top--hidden'] + ); + const updateBackToTopClassName = () => { + setBackToTopClassName( + window.scrollY > 300 + ? styles['back-to-top--visible'] + : styles['back-to-top--hidden'] + ); + }; + + useScrollPosition(updateBackToTopClassName); + return ( <> <Script @@ -203,6 +217,7 @@ const Layout: FC<LayoutProps> = ({ copyright={copyrightData} navItems={footerNav} topId="top" + backToTopClassName={backToTopClassName} className={styles.footer} /> <noscript> |
