Skip to content

Instantly share code, notes, and snippets.

@Kornil
Last active December 30, 2018 11:29
Show Gist options
  • Save Kornil/e65184cf0c840185b8b97ca932a4891d to your computer and use it in GitHub Desktop.
Save Kornil/e65184cf0c840185b8b97ca932a4891d to your computer and use it in GitHub Desktop.
class Modal extends Component {
modalRef = createRef();
state = {
show: false
}
closeModal = (e) => {
const node = this.modalRef.current;
if (node && node.contains(e.target)) {
return;
}
this.setState({
show: false
})
};
componentDidMount() {
document.addEventListener("mousedown", this.closeModal, false);
}
componentWillUnmount() {
document.removeEventListener("mousedown", this.closeModal, false);
}
render() {
const { title, children } = this.props;
const { show } = this.state;
return 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