-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathreact-layer-mixin.js
63 lines (51 loc) · 1.73 KB
/
react-layer-mixin.js
1
2
3
4
5
6
7
8
9
10
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import E from "react-dom-factories"
import ReactDOM from "react-dom"
import { noop } from "./utils"
import { isClient } from "./platform"
const createElement = (x) =>
isClient ? document.createElement(x) : noop
const appendElement = (x, target) =>
isClient ? target.appendChild(x) : noop
const removeElement = (x, target) =>
isClient ? target.removeChild(x) : noop
const ReactLayerMixin = () => ({
componentWillMount () {
this.targetBounds = null
this.appendTarget = document.body
/* Create a DOM node for mounting the React Layer. */
this.layerContainerNode = createElement("div")
},
componentDidMount () {
if (this.props.appendTarget && document.querySelector(this.props.appendTarget)) {
this.appendTarget = document.querySelector(this.props.appendTarget)
}
/* Mount the mount. */
appendElement(this.layerContainerNode, this.appendTarget)
this._layerRender()
},
componentDidUpdate () {
this._layerRender()
},
componentWillUnmount () {
this._layerUnrender()
/* Unmount the mount. */
removeElement(this.layerContainerNode, this.appendTarget)
},
_layerRender () {
const layerReactEl = this.renderLayer()
if (!layerReactEl) {
this.layerReactComponent = null
ReactDOM.unstable_renderSubtreeIntoContainer(this, E.noscript(), this.layerContainerNode)
} else {
this.layerReactComponent = ReactDOM.unstable_renderSubtreeIntoContainer(this, layerReactEl, this.layerContainerNode)
}
},
_layerUnrender () {
if (this.layerWillUnmount) this.layerWillUnmount(this.layerContainerNode)
ReactDOM.unmountComponentAtNode(this.layerContainerNode)
},
// renderLayer() {
// Must be implemented by consumer.
// }
})
export default ReactLayerMixin