main
js 73 lines 1.39 KB
Raw
1 /** @flow */
2
3 import * as React from 'react';
4 import {Fragment} from 'react';
5 import {createPortal} from 'react-dom';
6
7 export default function Iframe(): React.Node {
8 return (
9 <Fragment>
10 <h2>Iframe</h2>
11 <div>
12 <Frame>
13 <Greeting />
14 </Frame>
15 </div>
16 </Fragment>
17 );
18 }
19
20 const iframeStyle = {border: '2px solid #eee', height: 80};
21
22 // $FlowFixMe[missing-local-annot]
23 function Frame(props) {
24 const [element, setElement] = React.useState(null);
25
26 const ref = React.useRef();
27
28 React.useLayoutEffect(function () {
29 const iframe = ref.current;
30
31 // $FlowFixMe[constant-condition]
32 if (iframe) {
33 const html = `
34 <!DOCTYPE html>
35 <html>
36 <body>
37 <div id="root"></div>
38 </body>
39 </html>
40 `;
41
42 const document = iframe.contentDocument;
43
44 document.open();
45 document.write(html);
46 document.close();
47
48 setElement(document.getElementById('root'));
49 }
50 }, []);
51
52 return (
53 <Fragment>
54 <iframe title="Test Iframe" ref={ref} style={iframeStyle} />
55 <iframe
56 title="Secured Iframe"
57 src="https://example.com"
58 style={iframeStyle}
59 />
60
61 {/* $FlowFixMe[constant-condition] */}
62 {element ? createPortal(props.children, element) : null}
63 </Fragment>
64 );
65 }
66
67 function Greeting() {
68 return (
69 <p>
70 Hello from within an <code>&lt;iframe&gt;</code>!
71 </p>
72 );
73 }