I’d like to close the modal on click outside the modal.
But with the following code the modal closes also when I click on the modal.
It should not close when I click on the modal component.
const useOutsideAlerter = (ref) => {
useEffect(() => {
function handleClickOutside(event) {
if (ref.current && !ref.current.contains(event.target)) {
alert("You clicked outside of me!");
}
}
document.addEventListener("mousedown", handleClickOutside);
return () => {
document.removeEventListener("mousedown", handleClickOutside);
};
}, [ref]);
};
const NavModal = (props) => {
const modalOpen = useNavModalStore((state) => state.open);
const setOpen = useNavModalStore((state) => state.setOpen);
const wrapperRef = useRef(null);
useOutsideAlerter(wrapperRef);
return (
<div>
<NavIconButton onClick={setOpen}>
<Icon src={props.buttonIcon} />
</NavIconButton>
<div>
{modalOpen && (
<NavModalContent ref={wrapperRef} width={props.modalWidth}>
{props.children}
</NavModalContent>
)}
</div>
</div>
);
};
2
Answers
Why do you want to handle the click outside action in a separate function?
Have you considered this solution:
refactor like this: