mirror of
https://github.com/cheeaun/phanpy.git
synced 2024-12-19 04:21:51 +03:00
effbe189e1
This reverts commit 9285a0ba9a
.
69 lines
1.5 KiB
JavaScript
69 lines
1.5 KiB
JavaScript
import './search-command.css';
|
|
|
|
import { memo } from 'preact/compat';
|
|
import { useRef, useState } from 'preact/hooks';
|
|
import { useHotkeys } from 'react-hotkeys-hook';
|
|
|
|
import SearchForm from './search-form';
|
|
|
|
export default memo(function SearchCommand({ onClose = () => {} }) {
|
|
const [showSearch, setShowSearch] = useState(false);
|
|
const searchFormRef = useRef(null);
|
|
|
|
useHotkeys(
|
|
['Slash', '/'],
|
|
(e) => {
|
|
setShowSearch(true);
|
|
setTimeout(() => {
|
|
searchFormRef.current?.focus?.();
|
|
searchFormRef.current?.select?.();
|
|
}, 0);
|
|
},
|
|
{
|
|
preventDefault: true,
|
|
ignoreEventWhen: (e) => {
|
|
const isSearchPage = /\/search/.test(location.hash);
|
|
const hasModal = !!document.querySelector('#modal-container > *');
|
|
return isSearchPage || hasModal;
|
|
},
|
|
},
|
|
);
|
|
|
|
const closeSearch = () => {
|
|
setShowSearch(false);
|
|
onClose();
|
|
};
|
|
|
|
useHotkeys(
|
|
'esc',
|
|
(e) => {
|
|
searchFormRef.current?.blur?.();
|
|
closeSearch();
|
|
},
|
|
{
|
|
enabled: showSearch,
|
|
enableOnFormTags: true,
|
|
preventDefault: true,
|
|
},
|
|
);
|
|
|
|
return (
|
|
<div
|
|
id="search-command-container"
|
|
hidden={!showSearch}
|
|
onClick={(e) => {
|
|
console.log(e);
|
|
if (e.target === e.currentTarget) {
|
|
closeSearch();
|
|
}
|
|
}}
|
|
>
|
|
<SearchForm
|
|
ref={searchFormRef}
|
|
onSubmit={() => {
|
|
closeSearch();
|
|
}}
|
|
/>
|
|
</div>
|
|
);
|
|
});
|