All Projects → tajo → React Portal

tajo / React Portal

Licence: mit
🎯 React component for transportation of modals, lightboxes, loading bars... to document.body or else.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Portal

React Useportal
🌀 React hook for Portals
Stars: ✭ 698 (-65.5%)
Mutual labels:  portal, modal, loading-bar
React Cool Portal
😎 🍒 React hook for Portals, which renders modals, dropdowns, tooltips etc. to <body> or else.
Stars: ✭ 458 (-77.36%)
Mutual labels:  portal, modal, loading-bar
React Native Loading Spinner Overlay
💈 React Native loading spinner overlay
Stars: ✭ 1,369 (-32.33%)
Mutual labels:  portal, modal
react-layer-stack
Layering system for React. Useful for popover/modals/tooltip/dnd application
Stars: ✭ 158 (-92.19%)
Mutual labels:  modal, portal
React Layer Stack
Layering system for React. Useful for popover/modals/tooltip/dnd application
Stars: ✭ 152 (-92.49%)
Mutual labels:  portal, modal
Vue Final Modal
🍕Vue Final Modal is a tiny, renderless, mobile-friendly, feature-rich modal component for Vue.js.
Stars: ✭ 128 (-93.67%)
Mutual labels:  modal
Popmodal
jquery plugin for showing tooltips, titles, modal dialogs and etc
Stars: ✭ 149 (-92.63%)
Mutual labels:  modal
React Native Bottom Sheet
A performant interactive bottom sheet with fully configurable options 🚀
Stars: ✭ 2,695 (+33.22%)
Mutual labels:  modal
React Relative Portal
React component for place dropdowns outside overflow: hidden; elements
Stars: ✭ 114 (-94.36%)
Mutual labels:  portal
Ax5ui Kernel
Javascript UI Framework - AX5UI - Kernel Module
Stars: ✭ 164 (-91.89%)
Mutual labels:  modal
Lightbox
A lightbox gallery plugin for Bootstrap
Stars: ✭ 1,866 (-7.76%)
Mutual labels:  modal
Vue Simple Portal
A simpler Portal implementation focussed on moving slot content to the end of the body element
Stars: ✭ 139 (-93.13%)
Mutual labels:  portal
Svelte Simple Modal
A simple, small, and content-agnostic modal for Svelte v3
Stars: ✭ 130 (-93.57%)
Mutual labels:  modal
React Native Modal Popover
React-Native pure JS popover that uses Modal
Stars: ✭ 151 (-92.54%)
Mutual labels:  modal
A11y Dialog
A very lightweight and flexible accessible modal dialog script.
Stars: ✭ 1,768 (-12.61%)
Mutual labels:  modal
React Native Portalize
The simplest way to render anything on top of the rest.
Stars: ✭ 157 (-92.24%)
Mutual labels:  portal
React Native Portal
A simplified portal implementation for ⭕️ React Native & Web ⭕️.
Stars: ✭ 112 (-94.46%)
Mutual labels:  portal
Djsemimodalviewcontroller
Simple semi modal presentation dialog with stacked content
Stars: ✭ 137 (-93.23%)
Mutual labels:  modal
Alertifyjs
A javascript framework for developing pretty browser dialogs and notifications.
Stars: ✭ 1,922 (-4.99%)
Mutual labels:  modal
Modal progress hud
A simple modal progress HUD (heads-up display, or progress indicator) for flutter
Stars: ✭ 137 (-93.23%)
Mutual labels:  modal

React-portal

npm version npm downloads Build Status

Struggling with modals, lightboxes or loading bars in React? React-portal creates a new top-level React tree and injects its children into it. That's necessary for proper styling (especially positioning).

Looking for v3 documentation? Go here.

Features

  • uses React v16 and its official API for creating portals
  • has a fallback for React v15
  • transports its children into a new React Portal which is appended by default to document.body
  • can target user specified DOM element
  • supports server-side rendering
  • supports returning arrays (no wrapper divs needed)
  • <Portal /> and <PortalWithState /> so there is no compromise between flexibility and convenience
  • doesn't produce any DOM mess
  • provides close on ESC and close on outside mouse click out of the box
  • no dependencies, minimalistic

Installation

yarn add react react-dom react-portal

Usage

Portal

import { Portal } from 'react-portal';

<Portal>
  This text is portaled at the end of document.body!
</Portal>

<Portal node={document && document.getElementById('san-francisco')}>
  This text is portaled into San Francisco!
</Portal>

That's it! Do you want to toggle portal? It's a plain React component, so you can simply do:

{isOpen && <Portal>Sometimes portaled?</Portal>}

This gives you absolute flexibility and control and I would recommend you to use it as a basic building block for your components like modals or notifications. This code also works with server-side rendering. If you think about just using official ReactDOM.createPortal(), you would have to check for existence of DOM environment.

React-portal used to come packed with some extra goodies because sometimes you are ok with giving up some flexibility for convenience. For that case, V4 introduces another component that handles its own state for you:

PortalWithState

import { PortalWithState } from 'react-portal';

<PortalWithState closeOnOutsideClick closeOnEsc>
  {({ openPortal, closePortal, isOpen, portal }) => (
    <React.Fragment>
      <button onClick={openPortal}>
        Open Portal
      </button>
      {portal(
        <p>
          This is more advanced Portal. It handles its own state.{' '}
          <button onClick={closePortal}>Close me!</button>, hit ESC or
          click outside of me.
        </p>
      )}
    </React.Fragment>
  )}
</PortalWithState>

Don't let this example intimidate you! PortalWithState expects one child, a function. This function gets a few parameters (mostly functions) and returns a React component.

There are 4 optional parameters:

  • openPortal - function that you can call to open the portal
  • closePortal - function that you can call to close the portal
  • portal - the part of component that should be portaled needs to be wrapped by this function
  • isOpen - boolean, tells you if portal is open/closed

<PortalWithState /> accepts this optional props:

  • node - same as <Portal>, you can target a custom DOM element
  • closeOnOutsideClick - boolean, portal closes when you click outside of it
  • closeOnEsc - boolean, portal closes when the ESC key is hit
  • defaultOpen - boolean, the starting state of portal is being open
  • onOpen - function, will get triggered after portal is open
  • onClose - function, will get triggered after portal is closed

Also notice, that the example returns a Fragment since React 16.2 supports it! You can also return:

  • an array - available from React v16, remember to add key attribute
  • regular component - the example would be wrapped by a div, not a fragment

If you start running into limits of <PortalWithState /> (complex animations), you probably want to use <Portal /> instead and build a component tailored to your specific taste.

Run Examples

git clone https://github.com/tajo/react-portal
cd react-portal
yarn install
yarn build:examples
open examples/index.html

Contributions Welcome!

git clone https://github.com/tajo/react-portal
cd react-portal
yarn install
yarn build:examples --watch
open examples/index.html

Run Tests

yarn test

Author

Vojtech Miksu 2017, miksu.cz, @vmiksu

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