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.     if (iframe) {
    
  32.       const html = `
    
  33.     <!DOCTYPE html>
    
  34.     <html>
    
  35.     <body>
    
  36.       <div id="root"></div>
    
  37.     </body>
    
  38.     </html>
    
  39.     `;
    
  40. 
    
  41.       const document = iframe.contentDocument;
    
  42. 
    
  43.       document.open();
    
  44.       document.write(html);
    
  45.       document.close();
    
  46. 
    
  47.       setElement(document.getElementById('root'));
    
  48.     }
    
  49.   }, []);
    
  50. 
    
  51.   return (
    
  52.     <Fragment>
    
  53.       <iframe title="Test Iframe" ref={ref} style={iframeStyle} />
    
  54.       <iframe
    
  55.         title="Secured Iframe"
    
  56.         src="https://example.com"
    
  57.         style={iframeStyle}
    
  58.       />
    
  59. 
    
  60.       {element ? createPortal(props.children, element) : null}
    
  61.     </Fragment>
    
  62.   );
    
  63. }
    
  64. 
    
  65. function Greeting() {
    
  66.   return (
    
  67.     <p>
    
  68.       Hello from within an <code>&lt;iframe&gt;</code>!
    
  69.     </p>
    
  70.   );
    
  71. }