All Projects → javivelasco → React Tunnels

javivelasco / React Tunnels

Licence: mit
🚇 Render React components in placeholders that are placed somewhere else in the component tree.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Tunnels

Wuss Weapp
🐳wuss-weapp 一款高质量,组件齐全,高自定义的微信小程序UI组件库
Stars: ✭ 338 (-15.08%)
Mutual labels:  components
Loaf
Manages and displays breadcrumb trails in Rails app - lean & mean.
Stars: ✭ 360 (-9.55%)
Mutual labels:  breadcrumbs
Chaizi
漢語拆字字典
Stars: ✭ 384 (-3.52%)
Mutual labels:  components
Vue Goodshare
🍿 Vue.js component for social share. A simple way to share a link on the pages of your website in the most popular (and not so) social networks. Powered by goodshare.js project.
Stars: ✭ 345 (-13.32%)
Mutual labels:  components
Handycontrols
Contains some simple and commonly used WPF controls based on HandyControl
Stars: ✭ 347 (-12.81%)
Mutual labels:  components
Pixo
Convert SVG icons into React components
Stars: ✭ 371 (-6.78%)
Mutual labels:  components
Reference Browser
A full-featured browser reference implementation using Mozilla Android Components.
Stars: ✭ 337 (-15.33%)
Mutual labels:  components
Shards Vue
🌟Shards Vue is a free, beautiful and modern Vue.js UI kit based on Shards.
Stars: ✭ 390 (-2.01%)
Mutual labels:  components
Kendo Angular
Issue tracker - Kendo UI for Angular
Stars: ✭ 352 (-11.56%)
Mutual labels:  components
Windmill React Ui
🧩 The component library for fast and accessible development of gorgeous interfaces.
Stars: ✭ 373 (-6.28%)
Mutual labels:  components
Malvid
UI to help you build and document web components.
Stars: ✭ 347 (-12.81%)
Mutual labels:  components
Uiengine
Workbench for UI-driven development
Stars: ✭ 349 (-12.31%)
Mutual labels:  components
Alva
Create living prototypes with code components.
Stars: ✭ 3,734 (+838.19%)
Mutual labels:  components
Decompose
Kotlin Multiplatform lifecycle-aware business logic components (aka BLoCs) with routing functionality and pluggable UI (Jetpack Compose, SwiftUI, JS React, etc.)
Stars: ✭ 339 (-14.82%)
Mutual labels:  components
Mam mol
$mol - fastest reactive micro-modular compact flexible lazy ui web framework.
Stars: ✭ 385 (-3.27%)
Mutual labels:  components
Vonic
Mobile UI Components, based on Vue.js and ionic CSS. https://wangdahoo.github.io/vonic-documents
Stars: ✭ 3,422 (+759.8%)
Mutual labels:  components
Flynt
Component based WordPress starter theme, powered by ACF Pro and Timber, optimized for a11y and fast page load results.
Stars: ✭ 363 (-8.79%)
Mutual labels:  components
Story2sketch
Convert Storybook into Sketch symbols 💎
Stars: ✭ 391 (-1.76%)
Mutual labels:  components
Mosaic
🎨 A front-end JavaScript library for building user interfaces!
Stars: ✭ 390 (-2.01%)
Mutual labels:  components
React Uploady
Modern file uploading - components & hooks for React
Stars: ✭ 372 (-6.53%)
Mutual labels:  components

React Tunnels 🚇 npmBuild Status

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

Install

yarn add react-tunnels

Why

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.

Usage

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.

Simple example: children tunneling

Define a placeholder without any render function so it will render any children coming from a Tunnel component with the same id.

import { TunnelProvider, TunnelPlaceholder, Tunnel } from 'react-tunnels'

render(
  <TunnelProvider>
    <div>
      <TunnelPlaceholder id="my-tunnel" />
      <Tunnel id="my-tunnel">
        This will be rendered on the placeholder 👆
      </Tunnel>
    </div>
  </TunnelProvider>
)

Check the real example here

More complex example: building a Breadcrumb

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.

const Breadcrumbs = () => (
  <TunnelPlaceholder id="breadcrumb" multiple>
    {({ items }) => (
      items.map(({ children, href }) => (
        <span><a href={href}>{children}</a></span>
      ))
    )}
  </TunnelPlaceholder>
)

const Breadcrumb = ({ children, url }) => (
  <Tunnel id="breadcrumb" href={url}>
    {children}
  </Tunnel>
)

render(
  <TunnelProvider>
    {/* This will render the breadcrumb */}
    <Breadcrumbs />
    {/* Somewhere else in children */}
    <Breadcrumb url="/products">Products</Breadcrumb>
    <Breadcrumb url="/products/123">Product <strong>123</strong></Breadcrumb>
  </TunnelProvider>
)

Check the live example here

Similar Libraries

  • React Slot Fill: A similar project built by Cameron Westland 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.
  • Preact Slots: A library similar to React Slot Fill but for Preact developed by Jason Miller.

About

This project has been developed by Javi Velasco as a way to build Breadcrumb components and Layout customizations for a variety of React projects. Any feeback, help or improvements is highly appreciated.

License

This project is licensed under the terms of the MIT license.

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].