-
-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathFrame.jsx
164 lines (139 loc) · 4.22 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import { FrameContextProvider } from './Context';
import Content from './Content';
export 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;
this.nodeRef = React.createRef();
this.state = { iframeLoaded: false };
}
componentDidMount() {
this._isMounted = true;
const doc = this.getDoc();
if (doc) {
this.nodeRef.current.contentWindow.addEventListener(
'DOMContentLoaded',
this.handleLoad
);
}
}
componentWillUnmount() {
this._isMounted = false;
this.nodeRef.current.removeEventListener(
'DOMContentLoaded',
this.handleLoad
);
}
getDoc() {
return this.nodeRef.current ? this.nodeRef.current.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];
}
setRef = node => {
this.nodeRef.current = node;
const { forwardedRef } = this.props; // eslint-disable-line react/prop-types
if (typeof forwardedRef === 'function') {
forwardedRef(node);
} else if (forwardedRef) {
forwardedRef.current = node;
}
};
handleLoad = () => {
clearInterval(this.loadCheck);
// Bail update as some browsers will trigger on both DOMContentLoaded & onLoad ala firefox
if (!this.state.iframeLoaded) {
this.setState({ iframeLoaded: true });
}
};
// In certain situations on a cold cache DOMContentLoaded never gets called
// fallback to an interval to check if that's the case
loadCheck = () =>
setInterval(() => {
this.handleLoad();
}, 500);
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>
);
const mountTarget = this.getMountTarget();
if (!mountTarget) {
return null;
}
return [
ReactDOM.createPortal(this.props.head, this.getDoc().head),
ReactDOM.createPortal(contents, mountTarget)
];
}
render() {
const props = {
...this.props,
srcDoc: this.props.initialContent,
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;
delete props.forwardedRef;
return (
<iframe {...props} ref={this.setRef} onLoad={this.handleLoad}>
{this.state.iframeLoaded && this.renderFrameContents()}
</iframe>
);
}
}
export default React.forwardRef((props, ref) => (
<Frame {...props} forwardedRef={ref} />
));