All Projects → klarna → remote-frames

klarna / remote-frames

Licence: Apache-2.0 License
Render a subset of the React tree to a different location, from many locations, without having to coordinate them

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to remote-frames

oembed
A simple plugin to extract media information from websites, like youtube videos, twitter statuses or blog articles.
Stars: ✭ 34 (+25.93%)
Mutual labels:  iframe
phar.scer.io
🗜️ Online PHAR converter based on JS
Stars: ✭ 29 (+7.41%)
Mutual labels:  react-dom
rc-dock
Dock Layout for React Component
Stars: ✭ 318 (+1077.78%)
Mutual labels:  react-dom
iframe-worker
A tiny WebWorker polyfill for the file:// protocol
Stars: ✭ 23 (-14.81%)
Mutual labels:  iframe
iFrameX
Iframe generator with dynamic content injection like HTML, Javascript, CSS, etc. and two ways communication, parent <-> iframe.
Stars: ✭ 18 (-33.33%)
Mutual labels:  iframe
portals-pluto
Mirror of Apache Pluto
Stars: ✭ 24 (-11.11%)
Mutual labels:  portals
redux-react-boilerplate-2018
Redux enabled boilerplate for React, react-bootstrap, babel and webpack ready for 2018
Stars: ✭ 12 (-55.56%)
Mutual labels:  react-dom
postonents
React meets Emails | ⚛️ x 📧= 🔥
Stars: ✭ 90 (+233.33%)
Mutual labels:  react-dom
angular-through-iframe
How to use angular.js through an iframe
Stars: ✭ 29 (+7.41%)
Mutual labels:  iframe
beat
Server framework to create fast and lightweight projects
Stars: ✭ 12 (-55.56%)
Mutual labels:  react-dom
vue-iframe-print
一款支持局部打印的 vue插件
Stars: ✭ 26 (-3.7%)
Mutual labels:  iframe
react-lite-yt-embed
React version of lite-youtube-embed iframe which render fast 🚀
Stars: ✭ 79 (+192.59%)
Mutual labels:  iframe
react-native-react-bridge
An easy way to integrate your React (or Preact) app into React Native app with WebView.
Stars: ✭ 84 (+211.11%)
Mutual labels:  react-dom
work
My open source projects portfolio. Built with React.
Stars: ✭ 73 (+170.37%)
Mutual labels:  react-dom
data-transport
A generic and responsible communication transporter(iframe/Broadcast/Web Worker/Service Worker/Shared Worker/WebRTC/Electron, etc.)
Stars: ✭ 27 (+0%)
Mutual labels:  iframe
useSharedState
useSharedState is a simple hook that can be used to share state between multiple React components.
Stars: ✭ 0 (-100%)
Mutual labels:  react-dom
ibridge
Typesafe iframe bridge for easy parent child bidirectional communication
Stars: ✭ 25 (-7.41%)
Mutual labels:  iframe
focus-outside
📦 一个很棒的 clickOutside 库,它解决了 iframe 无法触发 clickOutside 的问题,并且它支持分组绑定处理。A good clickOutside library, which solves the problem that iframe cannot trigger clickOutside, and it supports grouping binding processing.
Stars: ✭ 74 (+174.07%)
Mutual labels:  iframe
qadmin
基于layui框架与Vue.js构建的QAdmin轻量级后台模板
Stars: ✭ 34 (+25.93%)
Mutual labels:  iframe
tacklebox
🎣React UX components for handling common interactions
Stars: ✭ 15 (-44.44%)
Mutual labels:  react-dom

Deprecated

No longer being maintained.

@klarna/remote-frames

Build Status npm version

Render a subset of the React tree to a different location, from many locations, without having to coordinate them.

Usage

Say that you have an HTML with two DOM nodes that you want to render to:

<!doctype html>
<html>
  <head>
    <title>Remote frame</title>
  </head>
  <body>
    <div id="dialogs-node"></div>
    <div id="main-content-node"></div>
  </body>
</html>

…and for some reason, you want elements in the React tree rendered under the "main-content-node" to be able to inject elements into the "dialogs-node". The RemoteFrame allows you to send this elements to the remote tree (the one under "dialogs-node").

import React, { Component } from 'react'
import { render } from 'react-dom'
import { RemoteFrame, RemoteFramesProvider } from '@klarna/remote-frames'

const Dialog1 = () => <article>
  <h2>Lorem ipsum</h2>
</article>

const Dialog2 = () => <section>
  <h3>Dolor sit amet</h3>
</section>

class App extends Component {
  constructor() {
    super()

    this.state = {
      showDialog1: false,
      showDialog2: false,
    }
  }

  render() {
    const { showDialog1, showDialog2 } = this.state

    return <RemoteFramesProvider
      targetDomElement={Promise.resolve(
        document.getElementById('dialogs-node')
      )}
      onFrameAdded={frameJSX => {
        console.log(
          'a new frame was added to the dialogs-node stack',
          frameJSX
        )
      }}
      onFrameRemoved={frameJSX => {
        console.log(
          'a frame was removed from the dialogs-node stack',
          frameJSX
        )
      }}
      onNoFrames={lastJSXRemoved => {
        console.log(
          'all frames have been removed from the stack',
          lastJSXRemoved
        )
      }}>
      <div>
        <h1>App that demonstrates remote-frames</h1>
        <button
          onClick={() => this.setState({
            showDialog1: !showDialog1
          })}>
          {showDialog1 ? 'Hide Dialog 1' : 'Show Dialog 1'}
        </button>

        <button
          onClick={() => this.setState({
            showDialog2: !showDialog1
          })}>
          {showDialog2 ? 'Hide Dialog 2' : 'Show Dialog 2'}
        </button>

        {showDialog1 && <RemoteFrame>
          <Dialog1 />
        </RemoteFrame>}

        {showDialog2 && <RemoteFrame>
          <Dialog2 />
        </RemoteFrame>}
      </div>
    </RemoteFramesProvider>
  }
}

render(
  <App />,
  document.getElementById('main-content-node')
)

Whenever you click the "Show" / "Hide" buttons, the dialogs are sent to a React tree under the "dialogs-node", and rendered one at a time. If there was no dialog being shown at the time, then the new dialog is added; if there was a dialog shown already, the new dialog is shown instead, but then if the new dialog is removed, the old dialog is shown again, as in a sort of stack.

State of the elements inside the RemoteFrame is preserved, even when unmounted.

Missing RemoteFramesProvider

If there is no RemoteFramesProvider in the tree before the RemoteFrame, the content of RemoteFrame will just be rendered in place.

Context

For the React.context to be propagated to the new tree, you have to manually specify what props of the context you want to propagate:

import React, { Component } from 'react'
import { render } from 'react-dom'
import PropTypes from 'prop-types'
import { getContext, withContext } from 'recompose'
import {
  RemoteFrame,
  RemoteFramesProvider
} from '@klarna/remote-frames'

const Dialog1 = getContext({ content1: PropTypes.string })(({content1}) => <article>
  <h2>{content1}</h2>
</article>)

const Dialog2 = getContext({ content2: PropTypes.string })(({content2}) => <section>
  <h3>{content2}</h3>
</section>)


const App = withContext(
  {
    content1: PropTypes.string,
    content2: PropTypes.string,
  },
  () => ({
    content1: 'Hello Dialog 1',
    content2: 'Hello Dialog 2',
  })
)(() => {
  return <RemoteFramesProvider
    contextTypes={{
      content1: PropTypes.string,
    }}
    targetDomElement={Promise.resolve(
      document.getElementById('dialogs-node')
    )}>
    <div>
      <h1>App that demonstrates remote-frames</h1>
      <button
        onClick={() => this.setState({
          showDialog1: !showDialog1
        })}>
        {showDialog1 ? 'Hide Dialog 1' : 'Show Dialog 1'}
      </button>

      <button
        onClick={() => this.setState({
          showDialog2: !showDialog1
        })}>
        {showDialog2 ? 'Hide Dialog 2' : 'Show Dialog 2'}
      </button>

      <RemoteFrame>
        <Dialog1 />
      </RemoteFrame>

      <RemoteFrame
        contextTypes={{
          content2: PropTypes.string,
        }}>
        <Dialog2 />
      </RemoteFrame>
    </div>
  </RemoteFramesProvider>
})

render(
  <App />,
  document.getElementById('main-content-node')
)

Callbacks on RemoteFramesProvider

Two callbacks are available on RemoteFramesProvider:

  • onFrameAdded: gets called whenever another frame is added to the stack
  • onNoFrames: gets called whenever all frames are removed from the stack
  • onFrameRemoved: gets called whenever a frame is removed from the stack

Passing the targetDomElement

The targetDomElement used to render the new React tree can be passed directly to the RemoteFramesProvider as a prop, or it can be passed as a Promise, allowing you to wait until the targetDomElement is available (for example if it is rendered in another window).

Frames stacked before the targetDomElement is available will be queued, so you will not lose any information.

Wrapping into wrapperComponent

The wrapperComponent (alongside with wrapperComponentProps) used to wrap GlobalTarget into HOC (for example if it is needed to wrap everything into ThemeProvider, etc.).

<RemoteFramesProvider
  targetDomElement={document.getElementById('dialogs-node')}
  wrapperComponent={ThemeProvider}
  wrapperComponentProps={{ value: themeName }}>
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].