Skip to content

Instantly share code, notes, and snippets.

@miguelludert
Created April 4, 2016 18:52
Show Gist options
  • Save miguelludert/185432a35e12076fc54265e23a1dd278 to your computer and use it in GitHub Desktop.
Save miguelludert/185432a35e12076fc54265e23a1dd278 to your computer and use it in GitHub Desktop.
Quick modal for React
var React = require('react');
var ReactDOM = require('react-dom');
var document = global.document;
module.exports = React.createClass({
componentDidMount : function() {
this.node = document.getElementById('component-modal-overlay');
if(!this.node){
this.node = document.createElement("div");
this.node.id = 'component-modal-overlay'
this.node.style.display = 'none';
this.node.addEventListener('click', this.onOverlayClick);
document.body.appendChild(this.node);
}
this.renderContent();
},
onOverlayClick : function(e) {
if(e.target === this.node && this.onOverlayClick){
this.props.onOverlayClick();
}
},
componentDidUpdate : function() {
this.renderContent();
},
renderContent : function(){
this.node.style.display = this.props.open ? '' : 'none';
this.node.innerHTML = '';
if(this.props.open) {
ReactDOM.render(<div>
{this.props.children}
</div>, this.node);
}
},
render : function(){
return null;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment