-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathNavigation.jsx
More file actions
294 lines (275 loc) · 8.74 KB
/
Navigation.jsx
File metadata and controls
294 lines (275 loc) · 8.74 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
// Import External Dependencies
import { DocSearch } from "@docsearch/react";
import PropTypes from "prop-types";
import { useEffect, useRef, useState } from "react";
import { Link as ReactDOMLink, NavLink, useLocation } from "react-router-dom";
// Import Internal Config
import {
DOCSEARCH_API_KEY,
DOCSEARCH_APP_ID,
DOCSEARCH_INDEX_NAME,
} from "../../config/docsearch.js";
// Import Components
import DiscordIcon from "../../styles/icons/discord.svg";
import GithubIcon from "../../styles/icons/github.svg";
import Hamburger from "../../styles/icons/hamburger.svg";
import StackOverflowIcon from "../../styles/icons/stack-overflow.svg";
import XIcon from "../../styles/icons/x.svg";
import Dropdown from "../Dropdown/Dropdown.jsx";
import HelloDarkness from "../HelloDarkness.jsx";
import Link from "../Link/Link.jsx";
import Logo from "../Logo/Logo.jsx";
import Tooltip from "../Tooltip/Tooltip.jsx";
// Load Styling
// eslint-disable-next-line import/no-extraneous-dependencies
import "@docsearch/css";
function NavigationItem({
children,
url,
isActive: isCustomActive,
ariaLabel,
}) {
const location = useLocation();
const classes =
"text-gray-100 dark:text-gray-100 text-sm font-light uppercase hover:text-blue-200";
const getActiveState = (isNavLinkActive) => {
if (isCustomActive) {
return isCustomActive({}, location);
}
return isNavLinkActive;
};
if (url.startsWith("http") || url.startsWith("//")) {
return (
<a
href={url}
target="_blank"
rel="noopener noreferrer"
className={classes}
aria-label={ariaLabel}
>
{children}
</a>
);
}
return (
<NavLink
className={({ isActive: isNavLinkActive }) =>
getActiveState(isNavLinkActive) ? `${classes} !text-blue-200` : classes
}
to={url}
aria-label={ariaLabel}
>
{children}
</NavLink>
);
}
NavigationItem.propTypes = {
children: PropTypes.node.isRequired,
url: PropTypes.string.isRequired,
isActive: PropTypes.func,
ariaLabel: PropTypes.string,
};
function NavigationIcon({ children, to, title }) {
const tooltipRef = useRef();
const hideTooltip = () => {
if (tooltipRef.current) {
tooltipRef.current.blur();
}
};
return (
<Tooltip content={`webpack on ${title}`} ref={tooltipRef}>
<Link
to={to}
className="inline-flex items-center text-gray-100 dark:text-gray-200 hover:text-blue-200"
aria-label={`webpack on ${title}`}
onClick={hideTooltip}
>
{children}
</Link>
</Tooltip>
);
}
NavigationIcon.propTypes = {
children: PropTypes.node.isRequired,
to: PropTypes.string.isRequired,
title: PropTypes.string.isRequired,
};
const navigationIconProps = {
"aria-hidden": true,
fill: "currentColor",
width: 16,
};
function Navigation({ links, pathname, hash = "", toggleSidebar }) {
const [locationHash, setLocationHash] = useState(hash);
const [mounted, setMounted] = useState(false);
const location = useLocation();
useEffect(() => {
setLocationHash(hash);
}, [hash]);
useEffect(() => {
setMounted(true);
}, []);
return (
<>
<header className="bg-blue-800 dark:bg-gray-900 print:hidden">
<div className="flex items-center py-10 px-[16px] justify-between md:px-[24px] md:max-w-[1024px] md:mx-auto md:justify-start">
<button
aria-label="Toggle navigation menu"
className="bg-transparent border-none md:hidden"
onClick={toggleSidebar}
>
<Hamburger
width={20}
height={20}
className="fill-current text-white"
/>
</button>
<Link to="/" className="md:mr-auto">
<Logo />
</Link>
<nav
className="hidden md:inline-grid md:grid-flow-col md:gap-x-[18px] md:items-center"
aria-label="Main navigation"
>
{links.map(({ content, url, isActive, ariaLabel }) => (
<NavigationItem
key={url}
url={url}
isActive={isActive}
ariaLabel={ariaLabel}
>
{content}
</NavigationItem>
))}
{[
{
to: "https://github.com/webpack/webpack",
title: "GitHub",
children: <GithubIcon {...navigationIconProps} />,
},
{
to: "https://x.com/webpack",
title: "X",
children: <XIcon {...navigationIconProps} />,
},
{
to: "https://stackoverflow.com/questions/tagged/webpack",
title: "StackOverflow",
children: <StackOverflowIcon {...navigationIconProps} />,
},
{
to: "https://discord.com/invite/webpack",
title: "Discord",
children: <DiscordIcon {...navigationIconProps} />,
},
].map(({ to, title, children }) => (
<NavigationIcon key={to} to={to} title={title}>
{children}
</NavigationIcon>
))}
<Dropdown
className=""
items={[
{
lang: "en",
title: "English",
url: `https://webpack.js.org${pathname}${locationHash}`,
},
{
lang: "zh",
title: "中文",
url: `https://webpack.docschina.org${pathname}${locationHash}`,
},
{
lang: "ko",
title: "한국어",
url: `https://webpack.kr${pathname}${locationHash}`,
},
]}
/>
</nav>
<div className="inline-flex items-center gap-x-[18px] ml-[18px]">
<HelloDarkness />
{mounted && (
<DocSearch
appId={DOCSEARCH_APP_ID}
apiKey={DOCSEARCH_API_KEY}
indexName={DOCSEARCH_INDEX_NAME}
disableUserPersonalization={true}
placeholder="Search webpack documentation"
transformItems={(items) =>
items.map(({ url, ...others }) => {
const { origin } = new URL(url);
return {
...others,
url: url.replace(new RegExp(`^${origin}`), ""),
};
})
}
hitComponent={({ hit, children }) => (
<ReactDOMLink to={hit.url}>{children}</ReactDOMLink>
)}
/>
)}
</div>
</div>
{/* sub navigation */}
{links
.filter((link) => link.children)
.map((link) => {
if (link.isActive && !link.isActive({}, location)) {
return null;
}
return (
<div
key={link.url}
className="bg-gray-100 dark:bg-gray-800 hidden md:block"
>
<div
className="md:max-w-[1024px] md:mx-auto md:grid md:grid-flow-col md:justify-end md:gap-x-[20px] md:px-[24px]"
data-testid="sub-navigation"
>
{link.children.map((child) => {
const classNames =
"text-[#1d78c1] dark:text-[#8dd6f9] py-5 text-sm capitalize hover:text-[#465E69] dark:hover:text-white";
const isActive = location.pathname.startsWith(child.url);
return (
<NavLink
key={child.url}
to={child.url}
title={child.title}
className={() =>
isActive
? `!text-black dark:!text-white ${classNames}`
: classNames
}
>
{child.content === "api"
? child.content.toUpperCase()
: child.content}
</NavLink>
);
})}
</div>
</div>
);
})}
</header>
</>
);
}
Navigation.propTypes = {
pathname: PropTypes.string,
hash: PropTypes.string,
links: PropTypes.arrayOf(
PropTypes.shape({
content: PropTypes.node.isRequired,
url: PropTypes.string.isRequired,
isActive: PropTypes.func,
ariaLabel: PropTypes.string,
children: PropTypes.arrayOf(PropTypes.object),
}),
).isRequired,
toggleSidebar: PropTypes.func,
};
export default Navigation;