11import { sendGAEvent } from "@next/third-parties/google"
2- import { Fragment , useEffect , useState } from "react"
2+ import { Fragment , useEffect , useLayoutEffect , useState } from "react"
33
44import { Box , Collapse , List , Stack } from "@mui/material"
55import { styled } from "@mui/system"
@@ -39,14 +39,22 @@ const MobileHeader = ({ currentMenu }) => {
3939 setActiveCollapse ( currentMenu [ 1 ] )
4040 } , [ currentMenu ] )
4141
42- const toggleDrawer = isOpen => {
43- setOpen ( isOpen )
44- if ( isOpen ) {
42+ useLayoutEffect ( ( ) => {
43+ if ( open ) {
4544 window . document . body . classList . add ( "mobile-top-nav-open" )
4645 } else {
4746 window . document . body . classList . remove ( "mobile-top-nav-open" )
48- setActiveCollapse ( currentMenu [ 1 ] )
4947 }
48+ } , [ open ] )
49+
50+ const handleCloseMobileHeader = ( ) => {
51+ if ( open ) {
52+ setOpen ( false )
53+ }
54+ }
55+
56+ const handleToggleMobileHeader = ( ) => {
57+ setOpen ( ! open )
5058 }
5159
5260 const toggleCollapse = collapse => {
@@ -58,7 +66,6 @@ const MobileHeader = ({ currentMenu }) => {
5866 label,
5967 device : "mobile" ,
6068 } )
61- toggleDrawer ( false )
6269 }
6370
6471 const renderList = ( ) => (
@@ -133,7 +140,7 @@ const MobileHeader = ({ currentMenu }) => {
133140 >
134141 < Stack sx = { { height : "6.4rem" , px : "2rem" , lineHeight : "3rem" } } direction = "row" justifyContent = "space-between" alignItems = "center" >
135142 < Link href = "/" className = "flex" >
136- < Box onClick = { ( ) => toggleDrawer ( false ) } >
143+ < Box onClick = { handleCloseMobileHeader } >
137144 < Logo light = { dark } />
138145 </ Box >
139146 </ Link >
@@ -154,7 +161,7 @@ const MobileHeader = ({ currentMenu }) => {
154161 transform : "rotate(-45deg) translate(5px, -5px)" ,
155162 } ,
156163 } }
157- onClick = { ( ) => toggleDrawer ( ! open ) }
164+ onClick = { handleToggleMobileHeader }
158165 className = { open ? "active" : "" }
159166 >
160167 < Bar dark = { dark } > </ Bar >
0 commit comments