From 7ad7c33fe5c5b166e5036eabf0e684b0785a2aa9 Mon Sep 17 00:00:00 2001 From: Phoenix Pereira <47909638+phoenixpereira@users.noreply.github.com> Date: Mon, 12 Feb 2024 12:29:38 +1030 Subject: [PATCH] fix(header): Mobile menu closes as expected (#48) * fix: add window resize check for header * fix: mobile menu closes when link pressed * refactor: replace useEffect with useMount * refactor: add breakpoint constants for media queries --- src/components/Header.tsx | 34 ++++++++++++++++++++++++---------- src/constants/breakpoints.ts | 6 ++++++ 2 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 src/constants/breakpoints.ts diff --git a/src/components/Header.tsx b/src/components/Header.tsx index a0e81212..9af155dc 100644 --- a/src/components/Header.tsx +++ b/src/components/Header.tsx @@ -1,10 +1,12 @@ 'use client'; import FancyRectangle from '@/components/FancyRectangle'; +import { BREAKPOINTS } from '@/constants/breakpoints'; +import { useMount } from '@/hooks/use-mount'; import { useUser } from '@clerk/nextjs'; import Image from 'next/image'; import Link from 'next/link'; -import { useEffect, useRef, useState } from 'react'; +import { useRef, useState } from 'react'; import { IoMdClose, IoMdMenu } from 'react-icons/io'; import Button from './Button'; import UserButton from './UserButton'; @@ -19,13 +21,25 @@ export default function Header() { setIsMenuOpen(!isMenuOpen); }; - useEffect(() => { + useMount(() => { window.addEventListener('scroll', handleScroll); + window.addEventListener('resize', handleResize); return () => { window.removeEventListener('scroll', handleScroll); + window.removeEventListener('resize', handleResize); }; - }, []); + }); + + const closeMenu = () => { + setIsMenuOpen(false); + }; + + const handleResize = () => { + if (window.innerWidth >= BREAKPOINTS.md) { + setIsMenuOpen(false); + } + }; const handleScroll = () => { const scrollPosition = window.scrollY; @@ -108,16 +122,16 @@ export default function Header() { isMenuOpen ? 'mt-12 space-y-8' : 'hidden' } mb-4 text-4xl md:mb-0 md:flex md:flex-row md:space-x-4 md:space-y-0 md:text-base lg:space-x-8`} > - + About - + Events - + Sponsors - + Contact