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

Commit

Permalink
feat(docusaurus): import DocSearch modal on hover
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed May 14, 2020
1 parent 218944e commit e680f24
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
17 changes: 7 additions & 10 deletions packages/docsearch-react/src/DocSearchButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ import React, { useState, useEffect } from 'react';

import { SearchIcon } from './icons/SearchIcon';

interface SearchButtonProps {
onClick(): void;
}

const ACTION_KEY_DEFAULT = 'Ctrl';
const ACTION_KEY_APPLE = '⌘';

Expand All @@ -17,7 +13,12 @@ function isAppleDevice() {
return /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);
}

export function DocSearchButton(props: SearchButtonProps) {
export function DocSearchButton(
props: React.DetailedHTMLProps<
React.ButtonHTMLAttributes<HTMLButtonElement>,
HTMLButtonElement
>
) {
const [key, setKey] = useState(() =>
isAppleDevice() ? ACTION_KEY_APPLE : ACTION_KEY_DEFAULT
);
Expand All @@ -29,11 +30,7 @@ export function DocSearchButton(props: SearchButtonProps) {
}, []);

return (
<button
type="button"
className="DocSearch-SearchButton"
onClick={props.onClick}
>
<button type="button" className="DocSearch-SearchButton" {...props}>
<SearchIcon />
<span className="DocSearch-SearchButton-Placeholder">Search</span>
<span className="DocSearch-SearchButton-Key">{key}</span>
Expand Down
35 changes: 21 additions & 14 deletions packages/website/src/theme/SearchBar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,29 @@ function SearchBar() {
searchParameters,
} = siteConfig.themeConfig.algolia;

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

return Promise.all([
import('@francoischalifour/docsearch-react/modal'),
import('@francoischalifour/docsearch-react/style'),
]).then(([{ DocSearchModal: Modal }]) => {
DocSearchModal = Modal;
});
}, []);
return Promise.all([
import('@francoischalifour/docsearch-react/modal'),
import('@francoischalifour/docsearch-react/style'),
]).then(([{ DocSearchModal: Modal }]) => {
DocSearchModal = Modal;
});
},
[]
);

const onOpen = useCallback(
function onOpen() {
load().then(() => {
importDocSearchModalIfNeeded().then(() => {
setIsOpen(true);
});
},
[load, setIsOpen]
[importDocSearchModalIfNeeded, setIsOpen]
);

const onClose = useCallback(
Expand All @@ -66,7 +69,11 @@ function SearchBar() {
/>
</Head>

<DocSearchButton onClick={onOpen} />
<DocSearchButton
onTouchStart={importDocSearchModalIfNeeded}
onMouseOver={importDocSearchModalIfNeeded}
onClick={onOpen}
/>

{isOpen &&
createPortal(
Expand Down

0 comments on commit e680f24

Please sign in to comment.