Skip to content

Commit 785a68d

Browse files
committed
feat(LazyLoadImages): EagerLoadImages now takes a predicate
1 parent 006d185 commit 785a68d

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ import { LazyCollection } from '@quintype/components'
209209

210210
This component will ensure all [ResponsiveImages](#ResponsiveImage) that are in its descendent path will be loaded async. By default, the image is loaded with an empty gif, and the image becomes visible when the image scrolls 250 from the edge of the screen.
211211

212-
You can use `EagerLoadImages` or `eager={true}` to force the image to be eager.
212+
You can use `EagerLoadImages` or `eager={true}` to force the image to be eager. If `EagerLoadImages` is passed a predicate, then images that pass a matching value to `eager` will be rendered eagerly.
213213

214214
```javascript
215215
import { LazyLoadImages, EagerLoadImages } from '@quintype/components';
@@ -221,11 +221,15 @@ function LazyLoadSecondImage() {
221221
<div>
222222
<UnrelatedContent/>
223223
<ResponsiveImage slug={props["lazy-image-1"]} />
224-
<ResponsiveImage slug={props["lazy-image-forced-to-be-eager"]} eager={true}/>
224+
<ResponsiveImage slug={props["lazy-image-forced-to-be-eager"]} eager/>
225225
<ResponsiveImage slug={props["lazy-image-2"]} />
226226
<EagerLoadImages>
227227
<ResponsiveImage slug={props["lazy-image-forced-to-be-eager"]} />
228228
</EagerLoadImages>
229+
<EagerLoadImages predicate={(token) => token % 2 === 0}>
230+
<ResponsiveImage slug={props["lazy-image"]} eager={1} />
231+
<ResponsiveImage slug={props["eager-image"]} eager={2} />
232+
</EagerLoadImages>
229233
</div>
230234
</LazyLoadImages>
231235
<ResponsiveImage slug={props["eager-image-2"]} />

src/components/lazy-load-images.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,15 @@ export class LazyLoadImages extends React.Component {
109109
}
110110
}
111111

112+
LazyLoadImages.childContextTypes = {
113+
lazyLoadObserveImage: func,
114+
lazyLoadUnobserveImage: func
115+
};
116+
112117
export class EagerLoadImages extends React.Component {
113118
getChildContext() {
114119
return {
115-
lazyLoadObserveImage: null,
116-
lazyLoadUnobserveImage: null
120+
lazyLoadEagerPredicate: this.props.predicate || (() => true)
117121
}
118122
}
119123

@@ -122,7 +126,6 @@ export class EagerLoadImages extends React.Component {
122126
}
123127
}
124128

125-
EagerLoadImages.childContextTypes = LazyLoadImages.childContextTypes = {
126-
lazyLoadObserveImage: func,
127-
lazyLoadUnobserveImage: func
129+
EagerLoadImages.childContextTypes = {
130+
lazyLoadEagerPredicate: func
128131
};

src/components/responsive-image.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ export class ResponsiveImageBase extends React.Component {
4848
}
4949

5050
shouldLazyLoad() {
51-
if (this.props.eager) {
51+
if (this.props.eager === true) {
52+
return false;
53+
}
54+
if (this.context.lazyLoadEagerPredicate && this.context.lazyLoadEagerPredicate(this.props.eager)) {
5255
return false;
5356
}
5457
if (this.context.lazyLoadObserveImage && this.context.lazyLoadUnobserveImage) {
@@ -86,7 +89,8 @@ function mapStateToProps(state) {
8689

8790
ResponsiveImageBase.contextTypes = {
8891
lazyLoadObserveImage: func,
89-
lazyLoadUnobserveImage: func
92+
lazyLoadUnobserveImage: func,
93+
lazyLoadEagerPredicate: func
9094
}
9195

9296
export const ResponsiveImage = connect(mapStateToProps, {})(ResponsiveImageBase);

0 commit comments

Comments
 (0)