All Projects → alex-cory → React Useportal

alex-cory / React Useportal

Licence: mit
🌀 React hook for Portals

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to React Useportal

React Cool Portal
😎 🍒 React hook for Portals, which renders modals, dropdowns, tooltips etc. to <body> or else.
Stars: ✭ 458 (-34.38%)
Mutual labels:  portal, dialog, modal, dropdown, notification, tooltip, lightbox, loading-bar
react-layer-stack
Layering system for React. Useful for popover/modals/tooltip/dnd application
Stars: ✭ 158 (-77.36%)
Mutual labels:  modal, lightbox, tooltip, portal, dropdown
React Layer Stack
Layering system for React. Useful for popover/modals/tooltip/dnd application
Stars: ✭ 152 (-78.22%)
Mutual labels:  portal, modal, dropdown, tooltip, lightbox
Jbox
jBox is a jQuery plugin that makes it easy to create customizable tooltips, modal windows, image galleries and more.
Stars: ✭ 1,251 (+79.23%)
Mutual labels:  dialog, modal, tooltip, lightbox
Simplelightbox
Touch-friendly image lightbox for mobile and desktop
Stars: ✭ 744 (+6.59%)
Mutual labels:  dialog, modal, lightbox
Ng Bootstrap
Angular powered Bootstrap
Stars: ✭ 7,872 (+1027.79%)
Mutual labels:  modal, dropdown, tooltip
React Portal
🎯 React component for transportation of modals, lightboxes, loading bars... to document.body or else.
Stars: ✭ 2,023 (+189.83%)
Mutual labels:  portal, modal, loading-bar
toppy
Overlay library for Angular 7+
Stars: ✭ 81 (-88.4%)
Mutual labels:  modal, tooltip, dropdown
MultiDialog
MultiDialog utilizes jQuery UI Dialog Widget for a full featured Modalbox / Lightbox application.
Stars: ✭ 23 (-96.7%)
Mutual labels:  modal, lightbox, dialog
Lightcase
The smart and flexible Lightbox Plugin.
Stars: ✭ 413 (-40.83%)
Mutual labels:  modal, lightbox
Ngx Gallery
Angular Gallery, Carousel and Lightbox
Stars: ✭ 417 (-40.26%)
Mutual labels:  modal, lightbox
Mediumlightbox
Nice and elegant way to add zooming functionality for images, inspired by medium.com
Stars: ✭ 671 (-3.87%)
Mutual labels:  modal, lightbox
React Native Actions Sheet
A Cross Platform(Android & iOS) ActionSheet with a flexible api, native performance and zero dependency code for react native. Create anything you want inside ActionSheet.
Stars: ✭ 412 (-40.97%)
Mutual labels:  dialog, modal
Popupdialog
A simple, customizable popup dialog for iOS written in Swift. Replaces UIAlertController alert style.
Stars: ✭ 3,709 (+431.38%)
Mutual labels:  dialog, modal
Gijgo
Gijgo - Free Javascript Controls
Stars: ✭ 424 (-39.26%)
Mutual labels:  dialog, modal
Popper Core
🍿 JavaScript positioning library for tooltips, popovers, dropdowns, and more
Stars: ✭ 18,903 (+2608.17%)
Mutual labels:  dropdown, tooltip
Android Simple Tooltip
A simple library based on PopupWindow to create Tooltips on Android. 💚
Stars: ✭ 622 (-10.89%)
Mutual labels:  dialog, tooltip
Vodal
A Vue modal with animations.
Stars: ✭ 312 (-55.3%)
Mutual labels:  dialog, modal
Sppermissions
Ask permissions with ready-use interface. You can check status permission and if it has been requested before. Support SwiftUI.
Stars: ✭ 4,701 (+573.5%)
Mutual labels:  dialog, notification
Ngx Modialog
Modal / Dialog for Angular
Stars: ✭ 691 (-1%)
Mutual labels:  dialog, modal

usePortal

🌀 React hook for using Portals

undefined undefined undefined Known Vulnerabilities Known Vulnerabilities

Need to make dropdowns, lightboxes/modals/dialogs, global message notifications, or tooltips in React? React Portals provide a first-class way to render children into a DOM node that exists outside the DOM hierarchy of the parent component (react docs).

This hook is also isomorphic, meaning it works with SSR (server side rendering).

Features

  • SSR (server side rendering) support
  • TypeScript support
  • 1 dependency (use-ssr)
  • Built in state

Examples

Installation

yarn add react-useportal      or     npm i -S react-useportal

Usage

Stateless

import usePortal from 'react-useportal'

const App = () => {
  const { Portal } = usePortal()

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

const App = () => {
  const { Portal } = usePortal({
    bindTo: document && document.getElementById('san-francisco')
  })

  return (
    <Portal>
      This text is portaled into San Francisco!
    </Portal>
  )
}

With State

import usePortal from 'react-useportal'

const App = () => {
  var { openPortal, closePortal, isOpen, Portal } = usePortal()

  // want to use array destructuring? You can do that too
  var [openPortal, closePortal, isOpen, Portal] = usePortal()

  return (
    <>
      <button onClick={openPortal}>
        Open Portal
      </button>
      {isOpen && (
        <Portal>
          <p>
            This Portal handles its own state.{' '}
            <button onClick={closePortal}>Close me!</button>, hit ESC or
            click outside of me.
          </p>
        </Portal>
      )}
    </>
  )
}

Need Animations?

import usePortal from 'react-useportal'

const App = () => {
  const { openPortal, closePortal, isOpen, Portal } = usePortal()
  return (
    <>
      <button onClick={openPortal}>
        Open Portal
      </button>
      <Portal>
        <p className={isOpen ? 'animateIn' : 'animateOut'}>
          This Portal handles its own state.{' '}
          <button onClick={closePortal}>Close me!</button>, hit ESC or
          click outside of me.
        </p>
      </Portal>
    </>
  )
}

Customizing the Portal directly

By using onOpen, onClose or any other event handler, you can modify the Portal and return it. See useDropdown for a working example. It's important that you pass the event object to openPortal and togglePortal otherwise you will need to attach a ref to the clicked element.

const useModal = () => {
  const { isOpen, openPortal, togglePortal, closePortal, Portal } = usePortal({
    onOpen({ portal }) {
      portal.current.style.cssText = `
        /* add your css here for the Portal */
        position: fixed;
        left: 50%;
        top: 50%;
        transform: translate(-50%,-50%);
        z-index: 1000;
      `
    }
  })

  return {
    Modal: Portal,
    openModal: openPortal,
    toggleModal: togglePortal,
    closeModal: closePortal,
    isOpen
  }
}

const App = () => {
  const { openModal, closeModal, isOpen, Modal } = useModal()
  
  return <>
    <button onClick={e => openModal(e)}>Open Modal<button>
    {isOpen && (
      <Modal>
        This will dynamically center to the middle of the screen regardless of the size of what you put in here
      </Modal>
    )}
  </>
}

Make sure you are passing the html synthetic event to the openPortal and togglePortal . i.e. onClick={e => openPortal(e)}

Usage with a ref

If for some reason, you don't want to pass around the event to openPortal or togglePortal, you can use a ref like this.

import usePortal from 'react-useportal'

const App = () => {
  var { ref, openPortal, closePortal, isOpen, Portal } = usePortal()

  return (
    <>
      {/* see below how I don't have to pass the event if I use the ref */}
      <button ref={ref} onClick={() => openPortal()}>
        Open Portal
      </button>
      {isOpen && (
        <Portal>
          <p>
            This Portal handles its own state.{' '}
            <button onClick={closePortal}>Close me!</button>, hit ESC or
            click outside of me.
          </p>
        </Portal>
      )}
    </>
  )
}

Options

Option Description
closeOnOutsideClick This will close the portal when not clicking within the portal. Default is true
closeOnEsc This will allow you to hit ESC and it will close the modal. Default is true
bindTo This is the DOM node you want to attach the portal to. By default it attaches to document.body
isOpen This will be the default for the portal. Default is false
onOpen This is used to call something when the portal is opened and to modify the css of the portal directly
onClose This is used to call something when the portal is closed and to modify the css of the portal directly
onPortalClick This is fired whenever clicking on the Portal
html event handlers (i.e. onClick) These can be used instead of onOpen to modify the css of the portal directly. onMouseEnter and onMouseLeave example

Option Usage

const {
  openPortal,
  closePortal,
  togglePortal,
  isOpen,
  Portal,
  // if you don't pass an event to openPortal, closePortal, or togglePortal, you will need
  // to put this on the element you want to interact with/click
  ref,
  // if for some reason you want to interact directly with the portal, you can with this ref
  portalRef,
} = usePortal({
  closeOnOutsideClick: true,
  closeOnEsc: true,
  bindTo, // attach the portal to this node in the DOM
  isOpen: false,
  // `event` has all the fields that a normal `event` would have such as `event.target.value`, etc.
  // with the additional `portal` and `targetEl` added to it as seen in the examples below
  onOpen: (event) => {
    // can access: event.portal, event.targetEl, event.event, event.target, etc.
  },
  // `onClose` will not have an `event` unless you pass an `event` to `closePortal`
  onClose({ portal, targetEl, event }) {},
  // `targetEl` is the element that you either are attaching a `ref` to
  // or that you are putting `openPortal` or `togglePortal` or `closePortal` on
  onPortalClick({ portal, targetEl, event }) {},
  // in addition, any event handler such as onClick, onMouseOver, etc will be handled the same
  onClick({ portal, targetEl, event }) {}
})

Todos

  • [ ] React Native support. 1 2 3 4 5 Probably going to have to add a Provider...
  • [ ] add correct typescript return types
  • [ ] add support for popup windows resource 1 resource 2. Maybe something like
  const { openPortal, closePortal, isOpen, Portal } = usePortal({
    popup: ['', '', 'width=600,height=400,left=200,top=200']
  })
  // window.open('', '', 'width=600,height=400,left=200,top=200')
  • [ ] tests (priority)
  • [ ] maybe have a <Provider order={['Portal', 'openPortal']} /> then you can change the order of the array destructuring syntax
  • [ ] fix code so maintainability is A
  • [ ] set up code climate test coverage
  • [ ] optimize badges see awesome badge list
    • [ ] add code climate test coverage badge
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].