Skip to content

Commit 7ea1fd2

Browse files
committed
Support base64, closes #5
1 parent 1544dcc commit 7ea1fd2

4 files changed

Lines changed: 28 additions & 26 deletions

File tree

lib/image.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,35 @@ function _inherits(subClass, superClass) { if (typeof superClass !== "function"
2323
var Image = function (_React$Component) {
2424
_inherits(Image, _React$Component);
2525

26-
function Image() {
26+
function Image(_ref) {
27+
var src = _ref.src;
28+
2729
_classCallCheck(this, Image);
2830

2931
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Image).call(this));
3032

31-
_this.state = {
32-
opacity: 0
33-
};
34-
_this.fadeIn = _this.fadeIn.bind(_this);
33+
var opacity = 0;
34+
if (!src.match(/http/)) opacity = 1;
35+
36+
_this.state = { opacity: opacity };
3537
return _this;
3638
}
3739

3840
_createClass(Image, [{
39-
key: 'fadeIn',
40-
value: function fadeIn() {
41-
this.setState({ opacity: 1 });
42-
}
43-
}, {
4441
key: 'render',
4542
value: function render() {
46-
//this vs including an Object.assign polyfill
43+
var _this2 = this;
44+
4745
var style = this.props.style || {};
4846
style.transition = 'opacity ' + (this.props.speed || 1) + 's';
4947
style.opacity = this.state.opacity;
5048

5149
return _react2.default.createElement('img', _extends({}, this.props, {
5250
style: style,
5351
src: this.props.src,
54-
onLoad: this.fadeIn
52+
onLoad: function onLoad() {
53+
return _this2.setState({ opacity: 1 });
54+
}
5555
}));
5656
}
5757
}]);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "legit-image",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "an img tag, with built in lazy loading",
55
"main": "lib/image.js",
66
"scripts": {

src/image.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
import React from 'react'
22

3-
export default class Image extends React.Component{
3+
export default class Image extends React.Component {
44

5-
constructor(){
5+
constructor({ src }){
66
super()
7-
this.state = {
8-
opacity: 0
9-
}
10-
this.fadeIn = this.fadeIn.bind(this)
11-
}
7+
let opacity = 0
8+
if(!src.match(/http/)) opacity = 1
129

13-
fadeIn() {
14-
this.setState({opacity: 1})
10+
this.state = { opacity }
1511
}
1612

1713
render(){
18-
//this vs including an Object.assign polyfill
1914
let style = this.props.style || {}
2015
style.transition = `opacity ${this.props.speed || 1}s`
2116
style.opacity = this.state.opacity
@@ -25,8 +20,10 @@ export default class Image extends React.Component{
2520
{...this.props}
2621
style={style}
2722
src={this.props.src}
28-
onLoad={this.fadeIn}
23+
onLoad={() => this.setState({opacity: 1})}
2924
/>
3025
)
3126
}
3227
}
28+
29+

tests/image.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@ import Image from '../src/image'
55

66
describe('Image', () => {
77
it('renders with opacity: 0', () => {
8-
const wrapper = shallow(<Image src="blah" />)
8+
const wrapper = shallow(<Image src="http://google.com" />)
99
expect(wrapper.props().style.opacity).to.equal(0)
1010
})
1111

1212
it('changes opacity on state change', () => {
13-
const wrapper = shallow(<Image src="blah" />)
13+
const wrapper = shallow(<Image src="http://google.com" />)
1414
wrapper.setState({ opacity: 1 })
1515
expect(wrapper.props().style.opacity).to.equal(1)
1616
})
17+
18+
it('opacity: 1 if base64', () => {
19+
const wrapper = shallow(<Image src="data:image/" />)
20+
expect(wrapper.props().style.opacity).to.equal(1)
21+
})
1722
})

0 commit comments

Comments
 (0)