Skip to content

A local-first personal finance app

Added logic to prevent autocomplete from flickering on alt tab (#7637)

Alec Bakholdine479c848985c
diff --git a/packages/desktop-client/src/components/autocomplete/Autocomplete.tsx b/packages/desktop-client/src/components/autocomplete/Autocomplete.tsx
index e6bc065886..6f36fa7ab6 100644
--- a/packages/desktop-client/src/components/autocomplete/Autocomplete.tsx
+++ b/packages/desktop-client/src/components/autocomplete/Autocomplete.tsx
@@ -251,10 +251,31 @@ function SingleAutocomplete<T extends AutocompleteItem>({
);
const [highlightedIndex, setHighlightedIndex] = useState(null);
const [isOpen, setIsOpen] = useState(embedded);
- const open = () => setIsOpen(true);
+ const [allowOpening, setAllowOpening] = useState(true);
+ useEffect(() => {
+ // without this logic, leaving the window and returning will always reopen the
+ // dropdown, which makes a poor user experience, especially when clicking into the window.
+ // You will find that the dropdown is "invisible" but reappears instantly, resulting in clicking
+ // on the "ghost" dropdown's first option
+ const setAllowOpenChange = () => setTimeout(() => setAllowOpening(true));
+ window.addEventListener('focus', setAllowOpenChange);
+ const setDontAllowOpenChange = () => setAllowOpening(false);
+ window.addEventListener('blur', setDontAllowOpenChange);
+ return () => {
+ window.removeEventListener('focus', setAllowOpenChange);
+ window.removeEventListener('blur', setDontAllowOpenChange);
+ };
+ }, []);
+ const open = () => {
+ if (allowOpening) {
+ setIsOpen(true);
+ }
+ };
const close = () => {
- setIsOpen(false);
- onClose?.();
+ if (document.hasFocus()) {
+ setIsOpen(false);
+ onClose?.();
+ }
};
const triggerRef = useRef(null);
diff --git a/upcoming-release-notes/7637.md b/upcoming-release-notes/7637.md
new file mode 100644
index 0000000000..185704c1c7
--- /dev/null
+++ b/upcoming-release-notes/7637.md
@@ -0,0 +1,6 @@
+---
+category: Bugfixes
+authors: [alecbakholdin]
+---
+
+Added logic to improve dropdown behavior when switching window focus