Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add keyboard shortcut ("/" key) to focus search input #380

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/doc-explorer/DocExplorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export default class DocExplorer extends Component<
<div className="type-doc" key={navStack.length}>
{renderNavigation(previousNav, currentNav)}
<SearchBox
placeholder={`Search ${name}...`}
placeholder={`Search ${name}... [ / ]`}
value={currentNav.searchValue}
onSearch={this.handleSearch}
/>
Expand Down
11 changes: 10 additions & 1 deletion src/components/utils/SearchBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,39 @@ import Box from '@mui/material/Box';
import IconButton from '@mui/material/IconButton';
import Input from '@mui/material/Input';
import InputAdornment from '@mui/material/InputAdornment';
import { useEffect, useState } from 'react';
import { useEffect, useRef, useState } from 'react';

import { useFocusInputWithHotkey } from '../../utils/useFocusInputWithHotkey';

interface SearchBoxProps {
placeholder: string;
value: string | null;
onSearch: (value: string) => void;
}

const SEARCH_FOCUS_KEY = '/';

export default function SearchBox(props: SearchBoxProps) {
const [value, setValue] = useState(props.value ?? '');
const { placeholder, onSearch } = props;

const inputRef = useRef<HTMLInputElement>();

useEffect(() => {
const timeout = setTimeout(() => onSearch(value), 200);
return () => clearTimeout(timeout);
}, [onSearch, value]);

useFocusInputWithHotkey(inputRef.current, SEARCH_FOCUS_KEY);

return (
<Box paddingLeft={2} paddingRight={2}>
<Input
fullWidth
placeholder={placeholder}
value={value}
onChange={(event) => setValue(event.target.value)}
inputRef={inputRef}
type="text"
className="search-box"
endAdornment={
Expand Down
37 changes: 37 additions & 0 deletions src/utils/useFocusInputWithHotkey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useCallback } from 'react';

import { useOnKeydown } from './useOnKeydown';

export function useFocusInputWithHotkey(
target: HTMLInputElement | undefined,
key: string,
) {
const keydownHandler = useCallback(
(keyboardEvent: KeyboardEvent) => {
if (target && !isActiveElementTypable()) {
target.focus();
keyboardEvent.preventDefault();
}
},
[target],
);

useOnKeydown(key, keydownHandler);
}

function isActiveElementTypable() {
const activeElement = document.activeElement;

const typableElements = [
HTMLInputElement,
HTMLTextAreaElement,
HTMLSelectElement,
HTMLDataListElement,
HTMLFieldSetElement,
];

return (
typableElements.some((type) => activeElement instanceof type) ||
activeElement?.hasAttribute('contentEditable')
);
}
23 changes: 23 additions & 0 deletions src/utils/useOnKeydown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useCallback, useEffect } from 'react';

export function useOnKeydown(
key: string,
callback: (keyboardEvent: KeyboardEvent) => void,
) {
const keydownHandler = useCallback(
(keyboardEvent: KeyboardEvent) => {
if (keyboardEvent.key === key) {
callback(keyboardEvent);
}
},
[key, callback],
);

useEffect(() => {
window.addEventListener('keydown', keydownHandler);

return () => {
window.removeEventListener('keydown', keydownHandler);
};
}, [keydownHandler]);
}
Loading