diff --git a/components/OrganizedContent.tsx b/components/OrganizedContent.tsx index 1d7ff46e..392d15cc 100644 --- a/components/OrganizedContent.tsx +++ b/components/OrganizedContent.tsx @@ -2,7 +2,7 @@ import React, { ReactNode, ComponentType, useState, - //useRef, + useRef, useEffect, } from "react"; import styles from "./OrganizedContent.module.css"; @@ -105,7 +105,7 @@ export const OrganizedContent = ({
- +
); @@ -218,6 +218,17 @@ const Burger = ({ open, setOpen }: MobileProps) => { ); }; +function MenuWrapper(menuProps: MenuProps) { + const wrapperRef = useRef(null); + useOutsideAlerter(wrapperRef, menuProps.setOpen); + + return ( +
+ +
+ ); +} + const Menu = ({ open, childProps }: MenuProps) => { const mobileNav = open ? styles.mobileNav @@ -228,3 +239,26 @@ const Menu = ({ open, childProps }: MenuProps) => { ); }; + +function useOutsideAlerter( + ref: React.RefObject, + setOpen: React.Dispatch> +) { + useEffect(() => { + /** + * Alert if clicked on outside of element + */ + const handleClickOutside = (event: Event) => { + if (ref.current && !ref.current.contains(event.target as Node)) { + setOpen(false); + } + }; + + // Bind the event listener + document.addEventListener("mousedown", handleClickOutside); + return () => { + // Unbind the event listener on clean up + document.removeEventListener("mousedown", handleClickOutside); + }; + }, [ref, setOpen]); +}