Skip to content

Commit 1aa87da

Browse files
committed
Add visibleByDefault prop
1 parent 2c77273 commit 1aa87da

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export default trackWindowScroll(Gallery);
7070
| beforeLoad | `Function` | Function called right before the image is rendered. |
7171
| placeholder | `ReactClass` | A React element to use as a placeholder. |
7272
| threshold | `Number` | Threshold in pixels. So the image starts loading before it appears in the viewport. _Defaults to 100._ |
73+
| visibleByDefault | `Boolean` | Whether the image must be visible from the beginning. |
7374
| ... | | Any other image attribute |
7475

7576

src/components/LazyLoadImage.jsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@ class LazyLoadImage extends React.Component {
66
constructor(props) {
77
super(props);
88

9+
const { afterLoad, beforeLoad, visibleByDefault } = this.props;
10+
911
this.state = {
10-
visible: false
12+
visible: visibleByDefault
1113
};
14+
15+
if (visibleByDefault) {
16+
beforeLoad();
17+
afterLoad();
18+
}
1219
}
1320

1421
componentDidMount() {
@@ -27,7 +34,8 @@ class LazyLoadImage extends React.Component {
2734
beforeLoad: true,
2835
placeholder: true,
2936
threshold: true,
30-
scrollPosition: true
37+
scrollPosition: true,
38+
visibleByDefault: true
3139
};
3240

3341
return keys.filter(key => !propsToIgnoreAfterVisible[key]);
@@ -125,7 +133,7 @@ class LazyLoadImage extends React.Component {
125133

126134
render() {
127135
const { afterLoad, beforeLoad, placeholder, scrollPosition, threshold,
128-
...props } = this.props;
136+
visibleByDefault, ...props } = this.props;
129137

130138
return this.state.visible ?
131139
<img {...props} /> :
@@ -144,6 +152,7 @@ LazyLoadImage.propTypes = {
144152
height: PropTypes.number,
145153
placeholder: PropTypes.element,
146154
threshold: PropTypes.number,
155+
visibleByDefault: PropTypes.bool,
147156
width: PropTypes.number
148157
};
149158

@@ -154,6 +163,7 @@ LazyLoadImage.defaultProps = {
154163
height: 0,
155164
placeholder: null,
156165
threshold: 100,
166+
visibleByDefault: false,
157167
width: 0
158168
};
159169

src/components/LazyLoadImage.spec.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ describe('LazyLoadImage', function() {
1818
beforeLoad = () => null,
1919
placeholder = null,
2020
scrollPosition = {x: 0, y: 0},
21-
style = {}
21+
style = {},
22+
visibleByDefault = false
2223
} = {}) {
2324
return mount(
2425
<LazyLoadImage
@@ -27,7 +28,8 @@ describe('LazyLoadImage', function() {
2728
placeholder={placeholder}
2829
scrollPosition={scrollPosition}
2930
src=""
30-
style={style} />
31+
style={style}
32+
visibleByDefault={visibleByDefault} />
3133
);
3234
}
3335

@@ -116,6 +118,16 @@ describe('LazyLoadImage', function() {
116118
expectPlaceholders(lazyLoadImage, 0);
117119
});
118120

121+
it('renders the image when it\'s not in the viewport but visibleByDefault is true', function() {
122+
const lazyLoadImage = renderLazyLoadImage({
123+
style: {marginTop: 100000},
124+
visibleByDefault: true
125+
});
126+
127+
expectImages(lazyLoadImage, 1);
128+
expectPlaceholders(lazyLoadImage, 0);
129+
});
130+
119131
it('doesn\'t trigger beforeLoad when the image is not the viewport', function() {
120132
const beforeLoad = jest.fn();
121133
const lazyLoadImage = renderLazyLoadImage({
@@ -148,6 +160,18 @@ describe('LazyLoadImage', function() {
148160
expect(beforeLoad).toHaveBeenCalledTimes(1);
149161
});
150162

163+
it('triggers beforeLoad when visibleByDefault is true', function() {
164+
const beforeLoad = jest.fn();
165+
const offset = 100000;
166+
const lazyLoadImage = renderLazyLoadImage({
167+
beforeLoad,
168+
style: {marginTop: offset},
169+
visibleByDefault: true
170+
});
171+
172+
expect(beforeLoad).toHaveBeenCalledTimes(1);
173+
});
174+
151175
it('doesn\'t trigger afterLoad when the image is not the viewport', function() {
152176
const afterLoad = jest.fn();
153177
const lazyLoadImage = renderLazyLoadImage({
@@ -179,4 +203,16 @@ describe('LazyLoadImage', function() {
179203

180204
expect(afterLoad).toHaveBeenCalledTimes(1);
181205
});
206+
207+
it('triggers afterLoad when visibleByDefault is true', function() {
208+
const afterLoad = jest.fn();
209+
const offset = 100000;
210+
const lazyLoadImage = renderLazyLoadImage({
211+
afterLoad,
212+
style: {marginTop: offset},
213+
visibleByDefault: true
214+
});
215+
216+
expect(afterLoad).toHaveBeenCalledTimes(1);
217+
});
182218
});

0 commit comments

Comments
 (0)