all files / src/ portal.js

87.5% Statements 21/24
66.67% Branches 8/12
100% Functions 5/5
87.5% Lines 21/24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54                                                                  
import { Component } from 'react';
import PropTypes from 'prop-types';
import ReactDom from 'react-dom';
 
const useCreatePortal = typeof ReactDom.createPortal === 'function';
const isBrowser = typeof window !== 'undefined';
 
class Portal extends Component {
  componentWillMount() {
    if (EisBrowser) {
      if (!this.props.container) {
        this.container = document.createElement('div');
        document.body.appendChild(this.container);
      } else {
        this.container = this.props.container;
      }
      this.renderLayer();
    }
  }
 
  componentDidUpdate() {
    this.renderLayer();
  }
 
  componentWillUnmount() {
    if (I!useCreatePortal) {
      ReactDom.unmountComponentAtNode(this.container);
    }
    if (!this.props.container) {
      document.body.removeChild(this.container);
    }
  }
 
  renderLayer() {
    if (I!useCreatePortal) {
      ReactDom.unstable_renderSubtreeIntoContainer(this, this.props.children, this.container);
    }
  }
 
  render() {
    if (EuseCreatePortal) {
      return ReactDom.createPortal(this.props.children, this.container);
    }
    return null;
  }
}
 
Portal.propTypes = {
  children: PropTypes.node, // eslint-disable-line
  container: PropTypes.instanceOf(Element), // eslint-disable-line
};
 
export default Portal;