11'use client'
22
33import { Dialog , Transition } from '@headlessui/react'
4- import { Fragment , useState } from 'react'
4+ import { disableBodyScroll , enableBodyScroll , clearAllBodyScrollLocks } from 'body-scroll-lock'
5+ import { Fragment , useState , useEffect , useRef } from 'react'
56import Link from './Link'
67import headerNavLinks from '@/data/headerNavLinks'
78
89const MobileNav = ( ) => {
910 const [ navShow , setNavShow ] = useState ( false )
11+ const navRef = useRef ( null )
1012
1113 const onToggleNav = ( ) => {
1214 setNavShow ( ( status ) => {
1315 if ( status ) {
14- document . body . style . overflow = 'auto'
16+ enableBodyScroll ( navRef . current )
1517 } else {
1618 // Prevent scrolling
17- document . body . style . overflow = 'hidden'
19+ disableBodyScroll ( navRef . current )
1820 }
1921 return ! status
2022 } )
2123 }
2224
25+ useEffect ( ( ) => {
26+ return clearAllBodyScrollLocks
27+ } )
28+
2329 return (
2430 < >
2531 < button aria-label = "Toggle Menu" onClick = { onToggleNav } className = "sm:hidden" >
@@ -36,8 +42,8 @@ const MobileNav = () => {
3642 />
3743 </ svg >
3844 </ button >
39- < Transition appear show = { navShow } as = { Fragment } >
40- < Dialog as = "div" className = "relative z-10" onClose = { onToggleNav } >
45+ < Transition appear show = { navShow } as = { Fragment } unmount = { false } >
46+ < Dialog as = "div" onClose = { onToggleNav } unmount = { false } >
4147 < Transition . Child
4248 as = { Fragment }
4349 enter = "ease-out duration-300"
@@ -46,60 +52,53 @@ const MobileNav = () => {
4652 leave = "ease-in duration-200"
4753 leaveFrom = "opacity-100"
4854 leaveTo = "opacity-0"
55+ unmount = { false }
4956 >
50- < div className = "fixed inset-0 bg-black/25" />
57+ < div className = "fixed inset-0 z-60 bg-black/25" />
5158 </ Transition . Child >
5259
53- < div className = "fixed inset-0 overflow-y-auto" >
54- < div className = "flex min-h-full items-center justify-center p-4 text-center" >
55- < Transition . Child
56- as = { Fragment }
57- enter = "transition ease-in-out duration-300 transform"
58- enterFrom = "translate-x-full opacity-0"
59- enterTo = "translate-x-0 opacity-95"
60- leave = "transition ease-in duration-200 transform"
61- leaveFrom = "translate-x-0 opacity-95"
62- leaveTo = "translate-x-full opacity-0"
60+ < Transition . Child
61+ as = { Fragment }
62+ enter = "transition ease-in-out duration-300 transform"
63+ enterFrom = "translate-x-full opacity-0"
64+ enterTo = "translate-x-0 opacity-95"
65+ leave = "transition ease-in duration-200 transform"
66+ leaveFrom = "translate-x-0 opacity-95"
67+ leaveTo = "translate-x-full opacity-0"
68+ unmount = { false }
69+ >
70+ < Dialog . Panel className = "fixed left-0 top-0 z-70 h-full w-full bg-white opacity-95 duration-300 dark:bg-gray-950 dark:opacity-[0.98]" >
71+ < nav
72+ ref = { navRef }
73+ className = "mt-8 flex h-full basis-0 flex-col items-start overflow-y-auto pl-12 pt-2 text-left"
6374 >
64- < Dialog . Panel className = "fixed left-0 top-0 z-10 h-full w-full bg-white opacity-95 duration-300 dark:bg-gray-950 dark:opacity-[0.98]" >
65- < nav className = "fixed mt-8 h-full text-left" >
66- { headerNavLinks . map ( ( link ) => (
67- < div key = { link . title } className = "px-12 py-4" >
68- < Link
69- href = { link . href }
70- className = "text-2xl font-bold tracking-widest text-gray-900 hover:text-primary-500 dark:text-gray-100 dark:hover:text-primary-400"
71- onClick = { onToggleNav }
72- >
73- { link . title }
74- </ Link >
75- </ div >
76- ) ) }
77- </ nav >
75+ { headerNavLinks . map ( ( link ) => (
76+ < Link
77+ key = { link . title }
78+ href = { link . href }
79+ className = "mb-4 py-2 pr-4 text-2xl font-bold tracking-widest text-gray-900 outline outline-0 hover:text-primary-500 dark:text-gray-100 dark:hover:text-primary-400"
80+ onClick = { onToggleNav }
81+ >
82+ { link . title }
83+ </ Link >
84+ ) ) }
85+ </ nav >
7886
79- < div className = "flex justify-end" >
80- < button
81- className = "mr-8 mt-11 h-8 w-8"
82- aria-label = "Toggle Menu"
83- onClick = { onToggleNav }
84- >
85- < svg
86- xmlns = "http://www.w3.org/2000/svg"
87- viewBox = "0 0 20 20"
88- fill = "currentColor"
89- className = "text-gray-900 hover:text-primary-500 dark:text-gray-100 dark:hover:text-primary-400"
90- >
91- < path
92- fillRule = "evenodd"
93- d = "M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z"
94- clipRule = "evenodd"
95- />
96- </ svg >
97- </ button >
98- </ div >
99- </ Dialog . Panel >
100- </ Transition . Child >
101- </ div >
102- </ div >
87+ < button
88+ className = "fixed right-4 top-7 z-80 h-16 w-16 p-4 text-gray-900 hover:text-primary-500 dark:text-gray-100 dark:hover:text-primary-400"
89+ aria-label = "Toggle Menu"
90+ onClick = { onToggleNav }
91+ >
92+ < svg xmlns = "http://www.w3.org/2000/svg" viewBox = "0 0 20 20" fill = "currentColor" >
93+ < path
94+ fillRule = "evenodd"
95+ d = "M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z"
96+ clipRule = "evenodd"
97+ />
98+ </ svg >
99+ </ button >
100+ </ Dialog . Panel >
101+ </ Transition . Child >
103102 </ Dialog >
104103 </ Transition >
105104 </ >
0 commit comments