1 | var React = require('react');
|
2 |
|
3 | var SkyLight = React.createClass({
|
4 | propTypes: {
|
5 | title: React.PropTypes.string,
|
6 | showOverlay: React.PropTypes.bool,
|
7 | beforeOpen: React.PropTypes.func,
|
8 | afterOpen: React.PropTypes.func,
|
9 | beforeClose: React.PropTypes.func,
|
10 | afterClose: React.PropTypes.func
|
11 | },
|
12 | getDefaultProps: function () {
|
13 | return {
|
14 | title: '',
|
15 | showOverlay: true
|
16 | }
|
17 | },
|
18 | getInitialState: function () {
|
19 | return {
|
20 | isVisible: false
|
21 | };
|
22 | },
|
23 | show: function () {
|
24 | this.setState({isVisible: true});
|
25 | },
|
26 | hide: function () {
|
27 | this.setState({isVisible: false});
|
28 | },
|
29 | componentWillUpdate: function (nextProps, nextState) {
|
30 | if (nextState.isVisible && this.props.beforeOpen) {
|
31 | this.props.beforeOpen();
|
32 | }
|
33 |
|
34 | if (!nextState.isVisible && this.props.beforeClose) {
|
35 | this.props.beforeClose();
|
36 | }
|
37 | },
|
38 | componentDidUpdate: function (prevProps, prevState) {
|
39 | if (!prevState.isVisible && this.props.afterOpen) {
|
40 | this.props.afterOpen();
|
41 | }
|
42 |
|
43 | if (prevState.isVisible && this.props.afterClose) {
|
44 | this.props.afterClose();
|
45 | }
|
46 | },
|
47 | render: function () {
|
48 |
|
49 | var overlay;
|
50 | var displayStyle = this.state.isVisible ? {display: 'block'} : {display: 'none'};
|
51 |
|
52 | if (this.props.showOverlay) {
|
53 | overlay = (<div className="skylight-dialog__overlay" style={displayStyle}></div>);
|
54 | }
|
55 |
|
56 | return (
|
57 | <section className="skylight-wrapper">
|
58 | {overlay}
|
59 | <div className="skylight-dialog" style={displayStyle}>
|
60 | <a role="button" className="skylight-dialog--close" onClick={this.hide}>×</a>
|
61 | <h2>{this.props.title}</h2>
|
62 | {this.props.children}
|
63 | </div>
|
64 | </section>
|
65 | )
|
66 | }
|
67 | });
|
68 |
|
69 | module.exports = SkyLight;
|