feat(docsearch): support keyboard on focus on default integration
This commit is contained in:
parent
2a00d3f3c0
commit
92e2565d45
2 changed files with 7 additions and 3 deletions
|
|
@ -29,6 +29,7 @@ export interface DocSearchProps
|
|||
|
||||
export function DocSearch(props: DocSearchProps) {
|
||||
const [isOpen, setIsOpen] = React.useState(false);
|
||||
const searchButtonRef = React.useRef<HTMLButtonElement>(null);
|
||||
|
||||
const onOpen = React.useCallback(() => {
|
||||
setIsOpen(true);
|
||||
|
|
@ -38,11 +39,11 @@ export function DocSearch(props: DocSearchProps) {
|
|||
setIsOpen(false);
|
||||
}, [setIsOpen]);
|
||||
|
||||
useDocSearchKeyboardEvents({ isOpen, onOpen, onClose });
|
||||
useDocSearchKeyboardEvents({ isOpen, onOpen, onClose, searchButtonRef });
|
||||
|
||||
return (
|
||||
<>
|
||||
<DocSearchButton onClick={onOpen} />
|
||||
<DocSearchButton onClick={onOpen} ref={searchButtonRef} />
|
||||
|
||||
{isOpen &&
|
||||
createPortal(
|
||||
|
|
|
|||
|
|
@ -25,7 +25,10 @@ export function useDocSearchKeyboardEvents({
|
|||
}
|
||||
}
|
||||
|
||||
if (searchButtonRef.current === document.activeElement) {
|
||||
if (
|
||||
searchButtonRef &&
|
||||
searchButtonRef.current === document.activeElement
|
||||
) {
|
||||
if (/[a-zA-Z0-9]/.test(String.fromCharCode(event.keyCode))) {
|
||||
onOpen();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue