• Stars
    star
    1,129
  • Rank 41,225 (Top 0.9 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 5 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Teleport React components in the same React tree ๐Ÿ“ก

react-teleporter

License npm package npm bundle size CI codecov

Teleport React components in the same React tree.

๐Ÿ‘‰ Read how to use it to create scalable layouts

๐Ÿ‘‰ Checkout the demo on CodeSandbox

npm install react-teleporter

Example

import { createTeleporter } from "react-teleporter";

const StatusBar = createTeleporter();

function Header() {
  return (
    <header>
      <StatusBar.Target />
    </header>
  );
}

function Page() {
  return (
    <main>
      {/* Teleport "Loading..." into the header */}
      <StatusBar.Source>Loading...</StatusBar.Source>
    </main>
  );
}

function App() {
  return (
    <div>
      <Header />
      <Page />
    </div>
  );
}

Why?

In complex app, you may have to configure a part of the application from another. If you know react-helmet it is the same philosophy. You want to configure a part of your application from another place.

Recipes

Use another target element

Use as property on target to specify another tag.

const Teleporter = createTeleporter()

<Teleporter.Target as="footer" />

Be careful of specifying an element with a ref to a DOM element, it uses React Portals under the hood.

Use props on target

All props are forwarded to target.

const Teleporter = createTeleporter()

<Teleporter.Target onClick={/* ... */} />

Create a custom target ref

Use useTargetRef to create a custom target ref.

const Teleporter = createTeleporter();

function CustomTarget() {
  const targetRef = Teleporter.useTargetRef();
  return <div ref={targetRef} />;
}

Use multiple sources

By default only one Source is allowed to be injected into a Target. Sometimes you may want to inject multiple sources into a single target. Create teleporter with { multiSources: true } option.

const Teleporter = createTeleporter({ multiSources: true })

<Teleporter.Source multiple>
  <a href="#">A link</a>
</Teleporter.Source>

<Teleporter.Source multiple>
  <a href="#">Another link</a>
</Teleporter.Source>

// The target will contains the two links

Use function as children

Useful for having access to the Target element. E.g., to dispatch an event through the Target when something happens in the Source.

const Teleporter = createTeleporter();

const forwardEvent = (element) => (event) =>
  element.dispatch(new Event(event.type, event));

<Teleporter.Source>
  {(element) => <div onClick={forwardEvent(element)}></div>}
</Teleporter.Source>;

API

createTeleporter

createTeleporter is the only method exposed by this package. It returns an object containing a Target, a Source and a useTargetRef to create a custom target.

import { createTeleporter } from "react-teleporter";

const Teleporter = createTeleporter();

More Repositories

1

svgr

Transform SVGs into React components ๐Ÿฆ
TypeScript
10,516
star
2

loadable-components

The recommended Code Splitting library for React โœ‚๏ธโœจ
JavaScript
7,653
star
3

twc

Create reusable React + Tailwind CSS components in one line โœจ
TypeScript
1,337
star
4

error-overlay-webpack-plugin

Catch errors with style ๐Ÿ’ฅโœจ
JavaScript
873
star
5

react-merge-refs

React utility to merge refs ๐Ÿ–‡
TypeScript
582
star
6

recompact

โš› A set of React higher-order components utilities. Drop-in replacement for recompose.
JavaScript
417
star
7

smooth-doc

Ready to use documentation theme for Gatsby.
JavaScript
256
star
8

circular-progress

๐ŸŒ€ Circular progress widget, dependency-free and configurable.
JavaScript
253
star
9

angular-notification

Notification service for Angular using native HTML5 API.
JavaScript
92
star
10

push-notify

Easily send notifications over multiple protocols (apn, gcm, c2dm, mpns).
JavaScript
77
star
11

node-apns

APNS (Apple Push Notification Service) interface written in node.js
JavaScript
61
star
12

react-flatten-children

React utility to flatten fragments ๐Ÿ—œ
TypeScript
59
star
13

jamtemplates.com

Curated collection of free Gatsby themes.
JavaScript
59
star
14

bower-registry

Simple bower registry using node and redis.
JavaScript
57
star
15

gregberge.com

Personal website of Greg Bergรฉ.
MDX
46
star
16

angular-offline

Offline support for AngularJS applications.
JavaScript
39
star
17

stargazer

Command line to list emails of GitHub repository stargazers.
JavaScript
38
star
18

bobun

Bobun is a very light (1.1k) UI oriented Backbone library. It supports one-way and two-way bindings and sub-views.
JavaScript
33
star
19

angular-draganddrop

Drag and drop directives for Angular using native HTML5 API.
JavaScript
30
star
20

node-taskman

Fast work queue based on redis.
JavaScript
28
star
21

std-mocks

Mock node stdout and stderr without pain.
JavaScript
26
star
22

angular-primus

Primus service for Angular.js
JavaScript
24
star
23

angular-clickout

AngularJS directive to handle a click outside an element.
JavaScript
19
star
24

hulkster

Command line utility to compile hogan.js templates
JavaScript
17
star
25

bobun-ui

Set of UI components based on Backbone Bobun.
JavaScript
13
star
26

lazy-retina

Lazyload image with retina support (based on https://github.com/fasterize/lazyload)
JavaScript
13
star
27

angular-ws

WebSocket service for AngularJS.
JavaScript
13
star
28

ya-sqs

Yet Another AWS SQS wrapper with pull (long polling), push, error management and promises.
JavaScript
12
star
29

actionJS

HTML5 Frameworks inspired by Flash AS3 API
JavaScript
10
star
30

shipit-tutorial

Deploy node.js application using Shipit (tutorial).
JavaScript
10
star
31

angular-pretty-bytes

Pretty bytes filter for AngularJS: 1337 โ†’ 1.34 kB.
JavaScript
9
star
32

content-range

Parser and formatter for HTTP/1.1 Content-Range header field.
TypeScript
9
star
33

smime

Node / io.js wrapper for OpenSSL S/MIME command.
JavaScript
9
star
34

connect-html-minifier

HTML minifier middleware for connect.
JavaScript
7
star
35

angular-match

Validate if two inputs match the same value.
JavaScript
7
star
36

npm-sync

npm link on steroids: watch and reinstall a npm package on demand.
JavaScript
7
star
37

black-pearl-client

Ultra simple client to push metrics in a Elastic Search + Kibana.
6
star
38

express-err

Basic error handler for express.
JavaScript
6
star
39

less-hidpi

Simple Less mixin that automatically serves high resolution graphics to high density (Retina-like) displays.
JavaScript
6
star
40

smooth-doc-starter

Official Smooth DOC starter project.
JavaScript
6
star
41

github-monitor

Monitor GitHub repository statuses.
JavaScript
5
star
42

funny-status

Check NPM and GitHub status with sound.
JavaScript
5
star
43

picjam-game

PicJam game realised during FHacktory 2014.
JavaScript
4
star
44

instatourparis13

Photo gallery of Tour Paris 13 based on Instagram.
CSS
4
star
45

black-pearl-server

Metrics collector that push metrics to Elastic Search + Kibana.
4
star
46

image-size-parser

Parse the size of an image from a string. '120x20@2x' โ†’ {width: 240, height: 40}
JavaScript
4
star
47

youhou

Easily broadcast your hostname in using Multicast DNS.
JavaScript
4
star
48

format-keys

Format object keys recursively.
JavaScript
4
star
49

bootstart

Modern website project starter. Using express, requirejs, backbone, less, grunt, bower.. and other cool stuff.
JavaScript
3
star
50

ua-semver

Check user-agent with semver syntax.
JavaScript
3
star
51

handlebars-md5

Handlebars MD5 helper.
JavaScript
3
star
52

svgr-create-react-app

Demo of SVGR + create-react-app
JavaScript
3
star
53

ya-sqs-monitor

Monitor SQS tasks and batches using redis.
JavaScript
3
star
54

github-label-templates

GitHub labels templates.
3
star
55

rethink-odm

Simple Object Document Mapper for RethinkDB.
JavaScript
3
star
56

node-sphero

A node.js client for controlling Sphero robotic ball.
JavaScript
3
star
57

smooth-code-website

Smooth Code website.
CSS
3
star
58

rollingpin

Reverse-proxy-cache written in node.js
JavaScript
3
star
59

on-mange-quoi

Website to help choosing a restaurant.
JavaScript
3
star
60

spn-push-package

Generate a Safari Push Notifications package.
JavaScript
2
star
61

connect-mock-request

Connect mock request useful for testing.
JavaScript
2
star
62

makemeepic

Transform your LinkedIn profile into a Mythic Tale using AI.
TypeScript
2
star
63

jenkins-badge

Generate status badges from Jenkins build.
JavaScript
2
star
64

spn-auth-token

Encoding and decoding authentication token of Safari Push Notifications.
JavaScript
2
star
65

clockify

jQuery plugin to create light custom time-icon using SVG
2
star
66

pipe-event

Pipe an event from an event emitter to another.
2
star
67

stream-line-wrapper

Wrap each lines of a stream with a prefix, suffix or a custom function.
JavaScript
2
star
68

arduino-sky

Reproduce sky using Arduino, WebSocket and SVG.
JavaScript
2
star
69

therightcode

JavaScript and wathever blog powered by Ghost.
JavaScript
1
star
70

auto-label

Automatically apply labels on pull-requests.
JavaScript
1
star
71

iphone-notifier

Get notified of iPhone availability by email.
JavaScript
1
star
72

syllabou

Random word generator
JavaScript
1
star
73

monki-run

HTML5 game experience using Pixi.js.
JavaScript
1
star
74

es2015-training

ES2015 training.
JavaScript
1
star
75

argos-test-repository

Argos test repository
1
star
76

springtik-www

Springtik website.
JavaScript
1
star
77

gtalk-xmpp

Interact with GTalk using node.
JavaScript
1
star
78

hipush

Hipush server.
JavaScript
1
star