Skip to content

Commit 521caf4

Browse files
skipjackSpaceK33z
authored andcommitted
Refactoring Navigation to get search working (required removing state for now)
1 parent 8f48679 commit 521caf4

File tree

2 files changed

+31
-56
lines changed

2 files changed

+31
-56
lines changed

components/navigation/navigation.jsx

Lines changed: 31 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,17 @@
11
import React from 'react';
2-
import CustomEvent from 'custom-event';
32
import Link from '../link/link';
43
import Container from '../container/container';
54
import Logo from '../logo/logo';
65
import './navigation-style';
76

87
export default class Navigation extends React.Component {
9-
constructor(props) {
10-
super(props);
11-
12-
this.state = {
13-
searchMode: false
14-
};
15-
}
16-
17-
componentDidMount() {
18-
window.docsearch({
19-
apiKey: 'fac401d1a5f68bc41f01fb6261661490',
20-
indexName: 'webpack-js-org',
21-
inputSelector: '.navigation__search-input'
22-
});
23-
}
24-
258
render() {
269
let { pageUrl, sections } = this.props;
2710
let isIndex = pageUrl === '/index';
2811
let transparentClass = isIndex ? 'navigation--transparent' : '';
29-
let searchClass = this.state.searchMode ? 'navigation--search-mode' : '';
3012

3113
return (
32-
<header className={ `navigation ${transparentClass} ${searchClass}` }>
14+
<header className={ `navigation ${transparentClass}` }>
3315
<Container className="navigation__inner">
3416
<div className="navigation__mobile" onClick={ this._toggleSidebar }>
3517
<i className="icon-menu" />
@@ -56,7 +38,7 @@ export default class Navigation extends React.Component {
5638
})
5739
}
5840

59-
<Link className="navigation__link" to={ '//opencollective.com/webpack' }>
41+
<Link className="navigation__link" to="//opencollective.com/webpack">
6042
Donate
6143
</Link>
6244
</nav>
@@ -65,13 +47,10 @@ export default class Navigation extends React.Component {
6547
<input
6648
type="text"
6749
className="navigation__search-input"
68-
ref={ ref => this.searchInput = ref }
69-
placeholder="Search documentation…"
70-
onFocus={ this._handleFocus.bind(this) }
71-
onChange={ this._handleSearch.bind(this) } />
50+
placeholder="Search documentation…" />
7251
<button
7352
className="navigation__search-icon"
74-
onClick={ this._toggleSearch.bind(this, !this.state.searchMode) }>
53+
onClick={ this._toggleSearch.bind(this) }>
7554
&#9906;
7655
</button>
7756
</div>
@@ -80,6 +59,24 @@ export default class Navigation extends React.Component {
8059
);
8160
}
8261

62+
componentDidMount() {
63+
if (typeof window !== 'undefined') {
64+
// Initialize DocSearch/Algolia
65+
window.docsearch({
66+
apiKey: 'fac401d1a5f68bc41f01fb6261661490',
67+
indexName: 'webpack-js-org',
68+
inputSelector: '.navigation__search-input'
69+
});
70+
71+
// Open the search on tabbing for usability
72+
window.addEventListener('keyup', e => {
73+
if (e.which === 9 && e.target.classList.contains('navigation__search-input')) {
74+
this._openSearch();
75+
}
76+
});
77+
}
78+
}
79+
8380
/**
8481
* Toggle the SidebarMobile component
8582
*
@@ -93,43 +90,22 @@ export default class Navigation extends React.Component {
9390
}
9491

9592
/**
96-
* Toggle search mode
97-
*
98-
* @param {boolean} state - True/false or null to toggle
99-
*/
100-
_toggleSearch(state) {
101-
this.setState({
102-
searchMode: state
103-
}, () => {
104-
if (state) this.searchInput.focus();
105-
});
106-
}
107-
108-
/**
109-
* Handle focus events on the search input
93+
* Toggle the search input
11094
*
111-
* @param {object} e - Native focus event
11295
*/
113-
_handleFocus(e) {
114-
this._toggleSearch(true);
96+
_toggleSearch() {
97+
let container = document.querySelector('.navigation');
98+
let input = document.querySelector('.navigation__search-input');
99+
let state = container.classList.toggle('navigation--search-mode');
115100

116-
if (e.target.value.length) {
117-
this._handleSearch(e);
118-
}
101+
if ( state === true ) input.focus();
119102
}
120103

121104
/**
122-
* Handle searching
105+
* Expand the search input
123106
*
124-
* @param {object} - Native click event
125107
*/
126-
_handleSearch(e) {
127-
window.dispatchEvent(
128-
new CustomEvent('search', {
129-
detail: {
130-
text: e.target.value
131-
}
132-
})
133-
);
108+
_openSearch() {
109+
container.classList.add('navigation--search-mode');
134110
}
135111
}

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@
9191
"yaml-frontmatter-loader": "0.0.3"
9292
},
9393
"dependencies": {
94-
"custom-event": "^1.0.1",
9594
"d3": "^4.2.7",
9695
"filesize": "^3.3.0",
9796
"preact": "^6.2.1",

0 commit comments

Comments
 (0)