reset scroll position on path change

This commit is contained in:
elise
2024-08-11 10:51:46 +02:00
parent 22507c2d09
commit 873d6ccc7c

View File

@ -20,6 +20,7 @@ export const Header = () => {
setShowMenu(!showMenu); setShowMenu(!showMenu);
} }
// remove ability to scroll when opening menu + keep previous scroll position when closing menu
useEffect(() => { useEffect(() => {
if (showMenu) { if (showMenu) {
const scrollY = window.scrollY; const scrollY = window.scrollY;
@ -33,9 +34,16 @@ export const Header = () => {
} }
}, [showMenu]); }, [showMenu]);
// reset scroll position on path change
useEffect(() => {
if (!showMenu) {
window.scrollTo(0, 0);
}
}, [location.pathname, showMenu]);
// hide menu and reset active menu item on path change
const pathname = usePathname(); const pathname = usePathname();
useEffect(() => { useEffect(() => {
// hide menu on path change
setShowMenu(false); setShowMenu(false);
setActiveMenuItem(undefined); setActiveMenuItem(undefined);
}, [pathname]); }, [pathname]);