-
-
Notifications
You must be signed in to change notification settings - Fork 521
Expand file tree
/
Copy pathindex.jsx
More file actions
292 lines (268 loc) Β· 7.17 KB
/
index.jsx
File metadata and controls
292 lines (268 loc) Β· 7.17 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
import cx from '../../lib/cx';
import { InvertedLogo } from '../logo';
import Search from './search';
import style from './style.module.css';
import config from '../../config.json';
import { useCallback, useEffect, useState } from 'preact/hooks';
import ReleaseLink from './gh-version';
import Corner from './corner';
import { useOverlayToggle } from '../../lib/toggle-overlay';
import { useLocation } from 'preact-iso';
import { useLanguageContext, useTranslate } from '../../lib/i18n';
import { headerNav } from '../../route-config.js';
export default function Header() {
const { url } = useLocation();
const [open, setOpen] = useOverlayToggle();
const toggle = useCallback(() => setOpen(!open), [open]);
useEffect(() => {
if (open) setOpen(false);
}, [url]);
return (
<header class={cx(style.header, open && style.open)}>
<div class={style.banner}>
<a href="https://www.stopputin.net/">
We stand with Ukraine. <b>Show your support</b> πΊπ¦
</a>
</div>
<div class={style.outer}>
<div class={style.inner}>
<MainNav />
<Search />
<SocialLinks />
<LanguagePicker />
<HamburgerMenu open={open} onClick={toggle} />
</div>
</div>
<Corner />
</header>
);
}
function MainNav() {
const { path, route } = useLocation();
const translate = useTranslate();
const brandingRedirect = e => {
e.preventDefault();
route('/branding');
};
return (
<nav class={style.nav}>
<NavLink
href="/"
clsx="home"
flair={<InvertedLogo title="Preact Logo" />}
onContextMenu={brandingRedirect}
aria-label="Home"
/>
<NavLink href="/tutorial" />
<NavLink href="/guide/v10/getting-started" />
<NavMenu>
{isOpen => (
<ExpandableNavLink
isOpen={isOpen}
label={translate('headerNav', 'about')}
class={cx(path.startsWith('/about') && style.current)}
>
<>
<NavLink href="/about/we-are-using" />
<NavLink href="/about/libraries-addons" />
<NavLink href="/about/demos-examples" />
<NavLink href="/about/project-goals" />
<NavLink href="/about/browser-support" />
</>
</ExpandableNavLink>
)}
</NavMenu>
<NavLink href="/blog" />
<NavLink href="/repl" />
</nav>
);
}
function SocialLinks() {
return (
<div class={style.social}>
<ReleaseLink class={cx(style.socialItem, style.release)} />
<SocialIcon
label="Browse the code on GitHub"
href="https://github.com/preactjs/preact"
viewbox="0 0 24 24"
id="github"
/>
<SocialIcon
label="Follow us on Twitter"
href="https://twitter.com/preactjs"
viewbox="0 0 34 27.646"
id="twitter"
/>
<SocialIcon
label="Follow us on Bluesky"
href="https://bsky.app/profile/preactjs.com"
viewbox="0 0 568 501"
id="bluesky"
/>
<SocialIcon
label="Chat with us on Slack"
href="http://chat.preactjs.com/"
viewbox="0 0 512 512"
id="slack"
/>
</div>
);
}
function LanguagePicker() {
const { lang, setLang } = useLanguageContext();
const translate = useTranslate();
return (
<div class={style.translation}>
<NavMenu>
{isOpen => (
<ExpandableNavLink
isOpen={isOpen}
label={
<svg aria-hidden viewBox="0 0 24 24">
<use href="/icons.svg#i18n" />
</svg>
}
aria-label={translate('i18n', 'selectYourLanguage')}
>
{Object.entries(config.locales).map(([id, label]) => (
<button
class={cx(id == lang && style.current)}
data-value={id}
onClick={e => setLang(e.currentTarget.dataset.value)}
>
{label}
</button>
))}
</ExpandableNavLink>
)}
</NavMenu>
</div>
);
}
/**
* @param {Object} props
* @param {string} props.label - The aria label for the social icon
* @param {string} props.href - The URL the icon links to
* @param {string} props.viewbox - The SVG viewBox attribute -- must match the SVG's viewBox
* @param {string} props.id - The ID of the icon in the `/icons.svg` spritesheet
*/
const SocialIcon = ({ label, href, viewbox, id }) => (
<a
class={style.socialItem}
aria-label={label}
href={href}
target="_blank"
rel="noopener noreferrer"
>
<svg aria-hidden viewBox={viewbox}>
<use href={`/icons.svg#${id}`} />
</svg>
</a>
);
/**
* @param {{ open: boolean } & import('preact').HTMLAttributes<HTMLDivElement>} props
*/
const HamburgerMenu = ({ open, ...props }) => (
<div class={style.hamburger} data-open={open} {...props}>
<div class={style.hb1} />
<div class={style.hb2} />
<div class={style.hb3} />
</div>
);
/**
* Sets up event listeners to toggle submenu visibility. Takes the menu as a child function
*
* @param {Object} props
* @param {(open: boolean) => import('preact').JSX.Element} props.children
*/
function NavMenu(props) {
const [isOpen, setIsOpen] = useState(false);
useEffect(() => {
// We don't actually have to check where the click occurs, as if
// it happens within the menu, the toggle handler will close it.
// Therefore, this will only catch the clicks outside the menu.
const handleClickOutside = () => {
if (isOpen) setIsOpen(false);
};
addEventListener('click', handleClickOutside);
return () => removeEventListener('click', handleClickOutside);
}, [isOpen]);
return (
<div
class={style.navGroup}
data-open={isOpen}
onClick={() => setIsOpen(!isOpen)}
>
{props.children(isOpen)}
</div>
);
}
/**
* @typedef {Object} ExpandableNavLinkProps
* @property {boolean} props.isOpen
* @property {string | import('preact').JSX.Element} props.label
* @property {import('preact').ComponentChildren} props.children
*/
/**
* Button that expands into a menu when clicked. Pass in label & menu items as children.
*
* @param {ExpandableNavLinkProps & import('preact').ButtonHTMLAttributes} props
*/
function ExpandableNavLink({ isOpen, label, children, ...rest }) {
return (
<>
<button aria-haspopup aria-expanded={isOpen} {...rest}>
{label}
</button>
<nav aria-label="submenu" aria-hidden={!isOpen}>
{children}
</nav>
</>
);
}
/**
* @typedef {Object} NavLinkProps
* @property {keyof typeof import('../../route-config.js').headerNav} props.href
* @property {string} [props.clsx]
* @property {import('preact').ComponentChildren} [props.flair]
* @property {boolean} [props.isOpen]
*/
/**
* @param {NavLinkProps & Omit<import('preact').AnchorHTMLAttributes, 'role'>} props
*/
function NavLink({ href, flair, clsx, isOpen, ...rest }) {
const { path } = useLocation();
const translate = useTranslate();
return (
<a
href={href}
class={cx(pathMatchesHref(path, href) && style.current, clsx)}
{...rest}
>
{flair}
{translate('headerNav', pathToI18nLabel(href))}
</a>
);
}
/**
* @param {string} path
* @param {string} href
* @returns {boolean}
*/
function pathMatchesHref(path, href) {
if (!path || !href) return false;
if (path === href) return true;
if (href !== '/' && path.startsWith(href)) return true;
if (path.startsWith('/guide/') && href.startsWith('/guide/')) return true;
return false;
}
/**
* @typedef {import('../../locales/en.json')} Translations
*/
/**
* @param {keyof typeof headerNav} path
* @returns {keyof Translations['headerNav']}
*/
function pathToI18nLabel(path) {
return headerNav[path].label;
}