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);
}
// remove ability to scroll when opening menu + keep previous scroll position when closing menu
useEffect(() => {
if (showMenu) {
const scrollY = window.scrollY;
@ -33,9 +34,16 @@ export const Header = () => {
}
}, [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();
useEffect(() => {
// hide menu on path change
setShowMenu(false);
setActiveMenuItem(undefined);
}, [pathname]);