Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable the PairSelector menu to close when clicking outside the menu #537

Merged
merged 1 commit into from
Aug 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading