{"id":13630468,"url":"https://github.com/javivelasco/react-tunnels","last_synced_at":"2025-04-12T22:37:11.903Z","repository":{"id":46938799,"uuid":"119613170","full_name":"javivelasco/react-tunnels","owner":"javivelasco","description":"🚇 Render React components in placeholders that are placed somewhere else in the component tree.","archived":false,"fork":false,"pushed_at":"2022-12-07T09:16:16.000Z","size":729,"stargazers_count":398,"open_issues_count":20,"forks_count":11,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-10-29T22:32:34.511Z","etag":null,"topics":["breadcrumbs","components","inverse-rendering","react","react-tunnels","tunnels"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/javivelasco.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-01-31T00:40:01.000Z","updated_at":"2024-09-06T12:47:52.000Z","dependencies_parsed_at":"2023-01-24T16:30:59.079Z","dependency_job_id":null,"html_url":"https://github.com/javivelasco/react-tunnels","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/javivelasco%2Freact-tunnels","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/javivelasco%2Freact-tunnels/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/javivelasco%2Freact-tunnels/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/javivelasco%2Freact-tunnels/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/javivelasco","download_url":"https://codeload.github.com/javivelasco/react-tunnels/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248643006,"owners_count":21138353,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["breadcrumbs","components","inverse-rendering","react","react-tunnels","tunnels"],"created_at":"2024-08-01T22:01:44.105Z","updated_at":"2025-04-12T22:37:11.885Z","avatar_url":"https://github.com/javivelasco.png","language":"JavaScript","funding_links":[],"categories":["JavaScript","*.js"],"sub_categories":["React"],"readme":"# 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)\n\nRender React components in placeholders that are placed somewhere else in the component tree.\n\n## Install\n\n```\nyarn add react-tunnels\n```\n\n### Why\n\nThere 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*.\n\n## Usage\n\nDefine 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.\n\n### Simple example: children tunneling\n\nDefine a placeholder without any render function so it will render any children coming from a `Tunnel` component with the same id.\n\n```jsx\nimport { TunnelProvider, TunnelPlaceholder, Tunnel } from 'react-tunnels'\n\nrender(\n  \u003cTunnelProvider\u003e\n    \u003cdiv\u003e\n      \u003cTunnelPlaceholder id=\"my-tunnel\" /\u003e\n      \u003cTunnel id=\"my-tunnel\"\u003e\n        This will be rendered on the placeholder 👆\n      \u003c/Tunnel\u003e\n    \u003c/div\u003e\n  \u003c/TunnelProvider\u003e\n)\n```\n\nCheck the real example [here](https://codesandbox.io/s/p79k8w0jnq)\n\n### More complex example: building a Breadcrumb\n\nIt'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.\n\n```jsx\nconst Breadcrumbs = () =\u003e (\n  \u003cTunnelPlaceholder id=\"breadcrumb\" multiple\u003e\n    {({ items }) =\u003e (\n      items.map(({ children, href }) =\u003e (\n        \u003cspan\u003e\u003ca href={href}\u003e{children}\u003c/a\u003e\u003c/span\u003e\n      ))\n    )}\n  \u003c/TunnelPlaceholder\u003e\n)\n\nconst Breadcrumb = ({ children, url }) =\u003e (\n  \u003cTunnel id=\"breadcrumb\" href={url}\u003e\n    {children}\n  \u003c/Tunnel\u003e\n)\n\nrender(\n  \u003cTunnelProvider\u003e\n    {/* This will render the breadcrumb */}\n    \u003cBreadcrumbs /\u003e\n    {/* Somewhere else in children */}\n    \u003cBreadcrumb url=\"/products\"\u003eProducts\u003c/Breadcrumb\u003e\n    \u003cBreadcrumb url=\"/products/123\"\u003eProduct \u003cstrong\u003e123\u003c/strong\u003e\u003c/Breadcrumb\u003e\n  \u003c/TunnelProvider\u003e\n)\n```\n\nCheck the live example [here](https://codesandbox.io/s/0ym0n37jnl)\n\n## Similar Libraries\n\n- [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.\n- [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).\n\n## About\n\nThis 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.\n\n## License\n\nThis project is licensed under the terms of the [MIT license](https://github.com/javivelasco/react-tunnels/blob/master/LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjavivelasco%2Freact-tunnels","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjavivelasco%2Freact-tunnels","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjavivelasco%2Freact-tunnels/lists"}