Skip to content

Commit fd076c6

Browse files
committed
return opacity: 1 if server rendered, fixes #4
1 parent 4bbc9b1 commit fd076c6

File tree

4 files changed

+10
-3
lines changed

4 files changed

+10
-3
lines changed

lib/image.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var Image = function (_React$Component) {
3131
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Image).call(this));
3232

3333
var opacity = 0;
34-
if (!src.match(/http/)) opacity = 1;
34+
if (!src.match(/http/) || typeof window === 'undefined') opacity = 1;
3535

3636
_this.state = { opacity: opacity };
3737
return _this;

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.2",
3+
"version": "0.1.3",
44
"description": "an img tag, with built in lazy loading",
55
"main": "lib/image.js",
66
"scripts": {

src/image.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default class Image extends React.Component {
55
constructor({ src }){
66
super()
77
let opacity = 0
8-
if(!src.match(/http/)) opacity = 1
8+
if(!src.match(/http/) || typeof window === 'undefined') opacity = 1
99

1010
this.state = { opacity }
1111
}

tests/image.js

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

66
describe('Image', () => {
77
it('renders with opacity: 0', () => {
8+
global.window = {}
89
const wrapper = shallow(<Image src="http://google.com" />)
910
expect(wrapper.props().style.opacity).to.equal(0)
1011
})
1112

13+
it('renders with opacity: 1 on server', () => {
14+
global.window = undefined
15+
const wrapper = shallow(<Image src="http://google.com" />)
16+
expect(wrapper.props().style.opacity).to.equal(1)
17+
})
18+
1219
it('changes opacity on state change', () => {
1320
const wrapper = shallow(<Image src="http://google.com" />)
1421
wrapper.setState({ opacity: 1 })

0 commit comments

Comments
 (0)