index.js 945 Bytes
Newer Older
zhanghaozhe committed
1 2
import React, { Component } from "react"
import ReactDom from "react-dom"
zhanghaozhe committed
3

zhanghaozhe committed
4
import "./overlay.scss"
zhanghaozhe committed
5 6 7

const root = document.body || document.documentElement

zhanghaozhe committed
8
const events = ["touchmove", "mousewheel"]
zhanghaozhe committed
9 10

class Overlay extends Component {
zhanghaozhe committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  constructor(props) {
    super(props)
    this.overlayBox = null
  }

  preventScroll = (e) => {
    e.preventDefault()
  }

  componentDidMount() {
    events.forEach((item) => {
      this.overlayBox.addEventListener(item, this.preventScroll, {
        passive: false,
      })
    })
  }

  componentWillUnmount() {
    events.forEach((item) => {
      this.overlayBox.removeEventListener(item, this.preventScroll)
    })
  }

  render() {
    return ReactDom.createPortal(
      <div
        className={"overlay"}
        ref={(el) => (this.overlayBox = el)}
        style={{ top: `${window.scrollY}px` }}
      >
        {this.props.children}
      </div>,
      root
    )
  }
zhanghaozhe committed
46 47
}

zhanghaozhe committed
48
export default Overlay