Last active
December 30, 2018 11:29
-
-
Save Kornil/e65184cf0c840185b8b97ca932a4891d 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 { | |
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