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 (