<div id="root"></div>
<div id="portal"></div>
html {
--red: #bc442f;
--green: #007b7d;
--green: #007b7d;
--yellow: #ffc978;
}
body {
background: var(--yellow);
}
h1 {
margin: 0;
color: var(--yellow);
}
body,
html {
padding: 0;
margin: 0;
}
header {
background: var(--red);
padding: 16px;
text-align: center;
display: flex;
align-items: center;
justify-content: space-around;
h1 {
color: var(--yellow);
}
}
.header__call-to-action {
z-index: 2;
border: none;
text-transform: uppercase;
padding: 12px;
border-radius: 4px;
color: #f2f2f6;
font-weight: bold;
background: var(--green);
}
.modal {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
z-index: 1;
display: flex;
align-items: center;
justify-content: center;
background: hsla(0, 0%, 0%, 0.42);
}
.modal__body {
position: relative;
color: var(--yellow);
background: var(--green);
height: 90%;
max-width: 500px;
flex: 1;
margin: 0 auto;
padding: 16px;
box-sizing: border-box;
}
.modal__close-button {
position: absolute;
top: -20px;
right: -20px;
width: 50px;
border: none;
height: 50px;
font-size: 24px;
border-radius: 100%;
}
.modal__title {
font-size: calc(36 / 16 * 1rem);
margin: 0;
top: 0;
left: 16px;
}
.posts {
display: grid;
max-width: 600px;
grid-template-columns: repeat(auto-fill, minmax(min(150px, 100%), 1fr));
gap: 20px;
margin: 30px auto;
position: relative;
z-index: 1;
&__item {
background: white;
font-size: calc(24 / 16 * 1rem);
height: 150px;
border-radius: 6px;
display: flex;
align-items: center;
justify-content: center;
}
}
#root{
isolation:isolate;
}
View Compiled
const portalRoot = document.getElementById("portal");
class Portal extends React.Component {
constructor() {
super();
this.el = document.createElement("div");
}
componentDidMount = () => {
portalRoot.appendChild(this.el);
};
componentWillUnmount = () => {
portalRoot.removeChild(this.el);
};
render() {
const { children } = this.props;
return ReactDOM.createPortal(children, this.el);
}
}
class App extends React.Component {
state = {
on: false
};
toggle = () => {
this.setState({
on: !this.state.on
});
};
render() {
const { on } = this.state;
return (
<div>
<header>
<h1 class="header__title">Labzindex</h1>
<button class="header__call-to-action">call us</button>
</header>
<div class="posts-wraper">
<div class="posts">
<article class="posts__item">Post 1</article>
<article class="posts__item">Post 2</article>
<article class="posts__item">Post 3</article>
<article class="posts__item">Post 4</article>
</div>
</div>
<Portal>
<div class="modal">
<div class="modal__body">
<h2 class="modal__title">Modal Title</h2>
<button class="modal__close-button">x</button>
</div>
</div>
</Portal>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById("root"));
View Compiled
This Pen doesn't use any external CSS resources.