Skip to content

Commit

Permalink
feat: create <Portal>
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Feb 13, 2018
1 parent a313f95 commit 6ea7435
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ React standard library &mdash; must-have toolbox for any React project.

- React [router](https://mailonline.github.io/libreact/en/routing.html), [sensors](https://mailonline.github.io/libreact/en/Sensors.html), [FaCC](https://mailonline.github.io/libreact/en/Introduction.html#facc)s, [render props](https://mailonline.github.io/libreact/en/Introduction.html#render-props), [HOC](https://mailonline.github.io/libreact/en/Introduction.html#hoc)s, [context](https://mailonline.github.io/libreact/en/Context.html) providers, [dummies](https://mailonline.github.io/libreact/en/Dummies.html), and [other goodies](https://mailonline.github.io/libreact/en/).
- *Isomorphic* - all components work in browser and on server (and some in `react-native`).
- See [demos](https://mailonline.github.io/libreact/demos/), [docs](https://mailonline.github.io/libreact/en/), and NPM [package](https://www.npmjs.com/package/libreact/).
- See [__demos__](https://mailonline.github.io/libreact/demos/), [__docs__](https://mailonline.github.io/libreact/en/), and NPM [__package__](https://www.npmjs.com/package/libreact/).


## Installation
Expand Down
52 changes: 52 additions & 0 deletions src/Portal/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {Component} from 'react';
import {createPortal} from 'react-dom';
import {isClient} from '../util';

export interface IPortalProps {
children?: any;
el?: HTMLElement;
}

export interface IPortalState {
}

export class Portal extends Component<IPortalProps, IPortalState> {
el: HTMLElement = null;

constructor (props, context) {
super(props, context);

if (isClient && !props.el) {
this.createEl();
}
}

componentWillUpdate (props) {
if (props.el && !this.el) {
this.createEl();
}
}

componentWillUnmount () {
if (this.el) {
document.body.removeChild(this.el);
}

this.el = null;
}

createEl () {
this.el = document.createElement('div');
document.body.appendChild(this.el);
}

render () {
if (!isClient) {
return null;
}

const {el, children} = this.props;

return createPortal(children, el || this.el);
}
}

0 comments on commit 6ea7435

Please sign in to comment.