diff --git a/packages/website/src/theme/SearchBar/index.js b/packages/website/src/theme/SearchBar/index.js index e7b0a92ae..267ac05d6 100644 --- a/packages/website/src/theme/SearchBar/index.js +++ b/packages/website/src/theme/SearchBar/index.js @@ -14,49 +14,37 @@ function Hit({ hit, children }) { return {children}; } -function SearchBar() { - const { siteConfig = {} } = useDocusaurusContext(); +function DocSearch({ + indexName, + appId = 'BH4D9OD16A', + apiKey, + searchParameters, +}) { const history = useHistory(); const [isOpen, setIsOpen] = useState(false); - const { - indexName, - appId = 'BH4D9OD16A', - apiKey, - searchParameters, - } = siteConfig.themeConfig.algolia; - - const importDocSearchModalIfNeeded = useCallback( - function importDocSearchModalIfNeeded() { - if (DocSearchModal) { - return Promise.resolve(); - } - - return Promise.all([ - import('@docsearch/react/modal'), - import('@docsearch/react/style'), - ]).then(([{ DocSearchModal: Modal }]) => { - DocSearchModal = Modal; - }); - }, - [] - ); + const importDocSearchModalIfNeeded = useCallback(() => { + if (DocSearchModal) { + return Promise.resolve(); + } - const onOpen = useCallback( - function onOpen() { - importDocSearchModalIfNeeded().then(() => { - setIsOpen(true); - }); - }, - [importDocSearchModalIfNeeded, setIsOpen] - ); + return Promise.all([ + import('@docsearch/react/modal'), + import('@docsearch/react/style'), + ]).then(([{ DocSearchModal: Modal }]) => { + DocSearchModal = Modal; + }); + }, []); - const onClose = useCallback( - function onClose() { - setIsOpen(false); - }, - [setIsOpen] - ); + const onOpen = useCallback(() => { + importDocSearchModalIfNeeded().then(() => { + setIsOpen(true); + }); + }, [importDocSearchModalIfNeeded, setIsOpen]); + + const onClose = useCallback(() => { + setIsOpen(false); + }, [setIsOpen]); useDocSearchKeyboardEvents({ isOpen, onOpen, onClose }); @@ -115,4 +103,19 @@ function SearchBar() { ); } +function SearchBar() { + const { siteConfig = {} } = useDocusaurusContext(); + + if (!siteConfig.themeConfig.algolia) { + // eslint-disable-next-line no-console + console.warn(`DocSearch requires an \`algolia\` field in your \`themeConfig\`. + +See: https://v2.docusaurus.io/docs/search/#using-algolia-docsearch`); + + return null; + } + + return ; +} + export default SearchBar;