Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Render your React app to an iFrame

License

NotificationsYou must be signed in to change notification settings

ryanseddon/react-frame-component

Repository files navigation

NPM versionBuild StatusDependency Status

This component allows you to encapsulate your entire React application or per component in an iFrame.

npm install --save react-frame-component

How to use:

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);
Props:
head

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

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

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 and contentDidUpdate

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

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>);}
Accessing the iframe's window and document

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>);

More info

I wrote ablog post about building this component.

License

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

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors45


[8]ページ先頭

©2009-2025 Movatter.jp