-
-
Notifications
You must be signed in to change notification settings - Fork 521
Expand file tree
/
Copy pathindex.js
More file actions
277 lines (251 loc) Β· 6.35 KB
/
index.js
File metadata and controls
277 lines (251 loc) Β· 6.35 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
import { h, Component } from 'preact';
import cx from '../../lib/cx';
import { InvertedLogo } from '../logo';
import Search from './search';
import style from './style.module.less';
import { useStore } from '../store-adapter';
import config from '../../config.json';
import { useCallback, useEffect } from 'preact/hooks';
import ReleaseLink from './gh-version';
import Corner from './corner';
import { useOverlayToggle } from '../../lib/toggle-overlay';
import { route as reroute } from 'preact-router';
import { useLanguage } from '../../lib/i18n';
const LINK_FLAIR = {
logo: InvertedLogo
};
export default function Header() {
const { url } = useStore(['url']).state;
const [open, setOpen] = useOverlayToggle(false);
const toggle = useCallback(() => setOpen(!open), [open]);
// change to RTL based on language
const { lang } = useStore(['lang']).state;
useEffect(() => {
if (open) setOpen(false);
}, [url]);
return (
// Adding rtl directtion if persian
// TODO: Auto RTL language detection
<header
class={cx(
style.header,
open && style.open,
lang === 'fa' && style['dir-rtl']
)}
>
<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}>
<Nav class={style.nav} routes={config.nav} current={url} />
<Search />
<div class={style.social}>
<ReleaseLink class={cx(style.socialItem, style.release)} />
<a
class={style.socialItem}
aria-label="Browse the code on GitHub"
href="https://github.com/preactjs/preact"
>
<img
src="/assets/github.svg"
alt="GitHub"
width="34"
height="33"
/>
</a>
<a
class={style.socialItem}
aria-label="Follow us on Twitter"
href="https://twitter.com/preactjs"
>
<img
src="/assets/twitter.svg"
alt="Twitter"
width="34"
height="28"
/>
</a>
<NavItem />
</div>
<Hamburger open={open} onClick={toggle} />
</div>
</div>
<Corner />
</header>
);
}
// hamburger menu
const Hamburger = ({ open, ...props }) => (
<div class={style.hamburger} open={open} {...props}>
<div class={style.hb1} />
<div class={style.hb2} />
<div class={style.hb3} />
</div>
);
// nested nav renderer
const Nav = ({ routes, current, ...props }) => (
<nav {...props}>
{routes.map(route => (
<NavItem
to={route}
current={current}
data-route={getRouteIdent(route)}
class={cx(
route.class,
(pathMatchesRoute(current, route) ||
(route.content === 'guide' && /^\/guide\//.test(current)) ||
(route.content === 'blog' && /^\/blog\//.test(current))) &&
style.current
)}
/>
))}
</nav>
);
// nav items are really the only complex bit for menuing, since they handle click events.
class NavItem extends Component {
state = { open: false };
close = () => (this.setState({ open: false }), false);
toggle = () => (this.setState({ open: !this.state.open }), false);
handleClickOutside = ({ target }) => {
if (this.state.open) {
do {
if (target === this.base) return;
} while ((target = target.parentNode));
this.close();
}
};
componentDidMount() {
addEventListener('click', this.handleClickOutside);
}
componentWillUnmount() {
removeEventListener('click', this.handleClickOutside);
}
componentDidUpdate({ current }) {
if (current !== this.props.current && this.state.open) {
this.close();
}
}
render({ to, current, ...props }, { open }) {
if (!to)
return (
<LanguageSelector
isOpen={open}
toggle={this.toggle}
close={this.close}
{...props}
/>
);
if (!to.routes) return <NavLink to={to} {...props} />;
return (
<div {...props} data-open={open} class={style.navGroup}>
<NavLink to={to} onClick={this.toggle} aria-haspopup isOpen={open} />
<Nav
routes={to.routes}
current={current}
aria-label="submenu"
aria-hidden={'' + !open}
/>
</div>
);
}
}
// depending on the type of nav link, use <a>
const NavLink = ({ to, isOpen, route, ...props }) => {
const { lang } = useStore(['lang']).state;
let Flair = to.flair && LINK_FLAIR[to.flair];
if (to.skipHeader) return;
if (!to.path) {
return (
<button
{...props}
aria-haspopup="true"
aria-expanded={isOpen}
data-route={route}
>
{getRouteName(to, lang)}
</button>
);
}
function BrandingRedirect(e) {
if (to.href == '/' || to.path == '/') {
e.preventDefault();
reroute('/branding', false);
}
}
return (
<a
href={to.href || to.path}
{...props}
data-route={route}
onContextMenu={BrandingRedirect}
>
{Flair && <Flair />}
{getRouteName(to, lang)}
</a>
);
};
const LanguageSelector = ({ isOpen, toggle, close, ...props }) => {
const [lang, setLang] = useLanguage();
const onClick = useCallback(
e => {
setLang(e.target.dataset.value);
close();
},
[setLang]
);
return (
<div
{...props}
data-open={isOpen}
class={cx(style.navGroup, style.translation)}
>
<button {...props} onClick={toggle} aria-haspopup aria-expanded={isOpen}>
<img
src="/assets/i18n.svg"
alt="Translate Page"
width="34"
height="28"
/>
</button>
<nav aria-label="submenu" aria-hidden={'' + !isOpen}>
{Object.keys(config.languages).map(id => (
<span
class={cx(id == lang && style.current)}
data-value={id}
onClick={onClick}
>
{config.languages[id]}
</span>
))}
</nav>
</div>
);
};
export function getRouteName(route, lang) {
return typeof route.name === 'object'
? route.name[lang] || route.name.en
: route.name || route.title;
}
function pathMatchesRoute(path, route) {
if (!route || !route.path) return false;
if (path === route.path) return true;
let segs = path.replace(/(^\/|\/$)/g, '').split('/');
let psegs = route.path.replace(/(^\/|\/$)/g, '').split('/');
let len = Math.max(psegs.length, segs.length);
for (let i = 0; i < len; i++) {
let p = psegs[i];
let s = segs[i];
if (!p || (p[0] !== ':' && s !== p)) return false;
if (!s) return /[?*]$/g.test(p);
if (/[*+]$/g.test(p)) return true;
}
return true;
}
// get a CSS-addressable identifier for a given route
const getRouteIdent = route =>
(getRouteName(route, 'en') || route.url)
.toLowerCase()
.replace(/[^a-z0-9]/i, '');