Skip to content

Commit

Permalink
fix: have docsearch.js dynamically imported
Browse files Browse the repository at this point in the history
This makes it work with server rendered gatsby.
  • Loading branch information
swashata committed Oct 29, 2018
1 parent 70424aa commit 74136ab
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions site/src/components/docsearch/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';
import docsearch from 'docsearch.js';
import classNames from 'classnames';

import { ReactComponent as SearchIcon } from '../svgs/magnifier.svg';
Expand All @@ -10,16 +9,27 @@ class DocSearch extends React.Component {
focused: false,
};

componentDidMount() {
hasSearchInited = false;

initSearch = () => {
if (this.hasSearchInited) {
return;
}
// Initialize Algolia search.
docsearch({
apiKey: '2ed553faf01ab789d57bd2882ddd2b2d',
indexName: 'wpack_io',
inputSelector: '#algolia-doc-search',
this.hasSearchInited = true;
import('docsearch.js').then(({ default: docsearch }) => {
docsearch({
apiKey: '2ed553faf01ab789d57bd2882ddd2b2d',
indexName: 'wpack_io',
inputSelector: '#algolia-doc-search',
});
});
}
};

handleFocus = () => this.setState({ focused: true });
handleFocus = () => {
this.initSearch();
this.setState({ focused: true });
};

handleBlur = () => this.setState({ focused: false });

Expand All @@ -31,6 +41,8 @@ class DocSearch extends React.Component {
className={classNames('wpackio-docsearch', {
'wpackio-docsearch--focused': focused,
})}
onMouseEnter={this.initSearch}
onTouchStart={this.initSearch}
>
<div className="control has-icons-left">
<input
Expand Down

0 comments on commit 74136ab

Please sign in to comment.