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
React component for transportation of modals, lightboxes, loading bars... to document.body
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 child into it. That's necessary for proper styling (especially positioning).
Try https://miksu.cz/react-portal or
git clone https://github.com/tajo/react-portal
cd react-portal
npm install
npm run build:examples
open examples/index.html
npm install react react-dom react-portal --save
import React from 'react';
import ReactDOM from 'react-dom';
import Portal from 'react-portal';
export default class App extends React.Component {
render() {
const button1 = <button>Open portal with pseudo modal</button>;
return (
<Portal closeOnEsc closeOnOutsideClick openByClickOn={button1}>
<PseudoModal>
<h2>Pseudo Modal</h2>
<p>This react component is appended to the document body.</p>
</PseudoModal>
</Portal>
);
}
}
export class PseudoModal extends React.Component {
render() {
return (
<div>
{this.props.children}
<p><button onClick={this.props.closePortal}>Close this</button></p>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('react-body'));
The portal expects one child (<Portal><Child ... /></Portal>
) that will be ported.
If true, the portal is open. If false, the portal is closed. It's up to you to take care of the closing (aka taking care of the state). Don't use this prop if you want to make your life easier. Use openByClickOn instead!
The second way how to open the portal. This element will be rendered by the portal immediately
with onClick
handler that triggers portal opening. How to close the portal then? The portal provides its ported child with a callback this.props.closePortal
. Or you can use built-in portal closing methods (closeOnEsc, ... more below). Notice that you don't have to deal with the open/close state (like when using the isOpened
prop).
If true, the portal can be closed by the key ESC.
If true, the portal can be closed by the outside mouse click.
This callback is called when the portal is opened and rendered (useful for animating the DOMNode).
This callback is called when the closing event is triggered but it prevents normal removal from the DOM. So, you can do some DOMNode animation first and then call removeFromDOM()
that removes the portal from DOM.
If you'd like to execute some async logic in beforeClose, you should pass isUnmounted:boolean
to removeFromDom(isUnmounted)
.
This callback is called when the portal closes and after beforeClose.
This callback is called when the portal is (re)rendered.
closeOnOutsideClick
doesn't work? There is a simple solution.<LevelTwo />
<Portal>
<LevelOne>
<LevelTwo />
</LevelOne>
</Portal>
also need an access to this.props.closePortal()
? You can't just use {this.props.children}
in render method of <LevelOne>
component. You have to clone it instead:
{React.cloneElement(
this.props.children,
{closePortal: this.props.closePortal}
)}
Sometimes you need to open your portal (e.g. modal) automatically. There is no button to click on. No problem, because the portal has the isOpened
prop, so you can just set it to true
or false
. However, then it's completely up to you to take care about the portal closing (ESC, outside click, no this.props.closePortal
callback...).
However, there is a nice trick how to make this happen even without isOpened
:
<Portal ref="myPortal">
<PseudoModal title="My modal">
Modal content
</PseudoModal>
</Portal>
this.refs.myPortal.openPortal()
// opens the portal, yay!
Please, create issues and pull requests.
git clone https://github.com/tajo/react-portal
cd react-portal
npm install
npm start
open http://localhost:3000
Don't forget to run this before every commit:
npm test
Inspired by the talk React.js Conf 2015 - Hype!, Ryan Florence
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.