-
Notifications
You must be signed in to change notification settings - Fork 15
/
react-lightbox.jsx
138 lines (120 loc) · 3.54 KB
/
react-lightbox.jsx
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
/** @jsx React.DOM */
// CSS from http://stackoverflow.com/questions/19064987/html-css-popup-div-on-text-click
// and http://stackoverflow.com/questions/10019797/pure-css-close-button
var LightboxModal = React.createClass({
whiteContentStyles: {
position: 'fixed',
top: '25%',
left: '30%',
right: '30%',
backgroundColor: '#fff',
color: '#7F7F7F',
padding: '20px',
border: '2px solid #ccc',
borderRadius: '20px',
boxShadow: '0 1px 5px #333',
zIndex:'101'
},
blackOverlayStyles: {
background: 'black',
opacity: '.5',
position: 'fixed',
top: '0px',
bottom: '0px',
left: '0px',
right: '0px',
zIndex: '100'
},
closeTagStyles: {
float: 'right',
marginTop: '-30px',
marginRight: '-30px',
cursor: 'pointer',
color: '#fff',
border: '1px solid #AEAEAE',
borderRadius: '30px',
background: '#605F61',
fontSize: '31px',
fontWeight: 'bold',
display: 'inline-block',
lineHeight: '0px',
padding: '11px 3px',
textDecoration: 'none'
},
componentDidMount: function(){
document.addEventListener("keydown", function (e) {
if ( (this.props.display) && (e.keyCode === 27) ){
this.props.closeLightbox();
}
}.bind(this));
},
render: function(){
for (var j in this.props){
if (j !== 'children'){
this.props.children.props[j] = this.props[j];
}
}
if (this.props.display){
return (
<div>
<div style={this.blackOverlayStyles} onClick={this.props.closeLightbox} />
<div style={this.whiteContentStyles}>
<a style={this.closeTagStyles} onClick={this.props.closeLightbox}>×</a>
{this.props.children}
</div>
</div>
);
} else {
return (<div></div>);
}
}
});
var LightboxTrigger = React.createClass({
render: function(){
this.props.children.props.onClick = this.props.openLightbox;
for (var j in this.props){
if (j !== 'children'){
this.props.children.props[j] = this.props[j];
}
}
return this.props.children;
}
});
var Lightbox = React.createClass({
getInitialState: function(){
return { display: false };
},
componentWillMount: function(){
if (this.props.data)
this.setState(this.props.data);
},
openLightbox: function(){
this.setState({display: true});
},
closeLightbox: function(){
this.setState({display: false});
},
setLightboxState: function(obj){
this.setState(obj);
},
render: function(){
var childrenWithProps = this.props.children.map(function(child, i) {
var childProps = {
openLightbox: this.openLightbox,
closeLightbox: this.closeLightbox,
setLightboxState: this.setLightboxState,
key: i
};
for (var j in this.state){
childProps[j] = this.state[j];
}
var childWithProps = React.addons.cloneWithProps(child, childProps);
return childWithProps;
}, this);
return (
<div>
{childrenWithProps}
</div>
);
}
});