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

@mutabazia/react-portal

v1.0.7

Published

React Context-based Portal

Downloads

4

Readme

@mutabazia/react-portal

React Context-based Portal

This library provides a set of React components for managing portals. It includes Portal, Portal.Context, Portal.Provider, and Portal.Placeholder.

Installation

You can install the library with npm:

npm install --save @mutabazia/react-portal

Or with yarn:

yarn add @mutabazia/react-portal

Usage

First, wrap your app with the Portal.Provider.

import Portal from '@mutabazia/react-portal';

function App() {
  return (
    <Portal.Provider>
      {/* your app code */}
    </Portal.Provider>
  );
}

export default App;

Next, you can create a Portal.Placeholder and a corresponding Portal anywhere within your app. The Portal.Placeholder component serves as a placeholder for where you want to insert the portal content. The Portal component is where you define the content to be rendered in the portal.

import Portal from '@mutabazia/react-portal';

function Example() {
  return (
    <div>
      <Portal.Placeholder id="myPortal" />

      <Portal id="myPortal">
        <div>This will be rendered in the portal!</div>
      </Portal>
    </div>
  );
}

export default Example;

In the above example, the div within Portal will be rendered inside the Portal.Placeholder with the same id ("myPortal").

You can also customize how the portal content is rendered using the renderItem prop. This function takes the portal content as its argument and should return the customized content.

Here's an example that uses renderItem to wrap the portal content with a ul tag:

import Portal from '@mutabazia/react-portal';

function ListPortalExample() {
  return (
    <div>
      <Portal.Placeholder id="listPortal" renderItem={(items) => <ul>{items}</ul>} />

      <Portal id="listPortal">
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
      </Portal>
    </div>
  );
}

export default ListPortalExample;

In this example, the Portal.Placeholder component with the id of "listPortal" is using a renderItem function that wraps the portal content with a ul tag. The Portal with the same id provides the list items (li tags). This results in a rendered list where the li items from Portal are contained within a ul in the Portal.Placeholder.

API

Portal.Provider

This component creates the portal context. Wrap your entire app with this component.

Portal.Placeholder

This component serves as a portal target. It takes an id prop which is used to identify the portal.

Props:

  • id: A unique identifier for the portal.
  • renderItem (optional): A function to customize how the portal content is rendered.
  • className (optional): A string to customize the placeholder's className

Portal

This component is used to render content into a portal. It requires an id prop which should match the id of the target Portal.Placeholder.

Props:

  • id: The identifier of the target portal.
  • children: The content to be rendered into the portal.

Contributing

Contributions are welcome. Please open an issue or submit a pull request on GitHub.