-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
46 lines (41 loc) · 1.07 KB
/
index.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
var React = require('react')
module.exports = React.createClass({
getInitialState: function() {
return {
loaded: false
}
},
render: function() {
var divStyles = {
position: 'relative',
paddingBottom: Math.round(100 / Number(this.props.aspectRatio)) + '%'
}
var imageStyles = {
position: 'absolute',
top: 0, right: 0, bottom: 0, left: 0,
backgroundSize: 'cover',
backgroundPosition: 'center center',
backgroundImage: 'url(' + this.props.src + ')',
opacity: this.state.loaded ? 100 : 0,
transition: this.props.transition || 'opacity 0.6s ease'
}
return (
React.DOM.div({
className: 'component-image' + (!this.state.loaded ? ' component-image--loading' : ''),
style: divStyles
},
React.DOM.div({
style: imageStyles
})
)
)
},
componentDidMount: function() {
var self = this
var img = document.createElement('img')
img.onload = function() {
self.setState({loaded: true})
}
img.src = this.props.src
}
})