Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. import React, { Component } from "react";
  2. import PropTypes from "prop-types";
  3. import ReactDOM from "react-dom";
  4.  
  5. import {SplitPaneContext} from "../../context";
  6.  
  7. class ModalPortal extends Component {
  8. static contextType = SplitPaneContext;
  9.  
  10. constructor(props) {
  11. super(props);
  12.  
  13. this.modalRoot = null;
  14. this.el = document.createElement('div');
  15. }
  16.  
  17. componentDidMount() {
  18. this.modalRoot = document.getElementById(`modal-root-${this.context}`);
  19.  
  20. if (!this.modalRoot) {
  21. throw `No root element for pane '${this.context}' in ModalPortal`;
  22. }
  23. this.modalRoot.appendChild(this.el);
  24. }
  25.  
  26. componentWillUnmount() {
  27. this.modalRoot.removeChild(this.el);
  28. }
  29.  
  30. render() {
  31. return ReactDOM.createPortal(
  32. this.props.children,
  33. this.el,
  34. );
  35. }
  36. }
  37.  
  38. ModalPortal.propTypes = {
  39. children: PropTypes.node.isRequired
  40. };
  41.  
  42.  
  43. export default ModalPortal;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement