Hotfix/hamburger close (#47)

* hamburger link click closes menu

* remove event listeners on unmount
This commit is contained in:
Owen Rees
2023-07-04 14:03:17 +02:00
committed by GitHub
parent 75a6c74306
commit 34f0c23526
3 changed files with 12 additions and 5 deletions
+12 -2
View File
@@ -1,5 +1,3 @@
// TODO is this really a hook? I think it is more of a util function
interface HamburgerClose {
menuVisible: boolean;
setMenuVisible: Dispatch<SetStateAction<boolean>>;
@@ -36,10 +34,22 @@ const useHamburgerClose = ({
setMouseOverMenu(false);
}
};
const handleLinkClick = () => {
setMenuVisible(false);
};
document.addEventListener("mousedown", handleMouseDownOutsideMenu);
const links = document.querySelectorAll("a");
links.forEach((link) => {
link.addEventListener("click", handleLinkClick);
});
return () => {
document.removeEventListener("mousedown", handleMouseDownOutsideMenu);
links.forEach((link) => {
link.removeEventListener("click", handleLinkClick);
});
};
}, [menuVisible]);