markdown 单击模态外部时关闭
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 单击模态外部时关闭相关的知识,希望对你有一定的参考价值。
side note: Close when click outside of modal - [ref](https://medium.com/@francesco.agnoletto/how-to-use-react-context-to-create-a-modal-d8a387e09c28)
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;
}
}
以上是关于markdown 单击模态外部时关闭的主要内容,如果未能解决你的问题,请参考以下文章