Skip to content
This repository was archived by the owner on Jan 26, 2024. It is now read-only.

Commit cdf755b

Browse files
authored
Merge pull request #44 from benox3/v0.1.1
Release v0.1.1
2 parents 2553cc4 + be85f65 commit cdf755b

File tree

3 files changed

+61
-46
lines changed

3 files changed

+61
-46
lines changed

lib/common/Pic.js

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,32 @@ export default class Pic extends Component {
1919
};
2020

2121
this.setResponsiveImage = this.setResponsiveImage.bind(this);
22-
this.loadHandler = this.loadHandler.bind(this);
23-
}
24-
componentDidMount() {
25-
this.loadHandler();
26-
}
22+
this.inViewHandler = this.inViewHandler.bind(this);
2723

28-
componentWillUnmount() {
29-
window.removeEventListener('scroll', debounce(() => {
30-
this.inViewHandler();
31-
}, 150));
24+
// calls inViewHandler with a debounce
25+
this.debouncedInViewHandler = debounce(this.inViewHandler.bind(this), 150);
3226

33-
window.removeEventListener('resize', debounce(this.setResponsiveImage, 150));
3427
}
35-
36-
loadHandler() {
37-
// set optimal image if in view or if elected to render out of view on mount
38-
this.props.renderOutOfView ? this.setResponsiveImage() : this.inViewHandler();
28+
componentDidMount() {
29+
this.inViewHandler();
3930

4031
// set responsive image on scroll
41-
window.addEventListener('scroll', debounce(() => {
42-
this.inViewHandler();
43-
}, 150));
32+
window.addEventListener('scroll', this.debouncedInViewHandler);
4433

4534
// set responsive image on resize
46-
window.addEventListener('resize', debounce(this.setResponsiveImage, 150));
35+
window.addEventListener('resize', this.debouncedInViewHandler);
4736
}
4837

38+
componentWillUnmount() {
39+
window.removeEventListener('scroll', this.debouncedInViewHandler);
40+
window.removeEventListener('resize', this.debouncedInViewHandler);
41+
}
42+
43+
/**
44+
* Sets responsive image if the element is in view
45+
*/
4946
inViewHandler() {
50-
if (isElementInView(this.refs.base)) {
47+
if (this.props.renderOutOfView || isElementInView(this.refs.base)) {
5148
this.setResponsiveImage();
5249
}
5350
}
@@ -80,7 +77,7 @@ export default class Pic extends Component {
8077
}
8178

8279
return (
83-
<div ref='base' style={this.props.baseStyle} onLoad={this.loadHandler}>
80+
<div ref='base' style={this.props.baseStyle} onLoad={this.inViewHandler}>
8481
<ImageBase {...this.props} {...this.state} />
8582
</div>
8683
);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-pic",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "React component for progressive and responsive image loading.",
55
"author": "Ben Budnevich",
66
"main": "dist/commonjs/react-pic.js",

playground/index.js

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ import React, { Component } from 'react';
44
import Pic from '../lib/index.js';
55

66
export default class Playground extends Component {
7+
constructor(props) {
8+
super(props);
9+
this.state = {
10+
show: true
11+
};
12+
this.clickHandler = this.clickHandler.bind(this);
13+
}
14+
15+
clickHandler() {
16+
this.setState({show:!this.state.show});
17+
}
18+
719
render() {
820
return (
921
<html>
@@ -12,31 +24,37 @@ export default class Playground extends Component {
1224
<title>react-pic</title>
1325
</head>
1426
<body>
15-
<div id="root">
16-
<Pic
17-
alt='winky face'
18-
images={[
19-
{
20-
width: 40,
21-
url: 'http://placehold.it/40?text=😉'
22-
},
23-
{
24-
width: 200,
25-
url: 'http://placehold.it/200?text=😉'
26-
},
27-
{
28-
width: 400,
29-
url: 'http://placehold.it/400?text=😉'
30-
},
31-
{
32-
width: 600,
33-
url: 'http://placehold.it/600?text=😉'
34-
},
35-
{
36-
width: 800,
37-
url: 'http://placehold.it/800?text=😉'
38-
}
39-
]} />
27+
<div id="root" style={{height:1500}}>
28+
<button onClick={this.clickHandler}>
29+
Toggle
30+
</button>
31+
{
32+
this.state.show &&
33+
<Pic
34+
alt='winky face'
35+
images={[
36+
{
37+
width: 40,
38+
url: 'http://placehold.it/40?text=😉'
39+
},
40+
{
41+
width: 200,
42+
url: 'http://placehold.it/200?text=😉'
43+
},
44+
{
45+
width: 400,
46+
url: 'http://placehold.it/400?text=😉'
47+
},
48+
{
49+
width: 600,
50+
url: 'http://placehold.it/600?text=😉'
51+
},
52+
{
53+
width: 800,
54+
url: 'http://placehold.it/800?text=😉'
55+
}
56+
]} />
57+
}
4058
</div>
4159
<script src='//localhost:8080/build/react-pic.js' />
4260
</body>

0 commit comments

Comments
 (0)