Skip to content

Commit

Permalink
refactor(docsearch): manage input focus with effects
Browse files Browse the repository at this point in the history
The `autoFocus` prop is not standardize when coming from a component that gets mounted after the first page load.

Although this works with React, it does with Preact. This makes it work with both libraries.
  • Loading branch information
francoischalifour committed Jul 7, 2020
1 parent 929b1c6 commit 2a00d3f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/DocSearchModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ export function DocSearchModal({
>({
id: 'docsearch',
defaultHighlightedIndex: 0,
autoFocus: true,
placeholder,
openOnFocus: true,
initialState: {
Expand Down
7 changes: 6 additions & 1 deletion src/SearchBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ export function SearchBox(props: SearchBoxProps) {
inputElement: props.inputRef.current,
});

React.useEffect(() => {
if (props.autoFocus && props.inputRef.current) {
props.inputRef.current.focus();
}
}, [props.autoFocus, props.inputRef]);

return (
<>
<form
Expand All @@ -50,7 +56,6 @@ export function SearchBox(props: SearchBoxProps) {
className="DocSearch-Input"
ref={props.inputRef}
{...props.getInputProps({
autoFocus: props.autoFocus,
inputElement: props.inputRef.current!,
type: 'search',
maxLength: MAX_QUERY_SIZE,
Expand Down

0 comments on commit 2a00d3f

Please sign in to comment.