Skip to content

Commit 1eadebb

Browse files
committed
Update README 🤓
1 parent a077dd8 commit 1eadebb

File tree

1 file changed

+37
-29
lines changed

1 file changed

+37
-29
lines changed

README.md

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# React Tunnels 🚇 [![npm](https://img.shields.io/npm/v/react-tunnels.svg?style=flat)](https://www.npmjs.org/package/react-tunnels)
1+
# React Tunnels 🚇 [![npm](https://img.shields.io/npm/v/react-tunnels.svg?style=flat)](https://www.npmjs.org/package/react-tunnels)[![Build Status](http://img.shields.io/travis/javivelasco/react-tunnels/master.svg?style=flat-square)](https://travis-ci.org/javivelasco/react-tunnels)
22

33
Render React components in placeholders that are placed somewhere else in the component tree.
44

@@ -10,52 +10,44 @@ yarn add react-tunnels
1010

1111
### Why
1212

13-
There is a common use case in React apps where you want to define a `Layout` where the content of some elements are defined by child components. For example, you want define `Layout` just once and reuse it for every page but it has a breadcrumb whose steps depend on children components. This tiny library allows you to define "tunnels" to render from a element to whatever other element in the app, even elements on top of the tree. It's like `Portal` but the target is a component instead of a DOM element.
13+
There is a common use case in React apps where you want to define a `Layout` where the content of some elements is defined by `children` components. For example, you want to define `Layout` just once and reuse it for every page but it has a breadcrumb whose steps depend on `children` components. This tiny library allows you to define *tunnels* to render from an element to whatever another element in the App, even elements located on top of the tree. It's like `Portal` but the target is a *component* instead of a *DOM element*.
1414

15-
### Usage
15+
## Usage
1616

17-
Define a `TunnelPlaceholder` identified by an `id` and decide what properties are going to be passed to its render function by defining `Tunnel` components with the same id anywhere else in the app. If you define just a single `Tunnel` its props will be passed, if there are more than once tunnel for an `id`, the tunnel will receive an array of `props`. Let's see some examples.
17+
Define a `TunnelPlaceholder` identified by an `id` and decide what properties are going to be passed to its `render` function by defining `Tunnel` components with the **same id** anywhere else in the app. If you define just a single `Tunnel` its props will be passed straight to the `render` function, if there is more than one `Tunnel` for a single `id`, the placeholder `render` function will receive an `item` argument which is an Array containing the `props` for each `Tunnel`. Let's see some examples.
1818

19-
### Simple example: tunneling children
19+
### Simple example: children tunneling
2020

21-
Define a placeholder without any render function so it will render any children coming from `Tunnel` components.
21+
Define a placeholder without any render function so it will render any children coming from a `Tunnel` component with the same id.
2222

2323
```jsx
24-
import { TunnelsProvider, TunnelPlaceholder, Tunnel } from 'react-tunnels'
24+
import { TunnelProvider, TunnelPlaceholder, Tunnel } from 'react-tunnels'
2525

2626
render(
27-
<TunnelsProvider>
27+
<TunnelProvider>
2828
<div>
2929
<TunnelPlaceholder id="my-tunnel" />
3030
<Tunnel id="my-tunnel">
3131
This will be rendered on the placeholder 👆
3232
</Tunnel>
3333
</div>
34-
</TunnelsProvider>
34+
</TunnelProvider>
3535
)
3636
```
3737

38-
### A more complex example: building a Breadcrumb
38+
Check the real example [here](https://codesandbox.io/s/p79k8w0jnq)
3939

40-
```jsx
41-
render(
42-
<TunnelsProvider>
43-
{/* This will render the breadcrumb */}
44-
<Breadcrumbs />
45-
{/* Somewhere else in children */}
46-
<Breadcrumb url="/products">Products</Breadcrumb>
47-
<Breadcrumb url="/products/123">Product <strong>123</strong></Breadcrumb>
48-
</TunnelsProvider>
49-
)
40+
### More complex example: building a Breadcrumb
41+
42+
It's easy to build a breadcrumb using the prop `multiple` in the `TunnelPlaceholder`. This allows to let it know that there will be multiple tunnels so the `render` function will be called with an array of props.
5043

44+
```jsx
5145
const Breadcrumbs = () => (
52-
<TunnelPlaceholder id="breadcrumb">
53-
{({ items = [] }) => (
54-
<ul>
55-
{items.map(({ children, href }) => (
56-
<li><a href={href}>{children}</a></li>
57-
))}
58-
</ul>
46+
<TunnelPlaceholder id="breadcrumb" multiple>
47+
{({ items }) => (
48+
items.map(({ children, href }) => (
49+
<span><a href={href}>{children}</a></span>
50+
))
5951
)}
6052
</TunnelPlaceholder>
6153
)
@@ -65,13 +57,29 @@ const Breadcrumb = ({ children, url }) => (
6557
{children}
6658
</Tunnel>
6759
)
60+
61+
render(
62+
<TunnelProvider>
63+
{/* This will render the breadcrumb */}
64+
<Breadcrumbs />
65+
{/* Somewhere else in children */}
66+
<Breadcrumb url="/products">Products</Breadcrumb>
67+
<Breadcrumb url="/products/123">Product <strong>123</strong></Breadcrumb>
68+
</TunnelProvider>
69+
)
6870
```
6971

70-
### Similar Libraries
72+
Check the live example [here](https://codesandbox.io/s/0ym0n37jnl)
73+
74+
## Similar Libraries
7175

72-
- [React Slot Fill](https://github.com/camwest/react-slot-fill): [@camwest](https://github.com/camwest) has built a similar project but with a different API and a bit more limited use cases. The main difference is that you can't pass content to a placeholder from multiple entry points while react-tunnels does it by passing an array with the props defined by each tunnel to the render function of the placeholder. For simple cases, it is pretty similar.
76+
- [React Slot Fill](https://github.com/camwest/react-slot-fill): A similar project built by [Cameron Westland](https://github.com/camwest) with a slightly different API and a bit more limited use cases. The main difference is that you can't pass content to a placeholder from multiple entry points. react-tunnels does this by passing an array with the props defined by each tunnel to the render function of the placeholder. For simple cases though, it is pretty similar.
7377
- [Preact Slots](https://github.com/developit/preact-slots): A library similar to React Slot Fill but for [Preact](https://github.com/developit/preact) developed by [Jason Miller](https://twitter.com/_developit).
7478

79+
## About
80+
81+
This project has been developed by [Javi Velasco](https://twitter.com/javivelasco) as a way to build *Breadcrumb* components and `Layout` customizations for a variety of React projects. Any feeback, help or improvements is highly appreciated.
82+
7583
## License
7684

7785
This project is licensed under the terms of the [MIT license](https://github.com/javivelasco/react-tunnels/blob/master/LICENSE).

0 commit comments

Comments
 (0)