Created
December 30, 2018 19:13
-
-
Save Kornil/fd45303df5e487a6a6534da91bd3f21e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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