import React from "react" import { Modal as AntdModal } from "antd" import classnames from "classnames" import { Icons } from "components/Icons" import useLayoutInterface from "hooks/useLayoutInterface" import { DOMWindow } from "components/RenderWindow" import "./index.less" class Modal extends React.Component { state = { visible: false, } contentRef = React.createRef() escTimeout = null componentDidMount() { setTimeout(() => { this.setState({ visible: true, }) }, 10) document.addEventListener("keydown", this.handleEsc, false) } componentWillUnmount() { document.removeEventListener("keydown", this.handleEsc, false) } close = () => { this.setState({ visible: false, }) setTimeout(() => { if (typeof this.props.onClose === "function") { this.props.onClose() } }, 250) } handleEsc = (e) => { if (e.key === "Escape") { if (this.escTimeout !== null) { clearTimeout(this.escTimeout) return this.close() } this.escTimeout = setTimeout(() => { this.escTimeout = null }, 250) } } handleClickOutside = (e) => { if (this.contentRef.current && !this.contentRef.current.contains(e.target)) { if (this.props.confirmOnOutsideClick) { return AntdModal.confirm({ title: this.props.confirmOnClickTitle ?? "Are you sure?", content: this.props.confirmOnClickContent ?? "Are you sure you want to close this window?", onOk: () => { this.close() } }) } return this.close() } } render() { return