Skip to content
This repository was archived by the owner on Aug 19, 2022. It is now read-only.

Commit 50cf146

Browse files
authored
Merge pull request #6 from FormidableLabs/add-loading-key
Add new `loading` argument to render callback
2 parents 3c193b8 + 9bf89a9 commit 50cf146

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,18 @@ If you use the UMD build you can find the library on `window.ReactProgressiveIma
2828

2929
`ProgressiveImage` accepts a render callback as a child, which will be called with the `placeholder` first, and then `src` once the image has been loaded.
3030

31-
```js
31+
```jsx
3232
<ProgressiveImage src='large-image.jpg' placeholder='tiny-image.jpg'>
3333
{(src) => <img src={src} alt='an image'/>}
3434
</ProgressiveImage>
3535
```
36+
37+
It will also call the render callback with a second argument, `loading`, which you can use to quickly determine what image is being rendered. `loading` will be `true` when the placeholder is rendered, and `false` when the full image is rendered.
38+
39+
```jsx
40+
<ProgressiveImage src='large-image.jpg' placeholder='tiny-image.jpg'>
41+
{(src, loading) => (
42+
<img style={{ opacity: loading ? 0.5 : 1 }} src={src} alt='an image'/>
43+
)}
44+
</ProgressiveImage>
45+
```

__tests__/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,13 @@ describe('react-progressive-image', () => {
6969
expect(render.mock.calls[1][0]).toEqual(src);
7070
});
7171

72+
it('should pass the loading state', () => {
73+
const render = jest.fn(() => <h1>Hello, world</h1>);
74+
mountProgressiveImage(render);
75+
expect(render.mock.calls[0][0]).toEqual(placeholder);
76+
expect(render.mock.calls[0][1]).toEqual(true);
77+
expect(render.mock.calls[1][0]).toEqual(src);
78+
expect(render.mock.calls[1][1]).toEqual(false);
79+
});
80+
7281
});

index.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type ProgressiveImageProps = {
1010

1111
type ProgressiveImageState = {
1212
image: string,
13+
loading: boolean,
1314
};
1415

1516
export default class ProgressiveImage extends React.Component {
@@ -19,7 +20,8 @@ export default class ProgressiveImage extends React.Component {
1920
constructor(props: ProgressiveImageProps) {
2021
super(props);
2122
this.state = {
22-
image: props.placeholder
23+
image: props.placeholder,
24+
loading: true
2325
};
2426
}
2527

@@ -32,7 +34,7 @@ export default class ProgressiveImage extends React.Component {
3234
const { src, placeholder } = nextProps;
3335
// We only invalidate the current image if the src has changed.
3436
if (src !== this.props.src) {
35-
this.setState({ image: placeholder }, () => {
37+
this.setState({ image: placeholder, loading: true }, () => {
3638
this.loadImage(src);
3739
});
3840
}
@@ -66,7 +68,8 @@ export default class ProgressiveImage extends React.Component {
6668
// new image loading before the new props are available as
6769
// this.props.
6870
this.setState({
69-
image: this.image.src
71+
image: this.image.src,
72+
loading: false
7073
});
7174
}
7275

@@ -78,13 +81,13 @@ export default class ProgressiveImage extends React.Component {
7881
}
7982

8083
render() {
81-
const { image } = this.state;
84+
const { image, loading } = this.state;
8285
const { children } = this.props;
8386
if (!children || typeof children !== 'function') {
8487
throw new Error(
8588
`ProgressiveImage requires a function as its only child`
8689
);
8790
}
88-
return children(image);
91+
return children(image, loading);
8992
}
9093
}

0 commit comments

Comments
 (0)