Security News
vlt Debuts New JavaScript Package Manager and Serverless Registry at NodeConf EU
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
react-portal
Advanced tools
The react-portal package allows you to render components into a DOM node that exists outside the DOM hierarchy of the parent component. This is useful for creating modals, tooltips, and other UI elements that need to be rendered outside the main document flow.
Basic Portal Usage
This example demonstrates the basic usage of the react-portal package. It renders a div element outside the main DOM hierarchy, which can be useful for creating modals or tooltips.
import React from 'react';
import ReactDOM from 'react-dom';
import { Portal } from 'react-portal';
function App() {
return (
<div>
<h1>Main App</h1>
<Portal>
<div style={{ position: 'absolute', top: '50px', left: '50px', background: 'white', border: '1px solid black', padding: '10px' }}>
This is a portal content
</div>
</Portal>
</div>
);
}
export default App;
Custom Portal Target
This example shows how to use a custom DOM node as the target for the portal. The content will be rendered inside the specified custom target element.
import React, { useRef } from 'react';
import ReactDOM from 'react-dom';
import { Portal } from 'react-portal';
function App() {
const customTarget = useRef(null);
return (
<div>
<h1>Main App</h1>
<div ref={customTarget} id="custom-target" style={{ position: 'relative', height: '200px', border: '1px solid black' }}>
Custom Target
</div>
<Portal node={customTarget.current}>
<div style={{ position: 'absolute', top: '10px', left: '10px', background: 'white', border: '1px solid black', padding: '10px' }}>
This is a portal content inside custom target
</div>
</Portal>
</div>
);
}
export default App;
The react-dom package provides the createPortal function, which allows you to render children into a DOM node that exists outside the DOM hierarchy of the parent component. It is a part of the React core library and offers similar functionality to react-portal.
The react-reverse-portal package allows you to create portals that can be moved around in the DOM. It provides more advanced features compared to react-portal, such as the ability to move portals between different parts of the DOM dynamically.
The react-teleport package offers a simple API for rendering components outside the main DOM hierarchy. It is similar to react-portal but provides additional features like teleporting components to different parts of the DOM based on conditions.
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).
This is documentation for React-portal v4+ (currently beta). It works only with React v16+. For v3, please check this document. The final API can change. The test suite needs to be rebuilt.
<Portal />
and <PortalWithState />
so there is no compromise between flexibility and convenienceyarn add react react-dom react-portal@next
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. But what if you typically use React-portal just to open modals and you want to cut some boilerplate? In other words, you are ok with giving up some flexibility for convenience. Let React-portal handle its own state!
import { PortalWithState } from 'react-portal';
<PortalWithState closeOnOutsideClick closeOnEsc>
{({ openPortal, closePortal, isOpen, portal }) => [
!isOpen && <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>
)
]}
</PortalWithState>
Don't let this example to 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 an array since React v16 supports it! You can also return a single component. In that case, the example would be wrapped by a div as you were used to.
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 559,153 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
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
Security News
Research
The Socket Research Team uncovered a malicious Python package typosquatting the popular 'fabric' SSH library, silently exfiltrating AWS credentials from unsuspecting developers.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.