-
Notifications
You must be signed in to change notification settings - Fork 395
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(docsearch): add
DocSearch
component
- Loading branch information
1 parent
adadacd
commit 65a98b0
Showing
3 changed files
with
59 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import React from 'react'; | ||
import { createPortal } from 'react-dom'; | ||
import { PublicAutocompleteOptions } from '@francoischalifour/autocomplete-core'; | ||
|
||
import { DocSearchHit, InternalDocSearchHit } from './types'; | ||
import { DocSearchButton } from './DocSearchButton'; | ||
import { DocSearchModal } from './DocSearchModal'; | ||
import { useDocSearchKeyboardEvents } from './useDocSearchKeyboardEvents'; | ||
|
||
export interface DocSearchProps | ||
extends Pick<PublicAutocompleteOptions<InternalDocSearchHit>, 'navigator'> { | ||
appId?: string; | ||
apiKey: string; | ||
indexName: string; | ||
placeholder?: string; | ||
searchParameters?: any; | ||
transformItems?(items: DocSearchHit[]): DocSearchHit[]; | ||
hitComponent?(props: { | ||
hit: DocSearchHit; | ||
children: React.ReactNode; | ||
}): JSX.Element; | ||
} | ||
|
||
export function DocSearch(props: DocSearchProps) { | ||
const [isOpen, setIsOpen] = React.useState(false); | ||
|
||
const onOpen = React.useCallback( | ||
function onOpen() { | ||
setIsOpen(true); | ||
}, | ||
[setIsOpen] | ||
); | ||
|
||
const onClose = React.useCallback( | ||
function onClose() { | ||
setIsOpen(false); | ||
}, | ||
[setIsOpen] | ||
); | ||
|
||
useDocSearchKeyboardEvents({ isOpen, onOpen, onClose }); | ||
|
||
return ( | ||
<> | ||
<DocSearchButton onClick={onOpen} /> | ||
|
||
{isOpen && | ||
createPortal( | ||
<DocSearchModal {...props} onClose={onClose} />, | ||
document.body | ||
)} | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './DocSearchModal'; | ||
export * from './DocSearch'; | ||
export * from './DocSearchButton'; | ||
export * from './DocSearchModal'; | ||
export * from './useDocSearchKeyboardEvents'; |