-
Notifications
You must be signed in to change notification settings - Fork 0
/
MobileHeader.jsx
229 lines (211 loc) · 6.25 KB
/
MobileHeader.jsx
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
'use client';
/* eslint-disable @next/next/no-img-element */
import { scrollToId } from './utils';
import { useEffect, useState } from 'react';
import Image from 'next/image';
const DropdownIcon = () => (
<svg
className="dropdownIcon"
xmlns="http://www.w3.org/2000/svg"
height="18"
viewBox="0 0 18 18"
width="18"
>
<rect id="Canvas" fill="#ff13dc" opacity="0" width="18" height="18" />
<path
className="fill"
d="M4,7.01a1,1,0,0,1,1.7055-.7055l3.289,3.286,3.289-3.286a1,1,0,0,1,1.437,1.3865l-.0245.0245L9.7,11.7075a1,1,0,0,1-1.4125,0L4.293,7.716A.9945.9945,0,0,1,4,7.01Z"
/>
</svg>
);
const MenuButtonIcon = () => (
<svg
xmlns="http://www.w3.org/2000/svg"
height="18"
viewBox="0 0 18 18"
width="18"
>
<rect id="Canvas" fill="#ff13dc" opacity="0" width="18" height="18" />
<path
className="fill"
d="M13.2425,3.343,9,7.586,4.7575,3.343a.5.5,0,0,0-.707,0L3.343,4.05a.5.5,0,0,0,0,.707L7.586,9,3.343,13.2425a.5.5,0,0,0,0,.707l.707.7075a.5.5,0,0,0,.707,0L9,10.414l4.2425,4.243a.5.5,0,0,0,.707,0l.7075-.707a.5.5,0,0,0,0-.707L10.414,9l4.243-4.2425a.5.5,0,0,0,0-.707L13.95,3.343a.5.5,0,0,0-.70711-.00039Z"
/>
</svg>
);
export default function MobileHeader({
maxWidth,
isAuthorVersion,
host,
mobileNavObj,
debugAnim,
}) {
const [openMenu, setOpenMenu] = useState(false);
const [openNav, setOpenNav] = useState(false);
const [navLabel, setNavLabel] = useState(
mobileNavObj?.menuItems[0].text || 'Navigation',
);
const navItems = mobileNavObj?.menuItems || [];
// runs passed function if timeout has gone by without being called again.
function debounce(func, wait) {
let timeout;
return () => {
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(func, wait);
};
}
function findCurrentElement() {
// searches each nav item for closest.
let newLabel = null;
for (let i = 0; i < navItems.length; i++) {
const element = document.getElementById(navItems[i].link.substring(1));
if (!element) {
continue;
}
const rect = element.getBoundingClientRect();
// if top of panel is within top half of screen
if (rect.top < window.innerHeight / 2) {
newLabel = navItems[i].text;
}
}
// after finding the closest label set that as nav button label.
newLabel && setNavLabel(newLabel);
}
// run without debounce if debugAnim is not set
const handleScroll = debounce(
() => findCurrentElement(),
debugAnim !== 'instant' ? 100 : 0,
);
useEffect(() => {
window.addEventListener('scroll', handleScroll);
return () => window.removeEventListener('scroll', handleScroll);
}, [handleScroll]);
const onClickHandler = (link) => {
if (link) {
scrollToId(link);
}
// add hash to url without refreshing page
window.history.replaceState(window.location.href.split('#')[0], null, link);
window.postMessage(
{ type: 'hashUpdate', hash: link },
window.location.origin,
);
setOpenNav(false);
};
return (
<header
className="mobileHeaderWrapper"
// style={{ maxWidth }}
>
<div className="mainHeader">
<button
className={`menuButton ${openMenu ? 'menuOpen' : 'menuClosed'}`}
id={'mobile-menu-button'}
onClick={() => setOpenMenu(true)}
>
<svg
xmlns="http://www.w3.org/2000/svg"
height="18"
viewBox="0 0 18 18"
width="18"
>
<defs></defs>{' '}
<rect
id="Canvas"
fill="#ff13dc"
opacity="0"
width="18"
height="18"
/>
<rect className="fill" height="2" rx="0.5" width="14" x="2" y="8" />
<rect className="fill" height="2" rx="0.5" width="14" x="2" y="3" />
<rect
className="fill"
height="2"
rx="0.5"
width="14"
x="2"
y="13"
/>
</svg>
</button>
{/* eslint-expect-error-next-line @next/next/no-img-element */}
<Image
src={'/wknd-logo-dk.svg'}
alt="logo"
height={22}
width={60}
className={`logo ${openMenu ? 'menuOpen' : 'menuClosed'}`}
/>
<a className="profileIconWrapper">
<Image
className="menuProfileIcon"
src={'/stacey-roswells.webp'}
width={42}
height={42}
alt="profile picture"
/>
</a>
</div>
<nav className="headerNavigation">
<button
className={`navigationButton ${openNav ? 'navOpen' : 'navClosed'}`}
id={'mobile-nav-button'}
onClick={() => setOpenNav(!openNav)}
>
<span>{navLabel}</span>
<DropdownIcon />
</button>
<menu className={`navigationMenu ${openNav ? 'open' : 'closed'}`}>
<ul>
{navItems.map((item, index) => {
return (
<a onClick={() => onClickHandler(item.link)} key={index}>
<li> {item.text} </li>
</a>
);
})}
</ul>
</menu>
</nav>
<menu
className={`headerMenu ${openMenu ? 'open' : 'closed'}`}
style={{ maxWidth: maxWidth ? maxWidth * 0.6 : null }}
>
<div className="top">
<button
className="closeButton menuButton"
onClick={() => setOpenMenu(false)}
>
<MenuButtonIcon />
</button>
</div>
<div className="list">
<ul>
<li>adventures</li>
<li>magazine</li>
<li>settings</li>
</ul>
</div>
<div className="bottom">
<a
href={isAuthorVersion ? '' : host}
target="_blank"
rel="noopener noreferrer"
>
<span>{isAuthorVersion ? 'my account' : 'login'}</span>
</a>
{isAuthorVersion && (
<Image
src={'/stacey-roswells.webp'}
width={40}
height={40}
alt="profile picture"
/>
)}
</div>
</menu>
</header>
);
}