Skip to content

Commit b4a14cb

Browse files
committed
feat(LazyLoadImages): Responsive image takes eager=true
1 parent 8dcec03 commit b4a14cb

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ 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 `eager={true}` to force the image to be eager
213+
212214
```javascript
213215
import { LazyLoadImages } from '@quintype/components';
214216

@@ -218,7 +220,9 @@ function LazyLoadSecondImage() {
218220
<LazyLoadImages margin={50}>
219221
<div>
220222
<UnrelatedContent/>
221-
<ResponsiveImage slug={props["lazy-image"]} />
223+
<ResponsiveImage slug={props["lazy-image-1"]} />
224+
<ResponsiveImage slug={props["lazy-image-forced-to-be-eager"]} eager={true}/>
225+
<ResponsiveImage slug={props["lazy-image-2"]} />
222226
</div>
223227
</LazyLoadImages>
224228
<ResponsiveImage slug={props["eager-image-2"]} />

src/components/responsive-image.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {func} from 'prop-types';
55
import omit from 'lodash/omit';
66
import emptyWebGif from 'empty-web-gif';
77

8-
const USED_PARAMS = ["imageCDN","defaultWidth","widths","imgParams","slug","metadata","aspectRatio", "reactTag"];
8+
const USED_PARAMS = ["imageCDN","defaultWidth","widths","imgParams","slug","metadata","aspectRatio", "reactTag", "eager"];
99

1010
// Add the following CSS somewhere: img.qt-image { width: 100%; object-fit: cover; }
1111

@@ -43,10 +43,20 @@ export class ResponsiveImageBase extends React.Component {
4343

4444
super(props, context);
4545
this.state = {
46-
showImage: !context.lazyLoadObserveImage
46+
showImage: !this.shouldLazyLoad()
4747
}
4848
}
4949

50+
shouldLazyLoad() {
51+
if (this.props.eager) {
52+
return false;
53+
}
54+
if (this.context.lazyLoadObserveImage && this.context.lazyLoadUnobserveImage) {
55+
return true;
56+
}
57+
return false;
58+
}
59+
5060
render() {
5161
const imageProps = this.state.showImage ? responsiveProps(this.props) : {src: emptyWebGif};
5262
return React.createElement(this.props.reactTag || "img", Object.assign(imageProps, omit(this.props, USED_PARAMS), {
@@ -56,11 +66,11 @@ export class ResponsiveImageBase extends React.Component {
5666
}
5767

5868
componentDidMount() {
59-
this.context.lazyLoadObserveImage && this.context.lazyLoadObserveImage(this.dom, this);
69+
this.shouldLazyLoad() && this.context.lazyLoadObserveImage(this.dom, this);
6070
}
6171

6272
componentWillUnmount() {
63-
this.context.lazyLoadUnobserveImage && this.context.lazyLoadUnobserveImage(this.dom, this);
73+
this.shouldLazyLoad() && this.context.lazyLoadUnobserveImage(this.dom, this);
6474
}
6575

6676
showImage() {

0 commit comments

Comments
 (0)