-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathNavigation.jsx
212 lines (187 loc) · 5.93 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
import React from 'react';
import Link from '../Link/Link';
import Container from '../Container/Container';
import Logo from '../Logo/Logo';
import Dropdown from '../Dropdown/Dropdown';
// TODO: Maybe by updating the routing scheme later on we can avoid hardcoding this?
let Sections = [
{
title: 'Concepts',
url: 'concepts'
},
{
title: 'Guides',
url: 'guides'
},
{
title: 'Documentation',
url: 'configuration',
children: [
{ title: 'CLI', url: 'api/cli' },
{ title: 'API', url: 'api' },
{ title: 'Configuration', url: 'configuration' },
{ title: 'Loaders', url: 'loaders' },
{ title: 'Plugins', url: 'plugins' },
{ title: 'Development', url: 'development' }
]
},
{
title: 'Donate',
url: '//opencollective.com/webpack'
},
{
title: 'Blog',
url: '//medium.com/webpack'
},
{
title: 'Support',
url: 'support'
}
];
// TODO: Move back to using state once we can handle algolia on our own
export default class Navigation extends React.Component {
render() {
let { pageUrl = '' } = this.props;
return (
<header className="navigation">
<Container className="navigation__inner">
<div className="navigation__mobile" onClick={ this._toggleSidebar }>
<i className="icon-menu" />
</div>
<Link className="navigation__logo" to="/">
<Logo light={ true } />
</Link>
<nav className="navigation__links">
{
Sections.map(section => {
let active = this._isActive(section);
let activeMod = active ? 'navigation__link--active' : '';
return (
<Link
key={ `navigation__link-${section.title}` }
className={ `navigation__link ${activeMod}` }
to={ `/${section.url}` }>
{ section.title }
</Link>
);
})
}
</nav>
<div className="navigation__search">
<input
type="text"
className="navigation__search-input"
placeholder="Search documentation…"
onBlur={ this._toggleSearch.bind(this) } />
<button
className="navigation__search-icon icon-magnifying-glass"
onClick={ this._toggleSearch.bind(this) } />
<button
className="navigation__search-icon icon-cross"
onClick={ this._toggleSearch.bind(this) } />
</div>
<Link
className="navigation__icon"
title="GitHub Repository"
to="//github.com/webpack/webpack">
<i className="sidecar__icon icon-github" />
</Link>
<Link
className="navigation__icon"
title="See Questions on Stack Overflow"
to="//stackoverflow.com/questions/tagged/webpack">
<i className="sidecar__icon icon-stack-overflow" />
</Link>
<Dropdown
className="navigation__languages"
items={[
{ title: 'English', url: 'https://webpack.js.org/' },
{ title: '中文', url: 'https://doc.webpack-china.org/' }
]} />
</Container>
{
Sections.filter(section => this._isActive(section) && section.children).map(section => {
return (
<div className="navigation__bottom" key={ section.title }>
<Container className="navigation__inner">
{
section.children.map(child => {
let activeMod = this._isActive(child) ? 'navigation__child--active' : '';
return (
<Link
key={ `navigation__child-${child.title}` }
className={ `navigation__child ${activeMod}` }
to={ `/${child.url}` }>
{ child.title }
</Link>
);
})
}
</Container>
</div>
);
})
}
</header>
);
}
componentDidMount() {
if (typeof window !== 'undefined') {
let docsearch = () => {};
// XXX: hack around docsearch
if (window.docsearch) {
docsearch = window.docsearch.default || window.docsearch;
}
docsearch({
apiKey: 'fac401d1a5f68bc41f01fb6261661490',
indexName: 'webpack-js-org',
inputSelector: '.navigation__search-input'
});
window.addEventListener('keyup', e => {
if (e.which === 9 && e.target.classList.contains('navigation__search-input')) {
this._openSearch();
}
});
}
}
/**
* Check if section is active
*
* @param {object} section - An object describing the section
* @return {bool} - Whether or not the given section is active
*/
_isActive(section) {
let { pageUrl = '' } = this.props;
if (section.children) {
return section.children.some(child => pageUrl.includes(`${child.url}/`));
} else return pageUrl.includes(`${section.url}/`);
}
/**
* Toggle the SidebarMobile component
*
* @param {object} e - Native click event
*/
_toggleSidebar(e) {
let sidebar = document.querySelector('.sidebar-mobile');
let modifier = 'sidebar-mobile--visible';
sidebar.classList.toggle(modifier);
}
/**
* Toggle the search input
*
*/
_toggleSearch() {
let container = document.querySelector('.navigation');
let input = document.querySelector('.navigation__search-input');
let state = container.classList.toggle('navigation--search-mode');
if ( state === true ) input.focus();
}
/**
* Expand the search input
*
*/
_openSearch() {
let container = document.querySelector('.navigation');
container.classList.add('navigation--search-mode');
}
}