From 065ee99f741fabe2a7e03cc760d9c6772c3a1796 Mon Sep 17 00:00:00 2001 From: Wuxh Date: Thu, 23 Feb 2023 10:11:23 +0800 Subject: [PATCH] fix: search modal unexpectedly activated by input-like elements (#1502) * chore: debug * fix: fix the bug that the search shortcut key causes the input box to be out of focus * Revert "chore: debug" This reverts commit 734cd24272551e9c40539af3fe7a134b1e2e54b9. --- src/client/theme-default/slots/SearchBar/index.tsx | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/client/theme-default/slots/SearchBar/index.tsx b/src/client/theme-default/slots/SearchBar/index.tsx index afc4428f79..423ec06b75 100644 --- a/src/client/theme-default/slots/SearchBar/index.tsx +++ b/src/client/theme-default/slots/SearchBar/index.tsx @@ -14,6 +14,11 @@ const isAppleDevice = /(mac|iphone|ipod|ipad)/i.test( typeof navigator !== 'undefined' ? navigator?.platform : '', ); +/** Determine if the element that triggered the event is an input element */ +const isInput = (target: HTMLElement) => + ['TEXTAREA', 'INPUT'].includes(target.tagName) || + target.contentEditable === 'true'; + const SearchBar: FC = () => { const [focusing, setFocusing] = useState(false); const inputRef = useRef(null); @@ -32,7 +37,7 @@ const SearchBar: FC = () => { const handler = (ev: KeyboardEvent) => { if ( ((isAppleDevice ? ev.metaKey : ev.ctrlKey) && ev.key === 'k') || - ev.key === '/' + (ev.key === '/' && !isInput(ev.target as HTMLElement)) ) { ev.preventDefault();