Security News
New Proposed CISA Mandate Would Require Critical Infrastructure to Report Ransom Payments Within 24 Hours
CISA has proposed a set of new rules that would require critical infrastructure to report cyber incidents and ransom payments.
react-portal
Advanced tools
To make your life with React Portals easier.
Weekly downloads
Readme
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.
<Portal />
and <PortalWithState />
so there is no compromise between flexibility and convenienceyarn add react react-dom react-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:
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.
<PortalWithState />
accepts this optional props:<Portal>
, you can target a custom DOM elementAlso notice, that the example returns a Fragment since React 16.2 supports it! You can also return:
key
attributeIf 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.
git clone https://github.com/tajo/react-portal
cd react-portal
yarn install
yarn build:examples
open examples/index.html
git clone https://github.com/tajo/react-portal
cd react-portal
yarn install
yarn build:examples --watch
open examples/index.html
yarn test
FAQs
To make your life with React Portals easier.
The npm package react-portal receives a total of 523,712 weekly downloads. As such, react-portal popularity was classified as popular.
We found that react-portal demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
CISA has proposed a set of new rules that would require critical infrastructure to report cyber incidents and ransom payments.
Security News
Redis is no longer OSS, breaking its explicit commitment to remain under the BSD 3-Clause License forever. This has angered contributors who are now working to fork the software.
Product
Socket AI now enables 'AI detected potential malware' alerts by default, ensuring users benefit from AI-powered state-of-the-art malware detection without needing to opt-in.