Skip to content

Commit

Permalink
Refactor #1880 - Added Portal component
Browse files Browse the repository at this point in the history
  • Loading branch information
mertsincan committed Mar 17, 2021
1 parent 11d8420 commit bb31939
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/components/portal/Portal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Component } from 'react';
import PropTypes from 'prop-types';
import ReactDOM from 'react-dom';

export class Portal extends Component {

static defaultProps = {
element: null,
appendTo: null,
visible: false
};

static propTypes = {
element: PropTypes.any,
appendTo: PropTypes.any,
visible: PropTypes.bool
};

constructor(props) {
super(props);

this.state = {
mounted: props.visible
};
}

hasDOM() {
return !!(typeof window !== 'undefined' && window.document && window.document.createElement);
}

componentDidMount() {
if (this.hasDOM() && !this.state.mounted) {
this.setState({ mounted: true });
}
}

render() {
return this.props.element && this.state.mounted ? ReactDOM.createPortal(this.props.element, this.props.appendTo || document.body) : null;
}
}

0 comments on commit bb31939

Please sign in to comment.