forked from webpack/webpack.js.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNavigation.jsx
241 lines (226 loc) · 7.38 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
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 Link from '../Link/Link';
import Logo from '../Logo/Logo';
import Dropdown from '../Dropdown/Dropdown';
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 Component
function NavigationItem({ children, url, isActive }) {
let obj = {};
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>
);
}
NavigationItem.propTypes = {
children: PropTypes.node.isRequired,
url: PropTypes.string.isRequired,
isActive: PropTypes.func,
};
// NavigationIcon component with tooltip
function NavigationIcon({ children, to, title }) {
return (
<div className="relative group inline-flex flex-col items-center">
<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>
{/* Tooltip */}
<div className="absolute top-full mt-2 px-3 py-1 bg-blue-500 text-white text-sm rounded shadow-md opacity-0 group-hover:opacity-100 transition-opacity duration-200 pointer-events-none">
{title}
</div>
</div>
);
}
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 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"
/>
</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>
))}
{/* Social Media Icons with Tooltips */}
{[
{
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) => 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-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>
</>
);
}
Navigation.propTypes = {
pathname: PropTypes.string,
hash: PropTypes.string,
links: PropTypes.array,
toggleSidebar: PropTypes.func,
};
export default Navigation;