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-portalgun

v1.0.0

Published

React modals made easy

Downloads

9

Readme

gzip size npm license

Why

Most apps need some type of modals, alerts, whatever global system. Creating those entities is not hard, right? After all, everything in React is a component. However, choosing how and where to render them can be a whole different story.

Let's think about modals for example. The most easy way of showing modals would be render them straight where they are needed, and use React state API to handle whether they are open or not. Will work? Yes. Would it be recommended? Well... no. You just cannot open the same modal elsewhere without copying a bunch of logic.

Another alternative would be using a state management tool to tackle this issue, such as Redux or MobX. There are many good ways of doing this, but using them just to handle these portals can turn up into adding significant boilerplate to your project.

For those scenarios react-portalgun is what you need!

Installation

Add react-portalgun to your project:

npm i react-portalgun --save
#or
yarn add react-portalgun

The gist

First, you'll need to globaly define what portals you want to handle:

import { PortalProvider } from 'react-portalgun';

const PORTAL_COMPONENTS = {
  TEST_MODAL: Modal,
};

const App = () => (
  <PortalProvider portals={PORTAL_COMPONENTS}>
    // You app implementation
  </PortalProvider>
);

Then, anywhere in your app hierarchy tree where a portal needs to be open just use the withPortal HOC that will handle everything for you:

import { withPortal } from 'react-portalgun';;

const Button = ({ onClick, children }) => (
  <button onClick={onClick}>{children}</button>
);

export default withPortal('TEST_MODAL', 'onClick')(Button);

That's it!

Demos

Check out the examples section to grasp what you can easily achieve using react-portalgun

API

<PortalProvider />

import React from 'react';
import { PortalProvider } from 'react-portalgun';
import Modal from './Modal';

const PORTAL_COMPONENTS = {
  TEST_MODAL: Modal,
};

const App = () => (
  <PortalProvider portals={PORTAL_COMPONENTS}>
    // Your app implementation
  </PortalProvider>
);

export default App;

PortalProvider is a wrapper component that makes portals (virtually any valid React component) accessible throughout your app.

This component should be defined only once in your application, in the part of the tree where you want your modals or alerts to show up (usually near the body element of your document).

All valid portals are going to be referenced by a unique string key. This key is going to be used by any child component who wants to trigger that portal to show.

PortalProvider props

portals: object required

Portals definitions. Must be a valid JS object that contains each possible component you want to make accessible in your app.

Automatically, each portal will then recieve an onClose function that can call to close itself. For more details please refer to the examples section.

withModal(modalType, [actionName], [mapPropsToPortal])

import { withPortal } from 'react-portalgun';;

const List = ({ items, onItemClick }) => (
  <ul>
    {items.map(item => (
       <li key={item.id} onClick={() => onItemClick(item)}>
         {item.content}
       </li>
    )}
  </ul>
);

export default withPortal('TEST_MODAL', 'onItemClick')(Button);

Higher order component to bind a component with a particular portal. This HOC will inject a callback function that the component can use to open the portal. This callback recieves an object as first parameter, which will be destructured and passed down as props to the portal (in the snippet above, the modal will recieve the destructured item).

withModal props

portalName: string required

Name of the portal yo want to bind to the component. Must match any of the portals defined on the PortalProvider

actionName: string default: 'showPortal'

Name of the prop in which the portal callback will be passed down to the wrapped component.

mapPropsToPortal: ({ event, ownProps }) => object

Used for a more fine-grained control on what and how props are passed to the portal. Returns an object with the desired props as keys.

Inspiration

This library was mostly inspired by this great post by @gaearon about how to handle modals in a React-Redux solution.

License

MIT © Diego Muracciole