forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPortal.tsx
More file actions
28 lines (24 loc) · 650 Bytes
/
Portal.tsx
File metadata and controls
28 lines (24 loc) · 650 Bytes
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
import * as React from 'react';
import ReactDOM from 'react-dom';
export interface Props {
node?: HTMLDivElement;
}
export default class Portal extends React.Component<Props> {
defaultNode: HTMLDivElement;
componentWillUnmount() {
if (this.defaultNode) {
document.body.removeChild(this.defaultNode);
}
this.defaultNode = null;
}
render() {
if (!this.props.node && !this.defaultNode) {
this.defaultNode = document.createElement('div');
document.body.appendChild(this.defaultNode);
}
return ReactDOM.createPortal(
this.props.children,
this.props.node || this.defaultNode
);
}
}