Skip to content

Instantly share code, notes, and snippets.

@eudoxia0
Created July 28, 2016 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eudoxia0/4e540c48d62177a16c158810a3ae95b2 to your computer and use it in GitHub Desktop.
Save eudoxia0/4e540c48d62177a16c158810a3ae95b2 to your computer and use it in GitHub Desktop.
React modal component
var App = React.createClass({
getInitialState: function() {
return { isModalOpen: false };
},
openModal: function() {
this.setState({ isModalOpen: true });
},
closeModal: function() {
this.setState({ isModalOpen: false });
},
render: function() {
return (
<div className="app">
<h1>App</h1>
<button onClick={this.openModal}>Open modal</button>
<Modal isOpen={this.state.isModalOpen}
transitionName="modal-anim">
<h3>My Modal</h3>
<div className="body">
<p>This is the modal&apos;s body.</p>
</div>
<button onClick={this.closeModal}>Close modal</button>
</Modal>
</div>
);
}
});
.modal-anim-enter {
opacity: 0.00;
transform: scale(0.7);
transition: all 0.2s;
}
.modal-anim-enter.modal-anim-enter-active {
opacity: 1;
transform: scale(1);
}
var ReactCSSTransitionGroup = React.addons.CSSTransitionGroup;
.modal-anim-leave {
opacity: 1;
transform: scale(1);
transition: all 0.2s;
}
.modal-anim-leave.modal-anim-leave-active {
opacity: 0.00;
transform: scale(0.7);
}
var Modal = React.createClass({
render: function() {
if(this.props.isOpen){
return (
<ReactCSSTransitionGroup transitionName={this.props.transitionName}>
<div className="modal">
{this.props.children}
</div>
</ReactCSSTransitionGroup>
);
} else {
return <ReactCSSTransitionGroup transitionName={this.props.transitionName} />;
}
}
});
React.render(
<App/>,
document.body
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment