-
-
Notifications
You must be signed in to change notification settings - Fork 175
/
Copy pathHeader.tsx
86 lines (76 loc) · 2.31 KB
/
Header.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
import Link from 'next/link'
import headerLogo from '../assets/img/logo.png'
import React from 'react'
import NavLinks from './NavLinks'
import classnames from 'classnames'
import Icon from './Icon'
type UseMobileCallback = (ev: MediaQueryListEvent) => any
const useMobile = (cb: UseMobileCallback) => {
React.useEffect(() => {
const mobileMql = window.matchMedia('(max-width: 480px)')
mobileMql.addEventListener?.('change', cb)
// Safari < 14
if (mobileMql.addListener) {
mobileMql.addListener(cb)
}
return () => {
mobileMql.removeEventListener?.('change', cb)
if (mobileMql.removeListener) {
mobileMql.removeListener(cb)
}
}
}, [])
}
const HeaderHome = () => {
const [displayMobileMenu, setDisplayMobileMenu] = React.useState(false)
useMobile((e) => {
if (!e.matches) {
setDisplayMobileMenu(false)
}
})
return (
<>
<header>
<div
className={classnames(
'bg-white bg-opacity-0 duration-200 transition-colors sticky z-10'
)}
>
<div className="flex items-center justify-between max-w-6xl px-5 py-4 mx-auto md:px-2">
<Link href="/">
<a>
<img
className="h-12 md:h-24"
src={headerLogo}
alt="Skolplattformen"
/>
</a>
</Link>
<nav className="hidden md:block">
<NavLinks />
</nav>
</div>
</div>
</header>
<button
aria-label={displayMobileMenu ? 'Stäng meny' : 'Visa meny'}
className="fixed z-30 block w-16 p-5 text-white bg-gray-900 dark:bg-gray-700 rounded-full md:hidden bottom-5 right-5"
onClick={() => setDisplayMobileMenu(!displayMobileMenu)}
>
{displayMobileMenu ? <Icon.Times /> : <Icon.Menu />}
</button>
{displayMobileMenu && (
<>
<div
className="fixed inset-0 z-10 bg-gray-800 bg-opacity-50"
onClick={() => setDisplayMobileMenu(false)}
/>
<div className="fixed top-0 bottom-0 left-0 z-20 w-9/12 h-screen p-5 bg-white dark:bg-gray-800">
<NavLinks onClick={() => setDisplayMobileMenu(false)} />
</div>
</>
)}
</>
)
}
export default HeaderHome