import React, { Component } from "react"; import ReactDOM from "react-dom"; import styles from "./style.less"; import preventBgScroll from "./preventBgScroll"; const classnames = require("classnames"); interface OstMaskState { _preventBgScroll: any; } export default class OstMask extends Component { componentActivated: boolean; container: any; constructor(props: any) { super(props); this.state = { _preventBgScroll: preventBgScroll(), }; this.componentActivated = false; } showUpdata = () => { if (this.props.show) { this.container && this.container.removeEventListener( "animationend", this.removeContainer ); this.state._preventBgScroll.afterOpen(); } else { this.container && this.container.addEventListener("animationend", this.removeContainer); this.state._preventBgScroll.beforeClose(); } }; componentDidMount() { this.showUpdata(); } componentDidUpdate(prevProps: any) { if (prevProps.show !== this.props.show) { this.showUpdata(); } } removeContainer = () => { this.container && this.container.parentNode.removeChild(this.container); this.componentActivated = false; this.container = null; }; getContainer = () => { if (!this.container) { const container = document.createElement("div"); const containerId = `ost_mask_container_${new Date().getTime()}`; container.setAttribute("id", containerId); document.body.appendChild(container); this.container = container; } return this.container; }; getComponent = () => { const { show, onClick, type } = this.props; return (
{!type && (
{this.props.children}
)} {
onClick && onClick(e)} >
}
); }; render() { const { show } = this.props; if (show) this.componentActivated = true; if (!this.componentActivated) return null; return ReactDOM.createPortal(this.getComponent(), this.getContainer()); } }