Skip to content

Commit

Permalink
feat(docsearch): support keyboard on focus on default integration
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Jul 7, 2020
1 parent 2a00d3f commit 92e2565
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/DocSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(
Expand Down
5 changes: 4 additions & 1 deletion src/useDocSearchKeyboardEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit 92e2565

Please sign in to comment.