UNPKG

1.54 kBJavaScriptView Raw
1import { DOM as E } from "react"
2import ReactDOM from "react-dom"
3import { noop } from "./utils"
4import { isClient } from "./platform"
5
6
7
8const createElement = (x) =>
9 isClient ? document.createElement(x) : noop
10
11const bodyAppendElement = (x) =>
12 isClient ? document.body.appendChild(x) : noop
13
14const bodyRemoveElement = (x) =>
15 isClient ? document.body.removeChild(x) : noop
16
17
18
19const ReactLayerMixin = () => ({
20 componentWillMount () {
21 this.targetBounds = null
22 /* Create a DOM node for mounting the React Layer. */
23 this.layerContainerNode = createElement("div")
24 },
25 componentDidMount () {
26 /* Mount the mount. */
27 bodyAppendElement(this.layerContainerNode)
28 this._layerRender()
29 },
30 componentDidUpdate () {
31 this._layerRender()
32 },
33 componentWillUnmount () {
34 this._layerUnrender()
35 /* Unmount the mount. */
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 // renderLayer() {
52 // Must be implemented by consumer.
53 // }
54})
55
56
57
58export default ReactLayerMixin