.overlay {
position: fixed;
top: 50%;
left: 50%;
width: 260px;
line-height: 200px;
height: 200px;
margin-left: -130px;
margin-top: -100px;
background-color: #fff;
text-align: center;
outline: 9999px solid rgba(0, 0, 0, 0.5);
}
.overlay div {
display: inline;
}
<!doctype html>
<html>
<head>
<script src="https://unpkg.com/react@16.0.0/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16.0.0/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6.26.0/babel.min.js"></script>
<link rel="stylesheet" href="index.css">
</head>
<body>
<div id="root"></div>
<div id="overlay-root">
</div>
<script type="text/jsx" src="script.jsx"></script>
</body>
</html>
//import React, { Component } from 'react';
//import ReactDOM from 'react-dom';
//import './index.css';
class Overlay extends React.Component {
constructor(props) {
super(props);
this.overlayContainer = document.getElementById('overlay-root');
}
componentWillUnmount() {
document.body.removeChild(this.overlayContainer);
}
render() {
return ReactDOM.createPortal(
<div className="overlay">
<span onClick={this.props.onClose}>x</span>
{this.props.children}
</div>,
this.overlayContainer
);
}
}
class App extends React.Component {
constructor(props) {
super(props);
this.state = { overlayActive: true };
}
closeOverlay = () => {
this.setState({ overlayActive: false });
};
render() {
return (
<div>
<h1>Dashboard</h1>
{this.state.overlayActive &&
<Overlay onClose={this.closeOverlay}>
<div>Welcome</div>
</Overlay>}
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('root'));