Dwinzo_dev/app/src/utils/outerClick.ts

30 lines
867 B
TypeScript
Raw Normal View History

2025-03-25 12:04:20 +00:00
import React from "react";
interface OuterClickProps {
contextClassName: string;
setMenuVisible: React.Dispatch<React.SetStateAction<boolean>>;
}
export default function OuterClick({
contextClassName,
setMenuVisible,
}: OuterClickProps) {
const handleClick = (event: MouseEvent) => {
const targets = event.target as HTMLElement;
// Check if the click is outside the selectable-dropdown-wrapper
if (!targets.closest(`.${contextClassName}`)) {
setMenuVisible(false); // Close the menu by updating the state
}
};
// Add event listener on mount and remove it on unmount
React.useEffect(() => {
document.addEventListener("click", handleClick);
return () => {
document.removeEventListener("click", handleClick);
};
}, []);
return null; // This component doesn't render anything
}