Skip to content

Instantly share code, notes, and snippets.

@Kornil
Created December 30, 2018 19:13
Show Gist options
  • Save Kornil/fd45303df5e487a6a6534da91bd3f21e to your computer and use it in GitHub Desktop.
Save Kornil/fd45303df5e487a6a6534da91bd3f21e to your computer and use it in GitHub Desktop.
class Modal extends Component {
private modalRef = createRef;
closeModal = (e) => {
const { modalProps } = this.props;
const node = this.modalRef.current;
// ignore if click is inside the modal
if (node && node.contains(e.target)) {
return;
}
modalProps.hideModal();
};
componentDidMount() {
document.addEventListener("mousedown", this.closeModal, false);
}
componentWillUnmount() {
document.removeEventListener("mousedown", this.closeModal, false);
}
render() {
const { modalProps, title, children } = this.props;
return modalProps.show ? (
<StyledModalOverlay>
<StyledModal ref={this.modalRef}>
<header>{title}</header>
{children}
</StyledModal>
</StyledModalOverlay>
) : null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment