From c0b7873edcc5f1062691f5cb358dc7c141d176d1 Mon Sep 17 00:00:00 2001 From: drippypop <146481814+drippypop@users.noreply.github.com> Date: Fri, 4 Oct 2024 10:47:35 -0400 Subject: [PATCH] Fix nav bar scrolling issue on mobile devices (#672) * fixed mobile nav issue, now need to make it nonscrollable or disable features * Fix Mobile Nav: When scrolling the nav is closed * Fix Coderabbit Issue --------- Co-authored-by: Saptarshi Sarkar --- Website/app/Header.js | 64 +++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/Website/app/Header.js b/Website/app/Header.js index f85b6234b..5ee4602e0 100644 --- a/Website/app/Header.js +++ b/Website/app/Header.js @@ -20,13 +20,13 @@ function NavLink({ to, children, cn }) { function MobileNav({ open }) { return (
-
- {/*logo container*/} - +
+ {/* Logo container */} +
+ {/* Mobile Nav */} -
+
- + - + - + -
+ + {/* Social Icons */} +
@@ -67,7 +70,7 @@ function MobileNav({ open }) { @@ -77,22 +80,31 @@ function MobileNav({ open }) { ); } + + + export default function Header({ props }) { const [open, setOpen] = useState(false); const [hcolor, setHcolor] = useState(props + " pt-7"); - const onScroll = useCallback(() => { - const { scrollY } = window; - if (window.innerWidth > 760) { - if (scrollY === 0) setHcolor(props + " pt-7"); - else setHcolor("bg-var shadow-lg pt-4"); - } - }, [props]); +const onScroll = useCallback(() => { + const { scrollY } = window; + if (window.innerWidth <= 760 && open) { + setOpen(false); + } + if (window.innerWidth > 760) { + if (scrollY === 0) setHcolor(props + " pt-7"); + else setHcolor("bg-var shadow-lg pt-4"); + } +}, [props, open]); - useEffect(() => { - //add event listener to window - window.addEventListener("scroll", onScroll, { passive: true }); - // remove event on unmounting to prevent Linkmemory leak with the cleanup - }, [onScroll]); +useEffect(() => { + // add event listener to window + window.addEventListener("scroll", onScroll, { passive: true }); + // remove event listener on unmount to prevent memory leaks + return () => { + window.removeEventListener("scroll", onScroll); + }; +}, [onScroll]); return (