Skip to content
This repository has been archived by the owner on Jun 11, 2021. It is now read-only.

Commit

Permalink
feat(docsearch): append modal to body
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Apr 9, 2020
1 parent ba60df8 commit 73a7f0e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
3 changes: 2 additions & 1 deletion packages/docsearch-react/src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ html[data-theme='dark'] {
--docsearch-muted-color: rgb(127, 132, 151);
}

.DocSearch--active .main-wrapper {
.DocSearch--active #__docusaurus {
filter: blur(2px);
}

Expand Down Expand Up @@ -167,6 +167,7 @@ html[data-theme='dark'] {
}

.DocSearch-Container {
z-index: var(--ifm-z-index-fixed);
height: 100vh;
width: 100vw;
position: fixed;
Expand Down
37 changes: 22 additions & 15 deletions packages/website/src/theme/SearchBar/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/* eslint-disable import/no-unresolved */

import React, { useState, useEffect, useCallback } from 'react';
import { createPortal } from 'react-dom';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import { SearchButton } from 'docsearch-react';

let DocSearch = null;

export default function SearchBar() {
function SearchBar() {
const [isLoaded, setIsLoaded] = useState(false);
const [isShowing, setIsShowing] = useState(false);
const { siteConfig = {} } = useDocusaurusContext();
Expand All @@ -21,10 +22,10 @@ export default function SearchBar() {
const load = useCallback(
function load() {
if (isLoaded === true) {
return;
return Promise.resolve();
}

Promise.all([
return Promise.all([
import('docsearch-react'),
import('docsearch-react/dist/esm/style.css'),
]).then(([{ DocSearch: DocSearchComp }]) => {
Expand All @@ -37,9 +38,10 @@ export default function SearchBar() {

const onOpen = useCallback(
function onOpen() {
load();
setIsShowing(true);
document.body.classList.add('DocSearch--active');
load().then(() => {
setIsShowing(true);
document.body.classList.add('DocSearch--active');
});
},
[load, setIsShowing]
);
Expand Down Expand Up @@ -79,15 +81,20 @@ export default function SearchBar() {
<>
<SearchButton onClick={onOpen} />

{isLoaded && isShowing && (
<DocSearch
appId={appId}
apiKey={apiKey}
indexName={indexName}
searchParameters={searchParameters}
onClose={onClose}
/>
)}
{isLoaded &&
isShowing &&
createPortal(
<DocSearch
appId={appId}
apiKey={apiKey}
indexName={indexName}
searchParameters={searchParameters}
onClose={onClose}
/>,
document.body
)}
</>
);
}

export default SearchBar;

0 comments on commit 73a7f0e

Please sign in to comment.