Skip to content

Commit 1b72948

Browse files
committed
Add loadedImageProps prop
1 parent c1cbd6f commit 1b72948

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export default MyImage;
7070
| delayMethod | `String` | `throttle` | Method from lodash to use to delay the scroll/resize events. It can be `throttle` or `debounce`. |
7171
| delayTime | `Number` | 300 | Time in ms sent to the delayMethod. |
7272
| effect | `String` | | Name of the effect to use. Please, read next section with an explanation on how to use them. |
73+
| loadedImageProps | `Object` | | Props passed to the `<img>` element once it's loaded. Useful to pass class names or style attributes that should only be applied once the image is loaded. |
7374
| placeholder | `ReactClass` | `<span>` | React element to use as a placeholder. |
7475
| placeholderSrc | `String` | | Image src to display while the image is not visible or loaded. |
7576
| threshold | `Number` | 100 | Threshold in pixels. So the image starts loading before it appears in the viewport. |

src/components/LazyLoadImage.jsx

+11-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,17 @@ class LazyLoadImage extends React.Component {
2828

2929
getImg() {
3030
const { afterLoad, beforeLoad, delayMethod, delayTime, effect,
31-
placeholder, placeholderSrc, scrollPosition, threshold,
31+
loadedImageProps, placeholder, placeholderSrc, scrollPosition, threshold,
3232
visibleByDefault, wrapperClassName, ...imgProps } = this.props;
33+
const { loaded } = this.state;
34+
const loadedProps = loaded ? loadedImageProps : null;
3335

34-
return <img onLoad={this.onImageLoad()} {...imgProps} />;
36+
return (
37+
<img
38+
onLoad={this.onImageLoad()}
39+
{...imgProps}
40+
{...loadedProps} />
41+
);
3542
}
3643

3744
getLazyLoadImage(image) {
@@ -105,6 +112,7 @@ LazyLoadImage.propTypes = {
105112
delayMethod: PropTypes.string,
106113
delayTime: PropTypes.number,
107114
effect: PropTypes.string,
115+
loadedImageProps: PropTypes.object,
108116
placeholderSrc: PropTypes.string,
109117
threshold: PropTypes.number,
110118
visibleByDefault: PropTypes.bool,
@@ -117,6 +125,7 @@ LazyLoadImage.defaultProps = {
117125
delayMethod: 'throttle',
118126
delayTime: 300,
119127
effect: '',
128+
loadedImageProps: {},
120129
placeholderSrc: '',
121130
threshold: 100,
122131
visibleByDefault: false,

src/components/LazyLoadImage.spec.js

+16
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,22 @@ describe('LazyLoadImage', function() {
9999
expect(loadedWrapper.style.getPropertyValue('background-size')).toEqual('');
100100
});
101101

102+
it('sets correct props to the loaded image', function() {
103+
const lazyLoadImage = mount(
104+
<LazyLoadImage className="lorem" loadedImageProps={{ className: 'ipsum' }} />
105+
);
106+
107+
let img = findRenderedDOMComponentWithTag(lazyLoadImage.instance(), 'img');
108+
109+
expect(img.className).toEqual('lorem');
110+
111+
Simulate.load(img);
112+
113+
img = findRenderedDOMComponentWithTag(lazyLoadImage.instance(), 'img');
114+
115+
expect(img.className).toEqual('ipsum');
116+
});
117+
102118
it('adds the effect class', function() {
103119
const lazyLoadImage = mount(
104120
<LazyLoadImage effect="blur" />

0 commit comments

Comments
 (0)