All Projects → diegomura → react-portalgun

diegomura / react-portalgun

Licence: other
Lightweight portal system for React. Mega seeds included 🔫

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to react-portalgun

vue-modal
Reusable Modal component, supports own custom HTML, text and classes.
Stars: ✭ 29 (-61.33%)
Mutual labels:  modal, modals
Django Admin Interface
django's default admin interface made customizable. popup windows replaced by modals. :mage: ⚡️
Stars: ✭ 717 (+856%)
Mutual labels:  modal, modals
Ngx Smart Modal
Modal/Dialog component crafted for Angular
Stars: ✭ 256 (+241.33%)
Mutual labels:  modal, modals
Jalert
jQuery alert/modal/lightbox plugin
Stars: ✭ 73 (-2.67%)
Mutual labels:  modal, modals
Modal
A powerful and customizable modal implementation for Blazor applications.
Stars: ✭ 406 (+441.33%)
Mutual labels:  modal, modals
Heyui
🎉UI Toolkit for Web, Vue2.0 http://www.heyui.top
Stars: ✭ 2,373 (+3064%)
Mutual labels:  modal, modals
HalfModal
HalfModal mimics the behavior of the drawer of Apple's shortcut application. It is realized using a combination of UIViewPropertyAnimator.
Stars: ✭ 24 (-68%)
Mutual labels:  modal
vue-thin-modal
Thin yet powerful modal component of Vue.js
Stars: ✭ 126 (+68%)
Mutual labels:  modal
jpopup
Simple lightweight (<2kB) javascript popup modal plugin
Stars: ✭ 27 (-64%)
Mutual labels:  modal
musicWebTemplate
Free website template built for musicians / artists to promote their music and connect to their audience.
Stars: ✭ 26 (-65.33%)
Mutual labels:  modal
react-zap
⚡ Zap props from one React component to another, using React new context API and your existing higher-order components ⚡
Stars: ✭ 17 (-77.33%)
Mutual labels:  higher-order-component
react-spring-bottom-sheet
Accessible ♿️, Delightful ✨, & Fast 🚀
Stars: ✭ 604 (+705.33%)
Mutual labels:  modal
react-table-hoc-draggable-columns
ReactTable HOC for draggable columns
Stars: ✭ 27 (-64%)
Mutual labels:  higher-order-component
react-redux-modals
This repo created for Medium.com: React/Redux: Modals and Dialogs
Stars: ✭ 30 (-60%)
Mutual labels:  modal
LSDialogViewController
Custom Dialog for iOS written in Swift
Stars: ✭ 74 (-1.33%)
Mutual labels:  modal
bootstrap-modal-ios
Bootstrap Modal for iOS
Stars: ✭ 20 (-73.33%)
Mutual labels:  modal
rrx
⚛️ Minimal React router using higher order components
Stars: ✭ 69 (-8%)
Mutual labels:  higher-order-component
sweet-modal-vue
The sweetest library to happen to modals.
Stars: ✭ 762 (+916%)
Mutual labels:  modal
react-native-modal-loader
Customizable animated modal progress hud for react apps.
Stars: ✭ 36 (-52%)
Mutual labels:  modal
SPLarkController
Custom transition between controllers. Settings controller for your iOS app.
Stars: ✭ 967 (+1189.33%)
Mutual labels:  modal

gzip size npm Travis 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.

withPortal(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).

withPortal 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

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