Giter Site home page Giter Site logo

philips-software / react-cross-client-router Goto Github PK

View Code? Open in Web Editor NEW
15.0 4.0 0.0 6.98 MB

A tool to control React apps spanning multiple tabs, windows or devices.

License: MIT License

HTML 3.18% JavaScript 94.40% CSS 2.42%
react react-router cross-device

react-cross-client-router's Introduction

react-cross-client-router

NPM Build Status

A tool to control React apps spanning multiple tabs, windows or devices.

Control the tab on your second screen by using your first screen, all without using a server.

  • Run the example app to see it in action.
  • Checkout the example/ folder for source code.

Features

  • Channel agnostic:
    • Uses broadcast-channel for a frontend-only implementation
    • Supports websockets to control apps spanning multiple devices.
  • Uses the url to control tabs, dependent on react-router-dom
  • Keeps track of active tabs, detects when tabs quit.
  • Tab IDs are persistent.

Demo

React cross client router example screencast

Install

yarn add broadcast-channel react-cross-client-router

Quick start

The example below shows one parent (or master) page that includes links to two child (or detail) pages. When you click on one of the links, a new tab will be opened that renders the detail view. If you move the child to a separate browser (so that you can see both views at the same time), you can conveniently change the content of the detail view by simply clicking on any of the two links in the master view. You can even close the master view and the currently open detail view will be notified, providing you with a convenience link to reopen the master view in the new tab.

Application entry point

import React from 'react'
import ReactDOM from 'react-dom'
import BroadcastChannel from 'broadcast-channel'
import { BrowserRouter } from 'react-router-dom'
import { ClientRouterProvider, ClientRouterContext } from 'react-cross-client-router'

import { App } from './App'

ReactDOM.render(
  <BrowserRouter>
    <ClientRouterProvider
      channel={new BroadcastChannel('react-cross-tab-router')}
      storage={window.sessionStorage}
    >
      <App />
    </ClientRouterProvider>
  </BrowserRouter>,
  document.getElementById('root')
)

App

import React, { Component } from 'react'
import { Route } from 'react-router-dom'
import {
  ClientRouterContext
} from 'react-cross-client-router'

import { Master } from './Master'
import { Detail } from './Detail'

class App extends Component {
  static contextType = ClientRouterContext;
  render() {
    const clientRouter = this.context;
    return (
      <div>
        <h1>react-cross-client-router example</h1>
        <div>
          <p>Tab name: {clientRouter.tabId}</p>
          <p>Connected tabs: {clientRouter.tabs.join(', ')}</p>
        </div>
        <Route path='/' exact component={Master} />
        <Route path='/detail/:id' component={Detail} />
      </div>
    );
  }
}

export { App }

Master

import React, { Component } from 'react';
import {
  withClientRouter,
  ClientLink
} from 'react-cross-client-router'

const Master = withClientRouter(({ clientRouter }) => {
  return (
    <div>
      {clientRouter.tabs.includes('detail') ? (
        <h2>
          The detail view tab exists, click an detail link to update
          the content of the detail view...
        </h2>
      ) : (
        <h2>Click one of the links below to open the corresponding detail view in the new tab</h2>
      )}
      <ClientLink targetTab='detail' to={'/detail/1'}>
        Detail View 1
      </ClientLink>
      <ClientLink targetTab='detail' to={'/detail/2'}>
        Detail View 2
      </ClientLink>
    </div>
  )
})

export { Master }

Detail

import React, { Component } from 'react';
import {
  withClientRouter,
  ClientLink
} from 'react-cross-client-router'

const Detail = withClientRouter(({ match, clientRouter }) => {
  const id = match.params.id
  return (
    <div>
      {!clientRouter.tabs.includes('parent') && (
        <ClientLink targetTab='parent' to='/'>
          <h2>The parent tab seems to be closed, click here to reopen it.</h2>
        </ClientLink>
      )}
      {`Detail with id=${id}`}
    </div>
  )
})

export { Detail }

react-cross-client-router's People

Contributors

dependabot[bot] avatar jeroenknoops avatar marcinczenko avatar stam avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

react-cross-client-router's Issues

Are the process.env vars in the example correct?

Unless something changed, React apps created with create-react-app require REACT_APP prefix in the environment variable for the variable to be forwarded (see https://facebook.github.io/create-react-app/docs/adding-custom-environment-variables)

So instead of:

let relativePath = ''
if (process.env.PUBLIC_URL) {
  const publicUrl = new URL(process.env.PUBLIC_URL)
  relativePath = publicUrl.pathname
}

it would be:

let relativePath = ''
if (process.env.REACT_APP_PUBLIC_URL) {
  const publicUrl = new URL(process.env.REACT_APP_PUBLIC_URL)
  relativePath = publicUrl.pathname
}

Example on CodeSandbox

If it is possible it would be nice to have a ready to use example on Code Sandbox with the link to it included in the Readme.

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.