this repo has no description
at main 82 lines 1.8 kB view raw
1import './search-command.css'; 2 3import { memo } from 'preact/compat'; 4import { useRef, useState } from 'preact/hooks'; 5import { useHotkeys } from 'react-hotkeys-hook'; 6 7import SearchForm from './search-form'; 8 9export default memo(function SearchCommand({ onClose = () => {} }) { 10 const [showSearch, setShowSearch] = useState(false); 11 const searchFormRef = useRef(null); 12 13 useHotkeys( 14 ['Slash', '/'], 15 (e) => { 16 setShowSearch(true); 17 setTimeout(() => { 18 searchFormRef.current?.focus?.(); 19 searchFormRef.current?.select?.(); 20 }, 0); 21 }, 22 { 23 useKey: true, 24 preventDefault: true, 25 ignoreEventWhen: (e) => { 26 const isSearchPage = /\/search/.test(location.hash); 27 const hasModal = !!document.querySelector('#modal-container > *'); 28 return ( 29 isSearchPage || 30 hasModal || 31 e.metaKey || 32 e.ctrlKey || 33 e.altKey || 34 e.shiftKey 35 ); 36 }, 37 }, 38 ); 39 40 const closeSearch = () => { 41 setShowSearch(false); 42 onClose(); 43 }; 44 45 useHotkeys( 46 'esc', 47 (e) => { 48 searchFormRef.current?.blur?.(); 49 closeSearch(); 50 }, 51 { 52 enabled: showSearch, 53 enableOnFormTags: true, 54 preventDefault: true, 55 useKey: true, 56 ignoreEventWhen: (e) => e.metaKey || e.ctrlKey || e.altKey || e.shiftKey, 57 }, 58 ); 59 60 const hidden = !showSearch; 61 62 return ( 63 <div 64 id="search-command-container" 65 hidden={hidden} 66 onClick={(e) => { 67 console.log(e); 68 if (e.target === e.currentTarget) { 69 closeSearch(); 70 } 71 }} 72 > 73 <SearchForm 74 ref={searchFormRef} 75 hidden={hidden} 76 onSubmit={() => { 77 closeSearch(); 78 }} 79 /> 80 </div> 81 ); 82});