diff --git a/src/components/quicklaunch.jsx b/src/components/quicklaunch.jsx index 4a3f345e8..d40e50d0a 100644 --- a/src/components/quicklaunch.jsx +++ b/src/components/quicklaunch.jsx @@ -1,5 +1,5 @@ import { useTranslation } from "react-i18next"; -import { useEffect, useState, useRef } from "react"; +import { useEffect, useState, useRef, useCallback } from "react"; import classNames from "classnames"; import { resolveIcon } from "./services/item"; @@ -17,10 +17,12 @@ export default function QuickLaunch({servicesAndBookmarks, searchString, setSear window.open(result.href, '_blank'); } - function resetAndClose() { - setSearchString(""); + const closeAndReset = useCallback(() => { close(false); - } + setTimeout(() => { + setSearchString(""); + }, 200); // delay a little for animations + }, [close, setSearchString]); function handleSearchChange(event) { setSearchString(event.target.value.toLowerCase()) @@ -28,9 +30,9 @@ export default function QuickLaunch({servicesAndBookmarks, searchString, setSear function handleSearchKeyDown(event) { if (event.key === "Escape") { - resetAndClose(); + closeAndReset(); } else if (event.key === "Enter" && results.length) { - resetAndClose(); + closeAndReset(); openCurrentItem(); } else if (event.key === "ArrowDown" && results[currentItemIndex + 1]) { setCurrentItemIndex(currentItemIndex + 1); @@ -46,7 +48,7 @@ export default function QuickLaunch({servicesAndBookmarks, searchString, setSear } function handleItemClick() { - resetAndClose(); + closeAndReset(); openCurrentItem(); } @@ -59,20 +61,27 @@ export default function QuickLaunch({servicesAndBookmarks, searchString, setSear setCurrentItemIndex(0); } } - }, [searchString, servicesAndBookmarks]) + }, [searchString, servicesAndBookmarks]); const [hidden, setHidden] = useState(true); useEffect(() => { + function handleBackdropClick(event) { + if (event.target?.tagName === "DIV") closeAndReset(); + } + if (isOpen) { searchField.current.focus(); + document.body.addEventListener('click', handleBackdropClick); setHidden(false); } else { + document.body.removeEventListener('click', handleBackdropClick); setTimeout(() => { setHidden(true); }, 300); // disable on close } - }, [isOpen]) + + }, [isOpen, closeAndReset]); return (
-
+ 0 && "rounded-t-md", results.length === 0 && "rounded-md", "w-full p-4 m-0 border-0 border-b border-slate-700 focus:border-slate-700 focus:outline-0 focus:ring-0 text-sm md:text-xl text-theme-700 dark:text-theme-200 bg-theme-60 dark:bg-theme-800" - )} type="text" ref={searchField} value={searchString} onChange={handleSearchChange} onKeyDown={handleSearchKeyDown} /> + )} type="text" autoCorrect="false" ref={searchField} value={searchString} onChange={handleSearchChange} onKeyDown={handleSearchKeyDown} /> {results.length > 0 &&
    {results.map((r, i) => (
  • @@ -112,7 +121,7 @@ export default function QuickLaunch({servicesAndBookmarks, searchString, setSear
  • ))}
} -
+
diff --git a/src/pages/index.jsx b/src/pages/index.jsx index 400d7eb5a..bf64cc595 100644 --- a/src/pages/index.jsx +++ b/src/pages/index.jsx @@ -209,7 +209,7 @@ function Home({ initialSettings }) { document.addEventListener('keydown', handleKeyDown); return function cleanup() { - document.removeEventListener('keydown', handleKeyDown); + document.removeEventListener('keydown', handleKeyDown); } })