-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathNavigation.jsx
244 lines (231 loc) · 7.41 KB
/
Navigation.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Import External Dependencies
import { useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import { DocSearch } from '@docsearch/react';
import { Link as ReactDOMLink, NavLink, useLocation } from 'react-router-dom';
// Import Components
import Link from '../Link/Link';
import Logo from '../Logo/Logo';
import Dropdown from '../Dropdown/Dropdown';
// Load Styling
import '@docsearch/css';
import GithubIcon from '../../styles/icons/github.svg';
import XIcon from '../../styles/icons/x.svg';
import StackOverflowIcon from '../../styles/icons/stack-overflow.svg';
import Hamburger from '../../styles/icons/hamburger.svg';
import HelloDarkness from '../HelloDarkness';
NavigationItem.propTypes = {
children: PropTypes.node.isRequired,
url: PropTypes.string.isRequired,
isactive: PropTypes.func,
};
function NavigationItem({ children, url, isactive }) {
let obj = {};
// decide if the link is active or not by providing a function
// otherwise we'll let react-dom makes the decision for us
if (isactive) {
obj = {
isactive,
};
}
const classes =
'text-gray-100 dark:text-gray-100 text-sm font-light uppercase hover:text-blue-200';
if (url.startsWith('http') || url.startsWith('//')) {
return (
<a
href={url}
target="_blank"
rel="noopener noreferrer"
className={classes}
>
{children}
</a>
);
}
return (
<NavLink
{...obj}
className={({ isActive }) =>
isActive ? `${classes} !text-blue-200` : classes
}
to={url}
>
{children}
</NavLink>
);
}
NavigationIcon.propTypes = {
children: PropTypes.node.isRequired,
to: PropTypes.string.isRequired,
title: PropTypes.string.isRequired,
};
function NavigationIcon({ children, to, title }) {
return (
<Link
to={to}
className="inline-flex items-center text-gray-100 dark:text-gray-200 hover:text-blue-200"
title={`webpack on ${title}`}
>
{children}
</Link>
);
}
const navigationIconProps = {
'aria-hidden': true,
fill: 'currentColor',
width: 16,
};
Navigation.propTypes = {
pathname: PropTypes.string,
hash: PropTypes.string,
links: PropTypes.array,
toggleSidebar: PropTypes.func,
theme: PropTypes.string,
switchTheme: PropTypes.func,
};
function Navigation({ links, pathname, hash = '', toggleSidebar }) {
const [locationHash, setLocationHash] = useState(hash);
const location = useLocation();
useEffect(() => {
setLocationHash(hash);
}, [hash]);
return (
<>
<header className="bg-blue-800 dark:bg-gray-900">
<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
className="bg-transparent border-none md:hidden"
onClick={toggleSidebar}
>
<Hamburger
width={20}
height={20}
className="fill-current text-white"
/>
<span className="sr-only">Hamburger Icon</span>
</button>
<Link to="/" className="md:mr-auto">
<Logo />
</Link>
<nav className="hidden md:inline-grid md:grid-flow-col md:gap-x-[18px]">
{links.map(({ content, url, isActive }) => (
<NavigationItem key={url} url={url} isActive={isActive}>
{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} />,
},
].map(({ to, title, children }) => (
<NavigationIcon key={to} to={to} title={title}>
{children}
</NavigationIcon>
))}
<Dropdown
className=""
items={[
{
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 ml-[18px]">
<HelloDarkness />
<DocSearch
appId="BH4D9OD16A"
apiKey={'fac401d1a5f68bc41f01fb6261661490'}
indexName="webpack-js-org"
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 }) => {
return <ReactDOMLink to={hit.url}>{children}</ReactDOMLink>;
}}
/>
</div>
</div>
{/* sub navigation */}
{links
.filter((link) => {
// only those with children are displayed
return link.children;
})
.map((link) => {
if (link.isactive) {
// hide the children if the link is not active
if (!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-blue-400 py-5 text-sm capitalize hover:text-black 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>
</>
);
}
export default Navigation;