Last active
February 7, 2020 01:34
-
-
Save rsoury/f63e6338e0cc561caf3d99f0d00509a3 to your computer and use it in GitHub Desktop.
React Frame Component + IFrame Resizer -- Resizing outside of React preventing re-rendering on resize. Good for wrapping providers (or wrapping an entire react app) in an IFrame to prevent style inheritance. Works with Create React App.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useEffect } from "react"; | |
import Frame, { FrameContextConsumer } from "react-frame-component"; | |
import { iframeResize } from "iframe-resizer"; | |
import iFrameResizeContentWindowScript from "!raw-loader!iframe-resizer/js/iframeResizer.contentWindow.min.js"; // eslint-disable-line | |
const SmartFrame = ({ children, ...props }) => { | |
const ref = React.createRef(); | |
// Component did mount | |
useEffect(() => { | |
iframeResize( | |
{ | |
checkOrigin: false | |
}, | |
ref.current.node | |
); | |
}, []); | |
return ( | |
<Frame ref={ref} {...props}> | |
<FrameContextConsumer> | |
{frameProps => { | |
const { document } = frameProps; | |
const script = document.createElement("script"); | |
script.type = "text/javascript"; | |
script.innerHTML = iFrameResizeContentWindowScript; | |
document.body.appendChild(script); | |
return children(frameProps); | |
}} | |
</FrameContextConsumer> | |
</Frame> | |
); | |
}; | |
export default SmartFrame; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment