Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-useportal

Package Overview
Dependencies
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-useportal

⏱ React hook for Portals

  • 0.1.14
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
27K
increased by4.06%
Maintainers
1
Weekly downloads
 
Created
Source

usePortal

⏱ A React hook for using Portals

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

Installation

yarn add react react-dom react-useportal

Usage

Stateless

import usePortal from 'react-useportal'

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

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

With State

import usePortal from 'react-useportal'

const App = () => {
  const { openPortal, closePortal, Portal } = usePortal()
  return (
    <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>
      </Portal>
    </React.Fragment>
  )
}

Examples

  • Modal Codepen Example
  • Select Dropdown Codepen Example

Options

OptionDescription
closeOnOutsideClickThis will close the portal when not clicking within the portal. Default is true
closeOnEscThis will allow you to hit ESC and it will close the modal. Default is true
renderBelowClickedElementThis will put the portal right under the element that you click on. Great for dropdowns.
bindToThis is the DOM node you want to attach the portal to. By default it attaches to document.body
isOpenThis will be the default for the portal. Default is false

Option Usage

const {
  openPortal,
  closePortal,
  togglePortal,
  isOpen,
  Portal
} = usePortal({
  closeOnOutsideClick: true,
  closeOnEsc: true,
  renderBelowClickedElement, // appear directly under the clicked element/node in the DOM
  bindTo, // attach the portal to this node in the DOM
  isOpen: false,
})

FAQs

Package last updated on 20 Feb 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc