-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLayer.js
37 lines (35 loc) · 1.06 KB
/
Layer.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
import React from "react"
import { createPortal } from "react-dom"
import { Consumer } from "./context"
const Layer = props => (
<Consumer>
{({ host, root }) =>
host && root && <LayerImpl {...props} host={host} root={root} />
}
</Consumer>
)
export default Layer
class LayerImpl extends React.Component {
state = { container: null }
componentDidMount() {
const { host, index, onMount, root } = this.props
const container = host.ownerDocument.createElement("div")
const sibling = typeof index === "number" && host.children[index]
sibling
? host.insertBefore(container, sibling)
: host.appendChild(container)
this.setState({ container }, () => {
root && onMount && onMount(root)
})
}
componentWillUnmount() {
const { container } = this.state
const { root, host, onUnmount } = this.props
root && onUnmount && onUnmount(root)
host && container && host.removeChild(container)
}
render() {
const { container } = this.state
return container && createPortal(this.props.children, container)
}
}