-
-
Notifications
You must be signed in to change notification settings - Fork 156
/
Frame.jsx
135 lines (116 loc) · 3.35 KB
/
Frame.jsx
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import { FrameContextProvider } from './Context';
import Content from './Content';
export default class Frame extends Component {
// React warns when you render directly into the body since browser extensions
// also inject into the body and can mess up React. For this reason
// initialContent is expected to have a div inside of the body
// element that we render react into.
static propTypes = {
style: PropTypes.object, // eslint-disable-line
head: PropTypes.node,
initialContent: PropTypes.string,
mountTarget: PropTypes.string,
contentDidMount: PropTypes.func,
contentDidUpdate: PropTypes.func,
children: PropTypes.oneOfType([
PropTypes.element,
PropTypes.arrayOf(PropTypes.element)
])
};
static defaultProps = {
style: {},
head: null,
children: undefined,
mountTarget: undefined,
contentDidMount: () => {},
contentDidUpdate: () => {},
initialContent:
'<!DOCTYPE html><html><head></head><body><div class="frame-root"></div></body></html>'
};
constructor(props, context) {
super(props, context);
this._isMounted = false;
}
componentDidMount() {
this._isMounted = true;
const doc = this.getDoc();
if (doc && doc.readyState === 'complete') {
this.forceUpdate();
} else {
this.node.addEventListener('load', this.handleLoad);
}
}
componentWillUnmount() {
this._isMounted = false;
this.node.removeEventListener('load', this.handleLoad);
}
getDoc() {
return this.node ? this.node.contentDocument : null; // eslint-disable-line
}
getMountTarget() {
const doc = this.getDoc();
if (this.props.mountTarget) {
return doc.querySelector(this.props.mountTarget);
}
return doc.body.children[0];
}
handleLoad = () => {
this.forceUpdate();
};
renderFrameContents() {
if (!this._isMounted) {
return null;
}
const doc = this.getDoc();
if (!doc) {
return null;
}
const contentDidMount = this.props.contentDidMount;
const contentDidUpdate = this.props.contentDidUpdate;
const win = doc.defaultView || doc.parentView;
const contents = (
<Content
contentDidMount={contentDidMount}
contentDidUpdate={contentDidUpdate}
>
<FrameContextProvider value={{ document: doc, window: win }}>
<div className="frame-content">{this.props.children}</div>
</FrameContextProvider>
</Content>
);
if (doc.body.children.length < 1) {
doc.open('text/html', 'replace');
doc.write(this.props.initialContent);
doc.close();
}
const mountTarget = this.getMountTarget();
return [
ReactDOM.createPortal(this.props.head, this.getDoc().head),
ReactDOM.createPortal(contents, mountTarget)
];
}
render() {
const props = {
...this.props,
children: undefined // The iframe isn't ready so we drop children from props here. #12, #17
};
delete props.head;
delete props.initialContent;
delete props.mountTarget;
delete props.contentDidMount;
delete props.contentDidUpdate;
return (
<iframe
{...props}
ref={node => {
this.node = node;
}}
>
{this.renderFrameContents()}
</iframe>
);
}
}