-
Notifications
You must be signed in to change notification settings - Fork 138
/
Viewer.tsx
37 lines (32 loc) · 950 Bytes
/
Viewer.tsx
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 * as React from 'react';
import * as ReactDOM from 'react-dom';
import ViewerCore from './ViewerCore';
import ViewerProps from './ViewerProps';
export default (props: ViewerProps) => {
const defaultContainer = React.useRef(typeof document !== 'undefined' ? document.createElement('div') : null);
const [ container, setContainer ] = React.useState(props.container);
const [ init, setInit ] = React.useState(false);
React.useEffect(() => {
document.body.appendChild(defaultContainer.current);
}, []);
React.useEffect(() => {
if (props.visible && !init) {
setInit(true);
}
}, [props.visible, init]);
React.useEffect(() => {
if (props.container) {
setContainer(props.container);
} else {
setContainer(defaultContainer.current);
}
}, [props.container]);
if (!init) {
return null;
}
return ReactDOM.createPortal((
<ViewerCore
{...props}
/>
), container);
};