diff --git a/src/components/Header/HeaderMobileMenu.tsx b/src/components/Header/HeaderMobileMenu.tsx index 563717c56..c98644135 100644 --- a/src/components/Header/HeaderMobileMenu.tsx +++ b/src/components/Header/HeaderMobileMenu.tsx @@ -1,21 +1,29 @@ 'use client'; +import { usePathname } from 'next/navigation'; import { useEffect, useState } from 'react'; import { IconMenu } from '@/components/icons/IconMenu'; import { useLanguage, tString } from '@/intl/client'; import { tcls } from '@/lib/tailwind'; +const globalClassName = 'navigation-open'; + +/** + * Button to show/hide the table of content on mobile. + */ export function HeaderMobileMenu(props: Partial>) { const language = useLanguage(); const scrollDistance = 320; + + const pathname = usePathname(); const [hasScrolled, setHasScrolled] = useState(false); const toggleNavigation = () => { - if (!hasScrolled && document.body.classList.contains('navigation-open')) { - document.body.classList.remove('navigation-open'); + if (!hasScrolled && document.body.classList.contains(globalClassName)) { + document.body.classList.remove(globalClassName); } else { - document.body.classList.add('navigation-open'); + document.body.classList.add(globalClassName); window.scrollTo(0, 0); } }; @@ -28,6 +36,11 @@ export function HeaderMobileMenu(props: Partial { + document.body.classList.remove(globalClassName); + }, [pathname]); + useEffect(() => { window.addEventListener('scroll', handleScroll); return () => {