mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-06 04:42:40 +02:00
* feat(v2): headerlinks * fix test * nits * remove tictactoe: * headerIcon * nits * remove lang dropdown * fix failing test * search box * algolia search * use babel-eslint to resolve dynamic import * nits * favicon and title * Update .eslintignore
61 lines
1.5 KiB
JavaScript
61 lines
1.5 KiB
JavaScript
/**
|
|
* Copyright (c) 2017-present, Facebook, Inc.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
import React from 'react';
|
|
|
|
import './styles.css';
|
|
|
|
class Search extends React.Component {
|
|
constructor() {
|
|
super();
|
|
this.state = {
|
|
enabled: true,
|
|
};
|
|
}
|
|
|
|
componentDidMount() {
|
|
const {siteConfig = {}, metadata = {}} = this.props;
|
|
const {version: thisVersion, language: thisLanguage} = metadata;
|
|
const {algolia} = siteConfig;
|
|
|
|
// https://github.com/algolia/docsearch/issues/352
|
|
const isClient = typeof window !== 'undefined';
|
|
if (isClient) {
|
|
import('docsearch.js').then(({default: docsearch}) => {
|
|
docsearch({
|
|
appId: algolia.appId,
|
|
apiKey: algolia.apiKey,
|
|
indexName: algolia.indexName,
|
|
inputSelector: '#search_input_react',
|
|
algoliaOptions: JSON.parse(
|
|
JSON.stringify(algolia.algoliaOptions)
|
|
.replace('VERSION', thisVersion)
|
|
.replace('LANGUAGE', thisLanguage),
|
|
),
|
|
});
|
|
});
|
|
} else {
|
|
console.warn('Search has failed to load and now is being disabled');
|
|
this.setState({enabled: false});
|
|
}
|
|
}
|
|
|
|
render() {
|
|
const {enabled} = this.state;
|
|
|
|
return enabled ? (
|
|
<input
|
|
id="search_input_react"
|
|
type="search"
|
|
placeholder="Search docs"
|
|
aria-label="Search docs"
|
|
/>
|
|
) : null;
|
|
}
|
|
}
|
|
|
|
export default Search;
|