
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
react-portal-stateless
Advanced tools
React component for transportation of modals, lightboxes, loading bars... to document.body
This Project is a fork of React Portal that implement Stateless version of it.
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).
react-portal is being rewritten, you are reading v3.x.x documentation
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 {
constructor(props) {
super(props);
this.state = {
isPortalOpen: true
};
}
onClose() {
this.setState({
isPortalOpen: false
});
}
onOpen() {
this.setState({
isPortalOpen: true
});
}
render() {
return (
<div>
<button onClick={() => this.onOpen}>Open Portal</button>
<Portal isOpen={this.state.isPortalOpen} closeOnEsc closeOnOutsideClick onClose={() => this.onClose}>
<PseudoModal>
<h2>Pseudo Modal</h2>
<p>This react component is appended to the document body.</p>
</PseudoModal>
</Portal>
</div>
);
}
}
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!
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 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 isOpen
prop, so you can just set it to true
or false
.
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
Vojtech Miksu 2015, miksu.cz, @vmiksu
Victor Arêas 2017
FAQs
React component for transportation of modals, lightboxes, loading bars... to document.body
The npm package react-portal-stateless receives a total of 1,574 weekly downloads. As such, react-portal-stateless popularity was classified as popular.
We found that react-portal-stateless 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
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.