Skip to content

Commit 5b3b4e0

Browse files
authored
Fix closing search modal on Escape key press (#2637)
1 parent 8d039d1 commit 5b3b4e0

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

packages/gitbook/src/components/Search/SearchModal.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ export function SearchModal(props: SearchModalProps) {
5555
};
5656
}, [isSearchOpened]);
5757

58-
// if (state === null) {
59-
// return null;
60-
// }
61-
6258
const onChangeQuery = (newQuery: SearchState) => {
6359
setSearchState(newQuery);
6460
};
@@ -170,10 +166,21 @@ function SearchModalBody(
170166
inputRef.current?.focus();
171167
}, []);
172168

169+
React.useEffect(() => {
170+
const handleKeyDown = (event: KeyboardEvent) => {
171+
if (event.key === 'Escape') {
172+
onClose();
173+
}
174+
};
175+
document.addEventListener('keydown', handleKeyDown);
176+
177+
return () => {
178+
document.removeEventListener('keydown', handleKeyDown);
179+
};
180+
}, [onClose]);
181+
173182
const onKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
174-
if (event.key === 'Escape') {
175-
onClose();
176-
} else if (event.key === 'ArrowUp') {
183+
if (event.key === 'ArrowUp') {
177184
event.preventDefault();
178185
resultsRef.current?.moveUp();
179186
} else if (event.key === 'ArrowDown') {

0 commit comments

Comments
 (0)