react-create-portal
TypeScript icon, indicating that this package has built-in type declarations

0.5.3 • Public • Published

React Create Portal

Build Status npm version coverage

Render elements through a Portal, give you more control over your UI, e.g. rendering a button into footer inside your deeply nested component

Principles

  • Declarative
  • Start quickly
  • Reasonable
  • Test friendly

Prerequisites

  • New React Context API
  • ReactDOM Portal

Get Started

npm install react-create-portal

or

yarn add react-create-portal

Examples

  1. render different contents based on current route
// App.js
import React from "react"
import { PortalProvider, createPortal } from "react-create-portal"

const [
  Slot, // place Slot at where elements render
  Render, // Render's children will be portaled into Slot
] = createPortal()

const App = () => (
  <PortalProvider>
    <div>
      Hello <Slot />!
    </div>
    <Router>
      <Route path="/world">
        <Render>World</Render>
      </Route>
      <Route path="/react">
        <Render>React</Render>
      </Route>
    </Router>
  </PortalProvider>
)

/** output dom
 * <div>Hello World!</div> // /world renders "Hello World!"
 * <div>Hello React!</div> // /react renders "Hello React!"
 */
  1. fallback UI when no Render rendered
// App.js
const App = () => (
  <PortalProvider>
    <div>
      Hello <Slot fallback="World" />!
    </div>
    <Router>
      <Route path="/world">
        <div>World Page</div>
      </Route>
      <Route path="/web">
        <div>Web Page</div>
      </Route>
      <Route path="/react">
        <Render>React</Render>
      </Route>
    </Router>
  </PortalProvider>
)

/** output dom
 * <div>Hello World!</div> // /world renders "Hello World!"
 * <div>Hello World!</div> // /web renders "Hello World!"
 * <div>Hello React!</div> // /react renders "Hello React!"
 */

Todos

  • [x] bundler
  • [x] static type
  • [x] test
  • [x] PortalProvider
  • [x] createPortal
  • [x] CI
  • [ ] Breadcrumbs
  • [ ] docs
  • [x] examples
  • [ ] publish

Readme

Keywords

none

Package Sidebar

Install

npm i react-create-portal

Weekly Downloads

81

Version

0.5.3

License

MIT

Unpacked Size

25.7 kB

Total Files

27

Last publish

Collaborators

  • levinqdl