-
Notifications
You must be signed in to change notification settings - Fork 0
/
Popup.js
93 lines (87 loc) · 2.25 KB
/
Popup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import React, { PropTypes } from 'react';
import ReactDOM from 'react-dom';
import classNames from 'classnames';
import StaticContainer from '../Utils/StaticContainer';
import TransitionMixin from '../Utils/TransitionMixin';
import WindowListener from '../Utils/WindowListener';
import './Popup.scss';
const Popup = React.createClass({
mixins: [TransitionMixin],
propTypes: {
arrow: PropTypes.bool,
className: PropTypes.string,
shown: PropTypes.bool,
},
getDefaultProps() {
return {
arrow: true,
placement: 'bottom',
shown: false,
};
},
render() {
let content;
const className = classNames('Popup', this.props.className, {
'Popup--shown': this.props.shown,
'Popup--leaving': this.state.leaveAnimation,
});
if (this.props.shown || this.state.leaveAnimation) {
content = (
<div className="Popup-staticContainer">
{this.props.arrow ? <div className="Popup-arrow" /> : null}
<div className="Popup-inner">
{this.props.children}
</div>
</div>
);
}
return (
<div className={className}>
<StaticContainer shouldUpdate={!this.state.leaveAnimation}>
{content}
</StaticContainer>
</div>
);
},
});
export const PopupHandler = React.createClass({
mixins: [new WindowListener('click', 'handleWindowClick', true)],
propTypes: {
handler: PropTypes.any,
popup: PropTypes.any,
},
getInitialState() {
return {
shown: false,
};
},
open() {
this.setState({ shown: true });
},
close() {
this.setState({ shown: false });
},
handleWindowClick(event) {
if (this.state.shown) {
const thisNode = ReactDOM.findDOMNode(this);
const target = event.target;
if (thisNode !== target && !thisNode.contains(target)) {
this.close();
} else {
setTimeout(() => this.close(), 60);
}
}
},
render() {
const PopupComponent = this.props.popup || Popup;
return (
<div className="PopupHandler" onClick={this.open}>
{this.props.handler}
<PopupComponent shown={this.state.shown} {...this.props}>
{this.props.children}
</PopupComponent>
</div>
);
},
});
export default Popup;