Answer the question
In order to leave comments, you need to log in
How to close a popup window in react?
Good afternoon. The application has the ability to edit menu items. Each item has a button, which, when clicked, will display a pop-up menu. I plan to implement this using css classes. How to implement the collapse of this element when clicking on any other area?
Answer the question
In order to leave comments, you need to log in
Watch for clicks outside the root node :
function Modal(props) {
const root = useRef();
useEffect(() => {
const onClick = e => root.current.contains(e.target) || закрытьОкно();
document.addEventListener('click', onClick);
return () => document.removeEventListener('click', onClick);
}, []);
return (
<div ref={root}>
...
</div>
);
}
function Modal(props) {
return (
<div
className="modal-overlay"
onClick={e => (e.currentTarget === e.target) && закрытьОкно()}
>
...
</div>
);
}
.modal-overlay {
position: absolute;
left: 0;
top: 0;
width: 100vw;
height: 100vh;
background: rgba(0, 0, 0, 0.5);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question