index.js 1.16 KB
Newer Older
zhanghaozhe committed
1 2 3 4
import React, { Component } from "react"
import ReactDOM from "react-dom"
import classnames from "classnames"
import "./index.scss"
FE committed
5

zhanghaozhe committed
6 7
const Root = document.querySelector("body")
const events = ["touchmove", "mousewheel"]
FE committed
8 9 10

class Mask extends Component {
  constructor(props) {
zhanghaozhe committed
11 12 13
    super(props)
    if (!this.el) {
      this.el = document.createElement("div")
FE committed
14 15 16 17
    }
  }

  componentDidMount() {
zhanghaozhe committed
18
    events.forEach((item) => {
FE committed
19
      this.el.addEventListener(item, this.preventEvent, {
zhanghaozhe committed
20
        passive: false,
FE committed
21 22
      })
    })
zhanghaozhe committed
23
    Root.appendChild(this.el)
FE committed
24 25 26
  }

  componentWillUnmount() {
zhanghaozhe committed
27
    Root.removeChild(this.el)
FE committed
28 29
  }

zhanghaozhe committed
30 31
  preventEvent = (e) => {
    e.preventDefault()
FE committed
32 33 34
  }

  render() {
zhanghaozhe committed
35 36
    const { visible, handleToHide, className } = this.props
    if (visible) {
FE committed
37
      return ReactDOM.createPortal(
zhanghaozhe committed
38 39 40 41 42 43
        <div className="mask">
          <div className={classnames("mask-content", className)}>
            {this.props.children}
          </div>
          <div className="mask-footer">
            <i className="mask-button__close" onClick={handleToHide}></i>
FE committed
44
          </div>
zhanghaozhe committed
45
        </div>,
FE committed
46
        this.el
zhanghaozhe committed
47 48 49
      )
    } else {
      return null
FE committed
50 51 52 53
    }
  }
}

zhanghaozhe committed
54
export default Mask