-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMobileMenu.tsx
132 lines (124 loc) · 3.67 KB
/
MobileMenu.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import Link from 'next/link'
import React, { useEffect, useState } from 'react'
import useDelayedRender from 'use-delayed-render'
import cn from 'classnames'
function MobileMenu() {
const [isMenuOpen, setIsMenuOpen] = useState(false)
const { mounted: isMenuMounted, rendered: isMenuRendered } = useDelayedRender(
isMenuOpen,
{
enterDelay: 20,
exitDelay: 300,
}
)
const toggleMenu = () => {
if (isMenuOpen) {
setIsMenuOpen(false)
document.body.style.overflow = ''
} else {
setIsMenuOpen(true)
document.body.style.overflow = 'hidden'
}
}
useEffect(() => {
return function cleanup() {
document.body.style.overflow = ''
}
}, [])
return (
<>
<button
className={cn('burger', 'visible md:hidden')}
aria-label="Toggle menu"
type="button"
onClick={toggleMenu}
>
<MenuIcon data-hide={isMenuOpen} />
<CrossIcon data-hide={!isMenuOpen} />
</button>
{isMenuMounted && (
<ul
className={cn(
'menu',
'absolute flex flex-col bg-gray-100 dark:bg-gray-900',
isMenuRendered && 'menuRendered'
)}
>
<li
className="border-b border-gray-300 text-sm font-semibold text-gray-900 dark:border-gray-700 dark:text-gray-100"
style={{ transitionDelay: '150ms' }}
>
<Link href="/">
<a className="flex w-auto pb-4">Home</a>
</Link>
</li>
<li
className="border-b border-gray-300 text-sm font-semibold text-gray-900 dark:border-gray-700 dark:text-gray-100"
style={{ transitionDelay: '250ms' }}
>
<Link href="/blog">
<a className="flex w-auto pb-4">Blog</a>
</Link>
</li>
<li
className="border-b border-gray-300 text-sm font-semibold text-gray-900 dark:border-gray-700 dark:text-gray-100"
style={{ transitionDelay: '275ms' }}
>
<Link href="/snippets">
<a className="flex w-auto pb-4">Snippets</a>
</Link>
</li>
<li
className="border-b border-gray-300 text-sm font-semibold text-gray-900 dark:border-gray-700 dark:text-gray-100"
style={{ transitionDelay: '300ms' }}
>
<Link href="/newsletter">
<a className="flex w-auto pb-4">Newsletter</a>
</Link>
</li>
<li
className="border-b border-gray-300 text-sm font-semibold text-gray-900 dark:border-gray-700 dark:text-gray-100"
style={{ transitionDelay: '325ms' }}
>
<Link href="/tweets">
<a className="flex w-auto pb-4">Tweets</a>
</Link>
</li>
</ul>
)}
</>
)
}
const MenuIcon = (props: JSX.IntrinsicElements['svg']) => (
<svg
xmlns="http://www.w3.org/2000/svg"
className="absolute h-6 w-6 text-gray-800 dark:text-gray-200"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth={2}
shapeRendering="geometricPrecision"
{...props}
>
<path strokeLinecap="round" strokeLinejoin="round" d="M4 8h16M4 16h16" />
</svg>
)
const CrossIcon = (props: JSX.IntrinsicElements['svg']) => (
<svg
xmlns="http://www.w3.org/2000/svg"
className="absolute h-6 w-6 text-gray-800 dark:text-gray-200"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth={2}
shapeRendering="geometricPrecision"
{...props}
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M6 18L18 6M6 6l12 12"
/>
</svg>
)
export default MobileMenu