Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork157
Render your React app to an iFrame
License
ryanseddon/react-frame-component
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This component allows you to encapsulate your entire React application or per component in an iFrame.
npm install --save react-frame-component
importFramefrom'react-frame-component';
Go check out thedemo.
const Header = ({ children }) => (<Frame><h1>{children}</h1></Frame>);ReactDOM.render(<Header>Hello</Header>, document.body);Or you can wrap it at therender call.
ReactDOM.render(<Frame><Header>Hello</Header></Frame>, document.body);
head: PropTypes.node
Thehead prop is a dom node that gets inserted before the children of the frame. Note that this is injected into the body of frame (see the blog post for why). This has the benefit of being able to update and works for stylesheets.
initialContent: PropTypes.string
Defaults to'<!DOCTYPE html><html><head></head><body><div></div></body></html>'
TheinitialContent props is the initial html injected into frame. It is only injected once, but allows you to insert any html into the frame (e.g. a head tag, script tags, etc). Note that it doesnot update if you change the prop. Also at least one div is required in the body of the html, which we use to render the react dom into.
mountTarget: PropTypes.string
ThemountTarget props is a css selector (#target/.target) that specifies where in theinitialContent of the iframe, children will be mounted.
<FrameinitialContent='<!DOCTYPE html><html><head></head><body><h1>i wont be changed</h1><div></div></body></html>'mountTarget='#mountHere'></Frame>
contentDidMount: PropTypes.funccontentDidUpdate: PropTypes.func
contentDidMount andcontentDidUpdate are conceptually equivalent tocomponentDidMount andcomponentDidUpdate, respectively. The reason these areneeded is because internally we callReactDOM.render which starts a new set oflifecycle calls. This set of lifecycle calls are sometimes triggered after thelifecycle of the parent component, so these callbacks provide a hook to knowwhen the frame contents are mounted and updated.
ref: PropTypes.oneOfType([ PropTypes.func, PropTypes.shape({ current: PropTypes.instanceOf(Element) }) ])
Theref prop provides a way to access inner iframe DOM node. To utilitize this prop use, for example, one of the React's built-in methods to create a ref:React.createRef() orReact.useRef().
constMyComponent=(props)=>{constiframeRef=React.useRef();React.useEffect(()=>{// Use iframeRef for:// - focus managing// - triggering imperative animations// - integrating with third-party DOM librariesiframeRef.current.focus()},[])return(<Frameref={iframeRef}><InnerComponent/></Frame>);}
The iframe'swindow anddocument may be accessed via theFrameContextConsumer or theuseFrame hook.
The example withFrameContextConsumer:
importFrame,{FrameContextConsumer}from'react-frame-component'constMyComponent=(props,context)=>(<Frame><FrameContextConsumer>{// Callback is invoked with iframe's window and document instances({document, window})=>{// Render Children}}</FrameContextConsumer></Frame>);
The example withuseFrame hook:
importFrame,{useFrame}from'react-frame-component';constInnerComponent=()=>{// Hook returns iframe's window and document instances from Frame contextconst{ document, window}=useFrame();returnnull;};constOuterComponent=()=>(<Frame><InnerComponent/></Frame>);
I wrote ablog post about building this component.
Copyright 2014, Ryan Seddon.This content is released under the MIT licensehttp://ryanseddon.mit-license.org
About
Render your React app to an iFrame
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.


