You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+28-22Lines changed: 28 additions & 22 deletions
Original file line number
Diff line number
Diff line change
@@ -34,11 +34,11 @@
34
34
* Easy to understand source code. You should be able to fork and do your thing if desired.
35
35
* Ample documentation to help you understand the problem, in addition to the solutions.
36
36
37
-
What it does not do by itself:
37
+
What it does not do out:
38
38
39
39
* Polyfill `IntersectionObserver`. Adding polyfills is something you should do consciously at the application level. See [Polyfilling IntersectionObserver](#polyfill-intersectionobserver) for how to do this.
40
40
* Dictate the kind of placeholders displayed. There are many ways to do it; you can use a simple box with a background color (I hear gray is popular), a blurred image, some gradient, or anything you'd like. You are in control of the element that gets rendered.
41
-
* Animate transitions between placeholder and source. Again, you are in control of the containers, so it is possible to implement those at the consumer.
41
+
* Animate transitions between placeholder and source. Again, you are in control of the containers, so it should be possible to implement those at the consumer.
42
42
43
43
In other words, this library focuses on loading the images once in view and supporting loading patterns around that.
44
44
The actual components are yours to decide!
@@ -58,10 +58,10 @@ Then import according to your modules model and bundler, such as [Rollup](https:
58
58
```js
59
59
// ES Modules
60
60
// For all possible functions to import look at the documentation
61
-
import {LazyImage} from"react-lazy-images";
61
+
import {LazyImage} from'react-lazy-images';
62
62
63
63
/// CommonJS modules
64
-
const {LazyImage} =require("react-lazy-images");
64
+
const {LazyImage} =require('react-lazy-images');
65
65
```
66
66
67
67
A [UMD](https://github.com/umdjs/umd) version is also available on [unpkg](https://unpkg.com/):
@@ -102,14 +102,14 @@ Using this API is not specific to React; it just seems like a good fit for this
@@ -170,7 +170,7 @@ A common optimisation to the loading strategy is to preload the image before swa
170
170
In other words, once the image is in view, you can kick off a request to load the image, and only show it once fully loaded.
171
171
This avoids presenting a half-loaded image (i.e. one that is still scanning top-to-bottom), and makes the transition smoother.
172
172
173
-
This behaviour is provided by default:
173
+
This behaviour is provided with the `src` prop:
174
174
175
175
```jsx
176
176
// Note that the actual src is also provided separately,
@@ -192,6 +192,8 @@ This behaviour is provided by default:
192
192
/>
193
193
```
194
194
195
+
There is another case if you are using `srcset` for your images; `LazyImage` needs that information to preload the correct image. You can provide it with the `srcSet` prop.
196
+
195
197
### Loading and Error states
196
198
197
199
You can choose what to display on Loading and Error using the render props `loading` and `error`:
@@ -200,16 +202,16 @@ You can choose what to display on Loading and Error using the render props `load
Copy file name to clipboardExpand all lines: stories/LazyImage.story.js
+51Lines changed: 51 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -64,6 +64,57 @@ storiesOf('LazyImage', module)
64
64
</Container>
65
65
))
66
66
)
67
+
// With srcSet
68
+
.add(
69
+
'With src and srcSet',
70
+
withInfo(
71
+
'With srcset, the browser decides which image to load. In that case, src is not informative enough for preloading. You can pass the `srcSet` prop to provide that additional information to LazyImage.'
'Sometimes, it might be impractical to specify the src with your current setup. For example, it is possible that you are generating the sources for an Image CDN and have a dedicated component for it. In those cases, changing the component might be impractical in the short-term. If you provide no src or srcSet, then the preload-before-swap behaviour is not used. We believe that showing a possibly still-downloading image is better than having lazy-loading at all.'
99
+
)(()=>(
100
+
<Container>
101
+
<LazyImage
102
+
placeholder={({cls})=>(
103
+
<img
104
+
src="https://www.fillmurray.com/g/60/40"
105
+
className={`${cls} w-100`}
106
+
/>
107
+
)}
108
+
actual={({cls})=>(
109
+
<img
110
+
src="https://www.fillmurray.com/g/600/400"
111
+
className={`${cls} w-100`}
112
+
/>
113
+
)}
114
+
/>
115
+
</Container>
116
+
))
117
+
)
67
118
// Always load an image (aka "eagerly"; how the browser does it already.
68
119
// Useful if you want to load the actual content without waiting for Javascript.
0 commit comments