npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-portal-bridge

v0.0.5

Published

Inject components across microfrontends using React portals

Downloads

8

Readme

react-portal-bridge

🪄 Inject components across microfrontends using React portals

react-portal-bridge is a lightweight library that allows you to easily inject React components across different microfrontends. It uses React portals to provide a flexible way of rendering components in predefined zones.

Features

  • 🤏 Lightweight - less than 2kB
  • 🙆 Easy to integrate
  • 🧘 Flexible

Installation

pnpm add react-portal-bridge
npm install react-portal-bridge
yarn add react-portal-bridge

Note: This library depends on react and react-dom

Getting started

You need to add ReactPortalBridgeHost to your app first. The host is responsible for portalling components around. It does not render anything on its own.

import { ReactPortalBridgeHost } from 'react-portal-bridge'

const App = () => {
  return (
    <div>
      <ReactPortalBridgeHost />
    </div>
  );
};

Then, you need to render a zone. A zone is a place where components will be injected.

For example, you could add a zone to navbar and enable other microfrontends to render items there:

import { ReactPortalBridgeZone } from 'react-portal-bridge';

const Navbar = () => {
  return (
    <div>
      <ReactPortalBridgeZone id="RPBZ_NAVBAR" />
    </div>
  );
}

Now you will be able to inject components in the zone by using injectComponent:

import { injectComponent } from 'react-portal-bridge';

useEffect(() => {
  injectComponent({
    id: 'helloworld',
    component: <HelloWorld />,
    zone: 'RPBZ_NAVBAR',
  });
}, []);

Documentation

ReactPortalBridgeHost

The component that manages the portals. You can place this in the root of your application.

<ReactPortalBridgeHost />
ReactPortalBridgeZone

Define a zone where components can be injected.

<ReactPortalBridgeZone id="UNIQUE_ZONE_ID" />
  • id: string: A unique identifier for the zone. The library does not check for uniqueness and it will pick the first matching DOM element.
injectComponent

Injects a component in the specified zone.

injectComponent({
  id: 'unique-component-id',
  component: <YourComponent />,
  zone: 'RPBZ_ZONE_ID'
});
  • id: string: A unique identifier for the component
  • component: ReactNode: The React component to inject
  • zone: string: The id of the zone where the component should be injected

Contributing

Contributions are welcomed!

License

react-portal-bridge is released under the MIT license.