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 typing query when search button is focused (#54
Browse files Browse the repository at this point in the history
)
  • Loading branch information
francoischalifour authored Jul 7, 2020
1 parent ce2982d commit dcf2247
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
16 changes: 11 additions & 5 deletions packages/docsearch-react/src/DocSearchButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ function isAppleDevice() {
return /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);
}

export function DocSearchButton(
props: React.DetailedHTMLProps<
export const DocSearchButton = React.forwardRef<
HTMLButtonElement,
React.DetailedHTMLProps<
React.ButtonHTMLAttributes<HTMLButtonElement>,
HTMLButtonElement
>
) {
>((props, ref) => {
const [key, setKey] = useState(() =>
isAppleDevice() ? ACTION_KEY_APPLE : ACTION_KEY_DEFAULT
);
Expand All @@ -31,7 +32,12 @@ export function DocSearchButton(
}, []);

return (
<button type="button" className="DocSearch-SearchButton" {...props}>
<button
type="button"
className="DocSearch-SearchButton"
{...props}
ref={ref}
>
<SearchIcon />
<span className="DocSearch-SearchButton-Placeholder">Search</span>

Expand All @@ -41,4 +47,4 @@ export function DocSearchButton(
<span className="DocSearch-SearchButton-Key">K</span>
</button>
);
}
});
15 changes: 13 additions & 2 deletions packages/docsearch-react/src/useDocSearchKeyboardEvents.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import React from 'react';

export function useDocSearchKeyboardEvents({ isOpen, onOpen, onClose }) {
export function useDocSearchKeyboardEvents({
isOpen,
onOpen,
onClose,
searchButtonRef,
}) {
React.useEffect(() => {
function onKeyDown(event: KeyboardEvent) {
if (
Expand All @@ -19,12 +24,18 @@ export function useDocSearchKeyboardEvents({ isOpen, onOpen, onClose }) {
onOpen();
}
}

if (searchButtonRef.current === document.activeElement) {
if (/[a-zA-Z0-9]/.test(String.fromCharCode(event.keyCode))) {
onOpen();
}
}
}

window.addEventListener('keydown', onKeyDown);

return () => {
window.removeEventListener('keydown', onKeyDown);
};
}, [isOpen, onOpen, onClose]);
}, [isOpen, onOpen, onClose, searchButtonRef]);
}
6 changes: 4 additions & 2 deletions packages/website/src/theme/SearchBar/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable import/no-unresolved */

import React, { useState, useCallback } from 'react';
import React, { useState, useCallback, useRef } from 'react';
import { createPortal } from 'react-dom';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import { useHistory } from '@docusaurus/router';
Expand Down Expand Up @@ -40,6 +40,7 @@ function transformItems(items) {
function DocSearch({ indexName, appId, apiKey, searchParameters }) {
const history = useHistory();
const [isOpen, setIsOpen] = useState(false);
const searchButtonRef = useRef(null);

const importDocSearchModalIfNeeded = useCallback(() => {
if (DocSearchModal) {
Expand All @@ -64,7 +65,7 @@ function DocSearch({ indexName, appId, apiKey, searchParameters }) {
setIsOpen(false);
}, [setIsOpen]);

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

return (
<>
Expand All @@ -84,6 +85,7 @@ function DocSearch({ indexName, appId, apiKey, searchParameters }) {
onFocus={importDocSearchModalIfNeeded}
onMouseOver={importDocSearchModalIfNeeded}
onClick={onOpen}
ref={searchButtonRef}
/>

{isOpen &&
Expand Down

0 comments on commit dcf2247

Please sign in to comment.