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

Commit

Permalink
feat(docsearch): support initialQuery
Browse files Browse the repository at this point in the history
Closes #51
  • Loading branch information
francoischalifour committed Jul 20, 2020
1 parent 89a9e24 commit 11aa27b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 9 deletions.
18 changes: 16 additions & 2 deletions packages/docsearch-react/src/DocSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ export interface DocSearchProps
}): JSX.Element | null;
transformSearchClient?(searchClient: SearchClient): SearchClient;
disableUserPersonalization?: boolean;
initialQuery?: string;
}

export function DocSearch(props: DocSearchProps) {
const [isOpen, setIsOpen] = React.useState(false);
const searchButtonRef = React.useRef<HTMLButtonElement>(null);
const [isOpen, setIsOpen] = React.useState(false);
const [initialQuery, setInitialQuery] = React.useState(undefined);

const onOpen = React.useCallback(() => {
setIsOpen(true);
Expand All @@ -41,7 +43,18 @@ export function DocSearch(props: DocSearchProps) {
setIsOpen(false);
}, [setIsOpen]);

useDocSearchKeyboardEvents({ isOpen, onOpen, onClose, searchButtonRef });
function onInput(event) {
setIsOpen(true);
setInitialQuery(event.key);
}

useDocSearchKeyboardEvents({
isOpen,
onOpen,
onClose,
onInput,
searchButtonRef,
});

return (
<>
Expand All @@ -52,6 +65,7 @@ export function DocSearch(props: DocSearchProps) {
<DocSearchModal
{...props}
initialScrollY={window.scrollY}
initialQuery={initialQuery}
onClose={onClose}
/>,
document.body
Expand Down
3 changes: 2 additions & 1 deletion packages/docsearch-react/src/DocSearchModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export function DocSearchModal({
initialScrollY = 0,
transformSearchClient = identity,
disableUserPersonalization = false,
initialQuery: initialQueryFromProp = '',
}: DocSearchModalProps) {
const [state, setState] = React.useState<
AutocompleteState<InternalDocSearchHit>
Expand All @@ -55,7 +56,7 @@ export function DocSearchModal({
const inputRef = React.useRef<HTMLInputElement | null>(null);
const snipetLength = React.useRef<number>(10);
const initialQuery = React.useRef(
typeof window !== 'undefined'
initialQueryFromProp || typeof window !== 'undefined'
? window.getSelection()!.toString().slice(0, MAX_QUERY_SIZE)
: ''
).current;
Expand Down
18 changes: 14 additions & 4 deletions packages/docsearch-react/src/useDocSearchKeyboardEvents.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import React from 'react';

interface UseDocSearchKeyboardEventsProps {
isOpen: boolean;
onOpen(): void;
onClose(): void;
onInput?(event: KeyboardEvent): void;
searchButtonRef?: React.RefObject<HTMLButtonElement>;
}

export function useDocSearchKeyboardEvents({
isOpen,
onOpen,
onClose,
onInput,
searchButtonRef,
}) {
}: UseDocSearchKeyboardEventsProps) {
React.useEffect(() => {
function onKeyDown(event: KeyboardEvent) {
if (
Expand All @@ -27,10 +36,11 @@ export function useDocSearchKeyboardEvents({

if (
searchButtonRef &&
searchButtonRef.current === document.activeElement
searchButtonRef.current === document.activeElement &&
onInput
) {
if (/[a-zA-Z0-9]/.test(String.fromCharCode(event.keyCode))) {
onOpen();
onInput(event);
}
}
}
Expand All @@ -40,5 +50,5 @@ export function useDocSearchKeyboardEvents({
return () => {
window.removeEventListener('keydown', onKeyDown);
};
}, [isOpen, onOpen, onClose, searchButtonRef]);
}, [isOpen, onOpen, onClose, onInput, searchButtonRef]);
}
6 changes: 6 additions & 0 deletions packages/website/docs/DocSearchModal.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ The text that appears in the search box input when there is no query.
[Search parameters](https://www.algolia.com/doc/api-reference/search-api-parameters/) to forward to Algolia.

### `initialQuery`

> `string`
The initial query when the modal opens.

### `onClose`

> `() => void`
Expand Down
17 changes: 15 additions & 2 deletions packages/website/src/theme/SearchBar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ function transformSearchClient(searchClient) {

function DocSearch(props) {
const history = useHistory();
const [isOpen, setIsOpen] = useState(false);
const searchButtonRef = useRef(null);
const [isOpen, setIsOpen] = useState(false);
const [initialQuery, setInitialQuery] = useState(null);

const importDocSearchModalIfNeeded = useCallback(() => {
if (DocSearchModal) {
Expand All @@ -75,7 +76,18 @@ function DocSearch(props) {
setIsOpen(false);
}, [setIsOpen]);

useDocSearchKeyboardEvents({ isOpen, onOpen, onClose, searchButtonRef });
function onInput(event) {
setIsOpen(true);
setInitialQuery(event.key);
}

useDocSearchKeyboardEvents({
isOpen,
onOpen,
onClose,
onInput,
searchButtonRef,
});

return (
<>
Expand All @@ -102,6 +114,7 @@ function DocSearch(props) {
createPortal(
<DocSearchModal
initialScrollY={window.scrollY}
initialQuery={initialQuery}
onClose={onClose}
navigator={{
navigate({ suggestionUrl }) {
Expand Down

0 comments on commit 11aa27b

Please sign in to comment.