1
1
import React from 'react' ;
2
- import docsearch from 'docsearch.js' ;
3
2
import classNames from 'classnames' ;
4
3
5
4
import { ReactComponent as SearchIcon } from '../svgs/magnifier.svg' ;
@@ -10,16 +9,27 @@ class DocSearch extends React.Component {
10
9
focused : false ,
11
10
} ;
12
11
13
- componentDidMount ( ) {
12
+ hasSearchInited = false ;
13
+
14
+ initSearch = ( ) => {
15
+ if ( this . hasSearchInited ) {
16
+ return ;
17
+ }
14
18
// Initialize Algolia search.
15
- docsearch ( {
16
- apiKey : '2ed553faf01ab789d57bd2882ddd2b2d' ,
17
- indexName : 'wpack_io' ,
18
- inputSelector : '#algolia-doc-search' ,
19
+ this . hasSearchInited = true ;
20
+ import ( 'docsearch.js' ) . then ( ( { default : docsearch } ) => {
21
+ docsearch ( {
22
+ apiKey : '2ed553faf01ab789d57bd2882ddd2b2d' ,
23
+ indexName : 'wpack_io' ,
24
+ inputSelector : '#algolia-doc-search' ,
25
+ } ) ;
19
26
} ) ;
20
- }
27
+ } ;
21
28
22
- handleFocus = ( ) => this . setState ( { focused : true } ) ;
29
+ handleFocus = ( ) => {
30
+ this . initSearch ( ) ;
31
+ this . setState ( { focused : true } ) ;
32
+ } ;
23
33
24
34
handleBlur = ( ) => this . setState ( { focused : false } ) ;
25
35
@@ -31,6 +41,8 @@ class DocSearch extends React.Component {
31
41
className = { classNames ( 'wpackio-docsearch' , {
32
42
'wpackio-docsearch--focused' : focused ,
33
43
} ) }
44
+ onMouseEnter = { this . initSearch }
45
+ onTouchStart = { this . initSearch }
34
46
>
35
47
< div className = "control has-icons-left" >
36
48
< input
0 commit comments