-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodalFactory.js
More file actions
183 lines (153 loc) · 5.61 KB
/
modalFactory.js
File metadata and controls
183 lines (153 loc) · 5.61 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
var React = require('react');
var transitionEvents = require('domkit/transitionEvents');
var appendVendorPrefix = require('domkit/appendVendorPrefix');
var createReactClass = require('create-react-class');
var PropTypes = require('prop-types');
module.exports = function(animation){
var Modal = createReactClass({
getDefaultProps: function() {
return {
className: "",
onShow: function(){},
onHide: function(){},
animation: animation,
keyboard: true,
backdrop: true,
closeOnClick: true,
modalStyle: {},
backdropStyle: {},
contentStyle: {},
};
},
getInitialState: function(){
return {
willHidden: false,
hidden: true
}
},
hasHidden: function(){
return this.state.hidden;
},
addTransitionListener: function(node, handle){
if (node) {
var endListener = function(e) {
if (e && e.target !== node) {
return;
}
transitionEvents.removeEndEventListener(node, endListener);
handle();
};
transitionEvents.addEndEventListener(node, endListener);
}
},
handleBackdropClick: function() {
if (this.props.closeOnClick) {
this.hide();
}
},
render: function() {
var hidden = this.hasHidden();
if (hidden) return null;
var willHidden = this.state.willHidden;
var animation = this.props.animation;
var modalStyle = animation.getModalStyle(willHidden);
var backdropStyle = animation.getBackdropStyle(willHidden);
var contentStyle = animation.getContentStyle(willHidden);
var ref = animation.getRef(willHidden);
var sharp = animation.getSharp && animation.getSharp(willHidden);
// Apply custom style properties
if (this.props.modalStyle) {
var prefixedModalStyle = appendVendorPrefix(this.props.modalStyle);
for (var style in prefixedModalStyle) {
modalStyle[style] = prefixedModalStyle[style];
}
}
if (this.props.backdropStyle) {
var prefixedBackdropStyle = appendVendorPrefix(this.props.backdropStyle);
for (var style in prefixedBackdropStyle) {
backdropStyle[style] = prefixedBackdropStyle[style];
}
}
if (this.props.contentStyle) {
var prefixedContentStyle = appendVendorPrefix(this.props.contentStyle);
for (var style in prefixedContentStyle) {
contentStyle[style] = prefixedContentStyle[style];
}
}
var backdrop = this.props.backdrop? React.createElement("div", {style: backdropStyle, onClick: this.props.closeOnClick? this.handleBackdropClick: null}): undefined;
if(willHidden) {
var node = this.refs[ref];
this.addTransitionListener(node, this.leave);
}
return (React.createElement("span", null,
React.createElement("div", {ref: "modal", style: modalStyle, className: this.props.className},
sharp,
React.createElement("div", {ref: "content", tabIndex: "-1", style: contentStyle},
this.props.children
)
),
backdrop
))
;
},
leave: function(){
this.setState({
hidden: true
});
this.props.onHide();
},
enter: function(){
this.props.onShow();
},
show: function(){
if (!this.hasHidden()) return;
this.setState({
willHidden: false,
hidden: false
});
setTimeout(function(){
var ref = this.props.animation.getRef();
var node = this.refs[ref];
this.addTransitionListener(node, this.enter);
}.bind(this), 0);
},
hide: function(){
if (this.hasHidden()) return;
this.setState({
willHidden: true
});
},
toggle: function(){
if (this.hasHidden())
this.show();
else
this.hide();
},
listenKeyboard: function(event) {
if (this.props.keyboard &&
(event.key === "Escape" ||
event.keyCode === 27)) {
this.hide();
}
},
componentDidMount: function(){
window.addEventListener("keydown", this.listenKeyboard, true);
},
componentWillUnmount: function() {
window.removeEventListener("keydown", this.listenKeyboard, true);
}
});
Modal.propTypes = {
className: PropTypes.string,
keyboard: PropTypes.bool, // Close the modal when esc is pressed? Defaults to true.
onShow: PropTypes.func,
onHide: PropTypes.func,
animation: PropTypes.object,
backdrop: PropTypes.bool,
closeOnClick: PropTypes.bool,
modalStyle: PropTypes.object,
backdropStyle: PropTypes.object,
contentStyle: PropTypes.object
};
return Modal;
};