1 | import { DOM as E } from "react"
|
2 | import ReactDOM from "react-dom"
|
3 | import { noop } from "./utils"
|
4 | import { isClient } from "./platform"
|
5 |
|
6 |
|
7 |
|
8 | const createElement = (x) =>
|
9 | isClient ? document.createElement(x) : noop
|
10 |
|
11 | const bodyAppendElement = (x) =>
|
12 | isClient ? document.body.appendChild(x) : noop
|
13 |
|
14 | const bodyRemoveElement = (x) =>
|
15 | isClient ? document.body.removeChild(x) : noop
|
16 |
|
17 |
|
18 |
|
19 | const ReactLayerMixin = () => ({
|
20 | componentWillMount () {
|
21 | this.targetBounds = null
|
22 |
|
23 | this.layerContainerNode = createElement("div")
|
24 | },
|
25 | componentDidMount () {
|
26 |
|
27 | bodyAppendElement(this.layerContainerNode)
|
28 | this._layerRender()
|
29 | },
|
30 | componentDidUpdate () {
|
31 | this._layerRender()
|
32 | },
|
33 | componentWillUnmount () {
|
34 | this._layerUnrender()
|
35 |
|
36 | bodyRemoveElement(this.layerContainerNode)
|
37 | },
|
38 | _layerRender () {
|
39 | const layerReactEl = this.renderLayer()
|
40 | if (!layerReactEl) {
|
41 | this.layerReactComponent = null
|
42 | ReactDOM.unstable_renderSubtreeIntoContainer(this, E.noscript(), this.layerContainerNode)
|
43 | } else {
|
44 | this.layerReactComponent = ReactDOM.unstable_renderSubtreeIntoContainer(this, layerReactEl, this.layerContainerNode)
|
45 | }
|
46 | },
|
47 | _layerUnrender () {
|
48 | if (this.layerWillUnmount) this.layerWillUnmount(this.layerContainerNode)
|
49 | ReactDOM.unmountComponentAtNode(this.layerContainerNode)
|
50 | },
|
51 |
|
52 |
|
53 |
|
54 | })
|
55 |
|
56 |
|
57 |
|
58 | export default ReactLayerMixin
|