Giter Site home page Giter Site logo

y-b-s / react-frame-component Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ryanseddon/react-frame-component

0.0 2.0 0.0 1.15 MB

Render your React app to an iFrame

Home Page: http://ryanseddon.github.io/react-frame-component/

License: MIT License

JavaScript 97.47% HTML 2.53%

react-frame-component's Introduction

React <Frame /> component

NPM version Build Status Dependency 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:

import Frame from 'react-frame-component';

Go check out the demo.

const Header = ({ children }) => (
  <Frame>
    <h1>{children}</h1>
  </Frame>
);

ReactDOM.render(<Header>Hello</Header>, document.body);

Or you can wrap it at the render call.

ReactDOM.render(
  <Frame>
    <Header>Hello</Header>
  </Frame>,
  document.body
);
Props:
head

head: PropTypes.node

The head 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>'

The initialContent 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 does not 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

The mountTarget props is a css selector (#target/.target) that specifies where in the initialContent of the iframe, children will be mounted.

<Frame
  initialContent='<!DOCTYPE html><html><head></head><body><h1>i wont be changed</h1><div id="mountHere"></div></body></html>'
  mountTarget='#mountHere'
  >
</Frame>
contentDidMount and contentDidUpdate

contentDidMount: PropTypes.func contentDidUpdate: PropTypes.func

contentDidMount and contentDidUpdate are conceptually equivalent to componentDidMount and componentDidUpdate, respecitvely. The reason these are needed is because internally we call ReactDOM.render which starts a new set of lifecycle calls. This set of lifecycle calls are sometimes triggered after the lifecycle of the parent component, so these callbacks provide a hook to know when the frame contents are mounted and updated.

Accessing the iframe's window and document

The iframe's window and document may be accessed via the FrameContextConsumer.

import Frame, { FrameContextConsumer } from 'react-frame-component'

const MyComponent = (props, context) => (
  <Frame>
    <FrameContextConsumer>
      {
        // Callback is invoked with iframe's window and document instances
        ({document, window}) => {
          // Render Children
        }
      }
    </FrameContextConsumer>
  </Frame>
);

More info

I wrote a blog post about building this component.

License

Copyright 2014, Ryan Seddon. This content is released under the MIT license http://ryanseddon.mit-license.org

react-frame-component's People

Contributors

alejandrogarciasalas avatar alvinlindstam avatar aronwoost avatar darthtrevino avatar daviferreira avatar dougshamoo avatar ericclemmons avatar erquhart avatar fer0x avatar fredrikekelund avatar graingert avatar hfter789 avatar iamdustan avatar ibash avatar jeremyblalock avatar joshuahhh avatar jstcki avatar le0nik avatar namefilip avatar ossamaz avatar raeldc avatar ryanseddon avatar slybridges avatar tuckerwhitehouse avatar urara avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.