Skip to content

Instantly share code, notes, and snippets.

@hamswaldenkv
Created October 6, 2018 07:22
Show Gist options
  • Save hamswaldenkv/0bd42334b5b8b66b37241b259a38b2d3 to your computer and use it in GitHub Desktop.
Save hamswaldenkv/0bd42334b5b8b66b37241b259a38b2d3 to your computer and use it in GitHub Desktop.
Simple react popup example
<div id="content"></div>
class Popup extends React.Component {
render() {
return (
<div className='popup'>
<div className='popup_inner'>
<h1>{this.props.text}</h1>
<button onClick={this.props.closePopup}>close me</button>
</div>
</div>
);
}
}
class App extends React.Component {
constructor() {
super();
this.state = {
showPopup: false
};
}
togglePopup() {
this.setState({
showPopup: !this.state.showPopup
});
}
render() {
return (
<div className='app'>
<h1>hihi</h1>
<button onClick={this.togglePopup.bind(this)}>show popup</button>
<button onClick={() => {alert('woooooooot?');}}>try me when popup is open</button>
<p>Ganz viel inhalt.<br />Ganz viel inhalt.<br />Ganz viel inhalt.<br />Ganz viel inhalt.<br />Ganz viel inhalt.<br />Ganz viel inhalt.<br />Ganz viel inhalt.<br />Ganz viel inhalt.<br />Ganz viel inhalt.<br />Ganz viel inhalt.<br />Ganz viel inhalt.<br />Ganz viel inhalt.<br />Ganz viel inhalt.<br />Ganz viel inhalt.<br />Ganz viel inhalt.<br />Ganz viel inhalt.<br />Ganz viel inhalt.<br />Ganz viel inhalt.<br />Ganz viel inhalt.<br />Ganz viel inhalt.<br />Ganz viel inhalt.<br />Ganz viel inhalt.<br />Ganz viel inhalt.<br />Ganz viel inhalt.<br />Ganz viel inhalt.<br />Ganz viel inhalt.<br />Ganz viel inhalt.<br />Ganz viel inhalt.<br /></p>
{this.state.showPopup ?
<Popup
text='Close Me'
closePopup={this.togglePopup.bind(this)}
/>
: null
}
</div>
);
}
};
ReactDOM.render(
<App />,
document.getElementById('content')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.min.js"></script>
h1 {
margin: 0;
padding: 0;
}
html, body, .app {
margin: 0;
padding: 0;
position: relative;
width: 100%;
height: 100vh;
}
.popup {
position: fixed;
width: 100%;
height: 100%;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
background-color: rgba(0,0,0, 0.5);
}
.popup_inner {
position: absolute;
left: 25%;
right: 25%;
top: 25%;
bottom: 25%;
margin: auto;
background: white;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment