Skip to content

Commit

Permalink
Merge pull request #537 from DeXter-on-Radix/fork-SiegfriedBz--close-…
Browse files Browse the repository at this point in the history
…pair-selector-dropdown

Enable the PairSelector menu to close when clicking outside the menu
  • Loading branch information
dcts authored Aug 4, 2024
2 parents ed45039 + 4d41f0b commit 88916b0
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/app/components/PairSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export function PairSelector() {
const pairSelector = useAppSelector((state) => state.pairSelector);
const dispatch = useAppDispatch();

const menuRef = useRef<HTMLDivElement>(null);

const inputRef = useRef<HTMLInputElement>(null);
const [query, setQuery] = useState("");
const [isOpen, setIsOpen] = useState(false);
Expand Down Expand Up @@ -187,8 +189,26 @@ export function PairSelector() {
}
}, [isOpen, options, setFilteredOptions, setHighlightedIndex]);

const handleClickOutside = (event: MouseEvent) => {
if (menuRef.current && !menuRef.current.contains(event.target as Node)) {
setIsOpen(false);
}
};

useEffect(() => {
if (isOpen) {
document.addEventListener("click", handleClickOutside);
} else {
document.removeEventListener("click", handleClickOutside);
}
return () => {
document.removeEventListener("click", handleClickOutside);
};
}, [isOpen]);

return (
<div
ref={menuRef}
className={
"w-full h-full relative uppercase" + (isOpen ? " bg-base-100" : "")
}
Expand Down

0 comments on commit 88916b0

Please sign in to comment.