Skip to content

Commit

Permalink
move importDocSearchModalIfNeeded
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Dec 26, 2024
1 parent 74385ab commit e2b63a7
Showing 1 changed file with 18 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ type DocSearchProps = Omit<

let DocSearchModal: typeof DocSearchModalType | null = null;

function importDocSearchModalIfNeeded() {
if (DocSearchModal) {
return Promise.resolve();
}
return Promise.all([
import('@docsearch/react/modal') as Promise<
typeof import('@docsearch/react')
>,
import('@docsearch/react/style'),
import('./styles.css'),
]).then(([{DocSearchModal: Modal}]) => {
DocSearchModal = Modal;
});
}

function useNavigator({
externalUrlRegex,
}: Pick<DocSearchProps, 'externalUrlRegex'>) {
Expand Down Expand Up @@ -175,8 +190,9 @@ function useSearchParameters({

function DocSearch({externalUrlRegex, ...props}: DocSearchProps) {
const navigator = useNavigator({externalUrlRegex});

const searchParameters = useSearchParameters({...props});
const transformItems = useTransformItems(props);
const transformSearchClient = useTransformSearchClient();

const searchContainer = useRef<HTMLDivElement | null>(null);
// TODO remove "as any" after React 19 upgrade
Expand All @@ -186,22 +202,6 @@ function DocSearch({externalUrlRegex, ...props}: DocSearchProps) {
undefined,
);

const importDocSearchModalIfNeeded = useCallback(() => {
if (DocSearchModal) {
return Promise.resolve();
}

return Promise.all([
import('@docsearch/react/modal') as Promise<
typeof import('@docsearch/react')
>,
import('@docsearch/react/style'),
import('./styles.css'),
]).then(([{DocSearchModal: Modal}]) => {
DocSearchModal = Modal;
});
}, []);

const prepareSearchContainer = useCallback(() => {
if (!searchContainer.current) {
const divElement = document.createElement('div');
Expand All @@ -213,7 +213,7 @@ function DocSearch({externalUrlRegex, ...props}: DocSearchProps) {
const openModal = useCallback(() => {
prepareSearchContainer();
importDocSearchModalIfNeeded().then(() => setIsOpen(true));
}, [importDocSearchModalIfNeeded, prepareSearchContainer]);
}, [prepareSearchContainer]);

const closeModal = useCallback(() => {
setIsOpen(false);
Expand All @@ -235,12 +235,8 @@ function DocSearch({externalUrlRegex, ...props}: DocSearchProps) {
[openModal],
);

const transformItems = useTransformItems(props);

const resultsFooterComponent = useResultsFooterComponent({closeModal});

const transformSearchClient = useTransformSearchClient();

useDocSearchKeyboardEvents({
isOpen,
onOpen: openModal,
Expand Down

0 comments on commit e2b63a7

Please sign in to comment.