Created
April 4, 2016 18:52
-
-
Save miguelludert/185432a35e12076fc54265e23a1dd278 to your computer and use it in GitHub Desktop.
Quick modal for React
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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