Repository navigation
Expand file tree
/
Copy pathHeader.tsx
More file actions
344 lines (325 loc) · 9.96 KB
/
Copy pathHeader.tsx
File metadata and controls
344 lines (325 loc) · 9.96 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/** @jsxRuntime classic */
/** @jsx jsx */
import {
createContext,
useContext,
useCallback,
useEffect,
useState,
useRef,
ReactNode,
} from 'react';
import { useRouter } from 'next/router';
import { jsx } from '@emotion/react';
import Link from 'next/link';
import debounce from 'lodash.debounce';
import { BREAK_POINTS } from '../lib/media';
import { useMediaQuery } from '../lib/media';
import { SearchField } from './primitives/SearchField';
import { Highlight } from './primitives/Highlight';
import { Wrapper } from './primitives/Wrapper';
import { Hamburger } from './icons/Hamburger';
import { Button } from './primitives/Button';
import { NavItem } from './docs/Navigation';
import { DarkModeBtn } from './DarkModeBtn';
import { Keystone } from './icons/Keystone';
import { MobileMenu } from './MobileMenu';
import { GitHub } from './icons/GitHub';
// TODO: Add in search for mobile via this button
// import { Search } from './icons/Search';
type HeaderContextType = { mobileNavIsOpen: boolean; desktopOpenState: number };
const HeaderContext = createContext<HeaderContextType>({
mobileNavIsOpen: false,
desktopOpenState: -1,
});
export const useHeaderContext = () => useContext(HeaderContext);
function Logo() {
const mq = useMediaQuery();
return (
<div
css={mq({
marginRight: [0, null, null, null, '1rem'],
marginTop: '0.1rem',
whiteSpace: 'nowrap',
})}
>
<Link href="/" passHref>
<a
css={{
fontSize: 'var(--font-medium)',
fontWeight: 600,
verticalAlign: 'middle',
transition: 'color 0.3s ease',
}}
>
<Keystone
grad="logo"
css={{
display: 'inline-block',
width: '2rem',
height: '2rem',
margin: '0 var(--space-medium) var(--space-xsmall) 0',
verticalAlign: 'middle',
}}
/>
<Highlight>Keystone 6</Highlight>
</a>
</Link>
</div>
);
}
function useCurrentSection() {
const { pathname } = useRouter();
const check = (candidate: string) => pathname.startsWith(candidate);
if (['/updates', '/releases'].some(check)) return '/updates';
if (['/why-keystone', '/for-'].some(check)) return '/why-keystone';
if (['/docs'].some(check)) return '/docs';
}
function LinkItem({ children, href }: { children: ReactNode; href: string }) {
const mq = useMediaQuery();
const currentSection = useCurrentSection();
const isActive = href === currentSection;
return (
<span css={mq({ display: ['none', 'inline'], fontWeight: 600 })}>
<NavItem
isActive={isActive}
alwaysVisible
href={href}
css={{
padding: '0 !important',
}}
>
{children}
</NavItem>
</span>
);
}
export function Header() {
const mq = useMediaQuery();
const router = useRouter();
const menuRef = useRef<HTMLDivElement>(null);
const headerRef = useRef<HTMLElement>(null);
const [mobileNavIsOpen, setMobileNavIsOpen] = useState(false);
const [desktopOpenState, setDesktopOpenState] = useState(-1);
useEffect(() => {
const listener = () => {
setMobileNavIsOpen(false);
setDesktopOpenState(-1);
const width = Math.max(
document.body.scrollWidth,
document.documentElement.scrollWidth,
document.body.offsetWidth,
document.documentElement.offsetWidth,
document.documentElement.clientWidth
);
if (width > BREAK_POINTS.sm) {
setDesktopOpenState(-1);
} else {
setDesktopOpenState(-1);
}
};
window.addEventListener('resize', debounce(listener, 130));
return () => {
window.removeEventListener('resize', debounce(listener, 130));
};
}, [setDesktopOpenState]);
useEffect(() => {
document.body.style.overflow = 'auto';
// search - init field
let searchAttempt = 0;
// @ts-ignore
document.getElementById('search-field').disabled = true;
const loadSearch = (searchAttempt: number) => {
// @ts-ignore
if (window.docsearch && searchAttempt < 10) {
// @ts-ignore
document.getElementById('search-field').disabled = false;
// @ts-ignore
window.docsearch({
apiKey: '211e94c001e6b4c6744ae72fb252eaba',
indexName: 'keystonejs',
inputSelector: '#search-field',
algoliaOptions: {
facetFilters: ['tags:stable'],
},
transformData: (results: any) => {
if (window.location.hostname == 'keystonejs.com') return results;
return results.map((result: object) => {
// @ts-ignore
result.url = result.url.replace('https://keystonejs.com', window.location.origin);
return result;
});
},
});
} else if (searchAttempt >= 10) {
// @ts-ignore
document.getElementById('search-field-container').style.visibility = 'hidden';
} else {
setTimeout(() => loadSearch(searchAttempt++), 500);
}
};
loadSearch(searchAttempt);
// search - keyboard shortcut
let keysPressed = {};
document.body.addEventListener('keydown', event => {
// @ts-ignore
keysPressed[event.key] = true;
// @ts-ignore
if (keysPressed['Meta'] && event.key == 'k') {
event.preventDefault();
document.getElementById('search-field')?.focus();
}
});
document.body.addEventListener('keyup', event => {
// @ts-ignore
delete keysPressed[event.key];
});
}, []);
const handleOpen = useCallback(() => {
setMobileNavIsOpen(true);
document.body.style.overflow = 'hidden';
document.getElementById('mobile-menu-close-btn')?.focus();
}, []);
const handleClose = useCallback(() => {
setMobileNavIsOpen(false);
document.body.style.overflow = 'auto';
document.getElementById('skip-link-navigation-btn')?.focus();
}, []);
useEffect(() => {
router.events.on('routeChangeComplete', handleClose);
return () => {
router.events.off('routeChangeComplete', handleClose);
};
}, [router.events, handleClose]);
return (
<header ref={headerRef}>
<Wrapper
css={mq({
display: 'grid',
gridTemplateColumns: [
'auto max-content max-content max-content',
'auto max-content max-content max-content max-content max-content max-content',
'max-content auto max-content max-content max-content max-content max-content',
'max-content auto max-content max-content max-content max-content max-content max-content',
'15rem auto max-content max-content max-content max-content max-content max-content',
],
gap: [
'var(--space-medium)',
'var(--space-large)',
'var(--space-medium)',
'var(--space-large)',
'var(--space-xlarge)',
],
justifyItems: 'start',
alignItems: 'center',
paddingTop: 'var(--space-xlarge)',
paddingBottom: 'var(--space-xlarge)',
color: 'var(--muted)',
'& a:hover': {
color: 'var(--link)',
},
marginBottom: '2rem',
})}
>
<Logo />
<div
id="search-field-container"
css={mq({
display: ['none', null, 'block'],
width: ['100%', null, null, null, '80%'],
})}
>
<SearchField />
</div>
<span
css={mq({
display: ['none', null, 'inline-block'],
})}
>
<LinkItem href="/why-keystone">Why Keystone</LinkItem>
</span>
<span
css={mq({
display: ['none', null, 'inline-block'],
})}
>
<LinkItem href="/updates">Updates</LinkItem>
</span>
{/* TODO: Add in search for mobile via this button */}
{/*
<button
css={mq({
display: ['inline-block', 'inline-block', 'none'],
appearance: 'none',
border: '0 none',
boxShadow: 'none',
background: 'transparent',
padding: '0.25rem',
cursor: 'pointer',
color: 'var(--muted)',
})}
>
<Search css={{ height: '1.4rem', marginTop: '0.2rem' }} />
</button>
*/}
<Button
as="a"
href="/docs"
shadow
css={mq({
'&&': {
display: ['none', null, 'inline-flex'],
},
})}
>
Documentation
</Button>
<DarkModeBtn />
<a
href="https://github.com/keystonejs/keystone"
target="_blank"
rel="noopener noreferrer"
css={mq({
display: ['none', null, 'inline-flex'],
padding: 0,
justifyContent: 'center',
borderRadius: '100%',
color: 'currentColor',
transition: 'color 0.3s ease',
':hover': {
color: '#000',
},
})}
>
<GitHub css={{ height: '1.5em' }} />
</a>
<HeaderContext.Provider value={{ mobileNavIsOpen, desktopOpenState }}>
<div
ref={menuRef}
css={mq({
display: ['inline-block', null, 'none'],
})}
>
<button
onClick={handleOpen}
id="skip-link-navigation-btn"
tabIndex={0}
css={mq({
appearance: 'none',
border: '0 none',
boxShadow: 'none',
background: 'transparent',
padding: '0.25rem',
cursor: 'pointer',
color: 'var(--muted)',
})}
>
<Hamburger css={{ height: '1.25rem' }} />
</button>
<MobileMenu handleClose={handleClose} />
</div>
</HeaderContext.Provider>
</Wrapper>
</header>
);
}