-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
/
Copy pathFrame.js
42 lines (37 loc) · 1.26 KB
/
Frame.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
import React, {Component} from 'react';
import {createPortal} from 'react-dom';
export default class Frame extends Component {
componentDidMount() {
this.node.addEventListener('load', this.handleLoad);
}
handleLoad = () => {
this.setupFrameBaseStyle();
};
componentWillUnmout() {
this.node.removeEventListener('load', this.handleLoad);
}
setupFrameBaseStyle() {
if (this.node.contentDocument) {
this.iframeHtml = this.node.contentDocument.documentElement;
this.iframeHead = this.node.contentDocument.head;
this.iframeRoot = this.node.contentDocument.body;
this.forceUpdate();
}
}
render() {
const {children, head, title = '', style = {}, dataTestId = '', ...rest} = this.props;
return (
<iframe
srcDoc={`<!DOCTYPE html>`}
data-testid={dataTestId}
ref={node => (this.node = node)}
title={title}
style={style} frameBorder="0"
{...rest}
>
{this.iframeHead && createPortal(head, this.iframeHead)}
{this.iframeRoot && createPortal(children, this.iframeRoot)}
</iframe>
);
}
}