1
1
import React from 'react' ;
2
- import CustomEvent from 'custom-event' ;
3
2
import Link from '../link/link' ;
4
3
import Container from '../container/container' ;
5
4
import Logo from '../logo/logo' ;
6
5
import './navigation-style' ;
7
6
8
7
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
-
25
8
render ( ) {
26
9
let { pageUrl, sections } = this . props ;
27
10
let isIndex = pageUrl === '/index' ;
28
11
let transparentClass = isIndex ? 'navigation--transparent' : '' ;
29
- let searchClass = this . state . searchMode ? 'navigation--search-mode' : '' ;
30
12
31
13
return (
32
- < header className = { `navigation ${ transparentClass } ${ searchClass } ` } >
14
+ < header className = { `navigation ${ transparentClass } ` } >
33
15
< Container className = "navigation__inner" >
34
16
< div className = "navigation__mobile" onClick = { this . _toggleSidebar } >
35
17
< i className = "icon-menu" />
@@ -56,7 +38,7 @@ export default class Navigation extends React.Component {
56
38
} )
57
39
}
58
40
59
- < Link className = "navigation__link" to = { ' //opencollective.com/webpack' } >
41
+ < Link className = "navigation__link" to = " //opencollective.com/webpack" >
60
42
Donate
61
43
</ Link >
62
44
</ nav >
@@ -65,13 +47,10 @@ export default class Navigation extends React.Component {
65
47
< input
66
48
type = "text"
67
49
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…" />
72
51
< button
73
52
className = "navigation__search-icon"
74
- onClick = { this . _toggleSearch . bind ( this , ! this . state . searchMode ) } >
53
+ onClick = { this . _toggleSearch . bind ( this ) } >
75
54
⚲
76
55
</ button >
77
56
</ div >
@@ -80,6 +59,24 @@ export default class Navigation extends React.Component {
80
59
) ;
81
60
}
82
61
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
+
83
80
/**
84
81
* Toggle the SidebarMobile component
85
82
*
@@ -93,43 +90,22 @@ export default class Navigation extends React.Component {
93
90
}
94
91
95
92
/**
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
110
94
*
111
- * @param {object } e - Native focus event
112
95
*/
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' ) ;
115
100
116
- if ( e . target . value . length ) {
117
- this . _handleSearch ( e ) ;
118
- }
101
+ if ( state === true ) input . focus ( ) ;
119
102
}
120
103
121
104
/**
122
- * Handle searching
105
+ * Expand the search input
123
106
*
124
- * @param {object } - Native click event
125
107
*/
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' ) ;
134
110
}
135
111
}
0 commit comments