Skip to content

Commit 9fc73b0

Browse files
committedAug 7, 2019
Add placeholderProps prop
1 parent 1b72948 commit 9fc73b0

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export default MyImage;
7272
| effect | `String` | | Name of the effect to use. Please, read next section with an explanation on how to use them. |
7373
| 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. |
7474
| placeholder | `ReactClass` | `<span>` | React element to use as a placeholder. |
75+
| placeholderProps | `Object` | | Props assigned to the placeholder <span>, useful to overwrite the style or assign other HTML attributes. |
7576
| placeholderSrc | `String` | | Image src to display while the image is not visible or loaded. |
7677
| threshold | `Number` | 100 | Threshold in pixels. So the image starts loading before it appears in the viewport. |
7778
| visibleByDefault | `Boolean` | false | Whether the image must be visible from the beginning. |

‎src/components/LazyLoadImage.jsx

+19-11
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ class LazyLoadImage extends React.Component {
2828

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

@@ -65,26 +66,31 @@ class LazyLoadImage extends React.Component {
6566
}
6667

6768
getWrappedLazyLoadImage(lazyLoadImage) {
68-
const { effect, height, placeholderSrc,
69+
const { effect, height, placeholderProps, placeholderSrc,
6970
width, wrapperClassName } = this.props;
7071
const { loaded } = this.state;
7172

7273
const loadedClassName = loaded ?
7374
' lazy-load-image-loaded' :
7475
'';
76+
const props = {
77+
...placeholderProps,
78+
style: {
79+
backgroundImage: loaded ? '' : 'url( ' + placeholderSrc + ')',
80+
backgroundSize: loaded ? '' : '100% 100%',
81+
color: 'transparent',
82+
display: 'inline-block',
83+
height: height,
84+
width: width,
85+
...placeholderProps.style,
86+
},
87+
};
7588

7689
return (
7790
<span
7891
className={wrapperClassName + ' lazy-load-image-background ' +
7992
effect + loadedClassName}
80-
style={{
81-
backgroundImage: loaded ? '' : 'url( ' + placeholderSrc + ')',
82-
backgroundSize: loaded ? '' : '100% 100%',
83-
color: 'transparent',
84-
display: 'inline-block',
85-
height: height,
86-
width: width,
87-
}}>
93+
{ ...props }>
8894
{lazyLoadImage}
8995
</span>
9096
);
@@ -113,6 +119,7 @@ LazyLoadImage.propTypes = {
113119
delayTime: PropTypes.number,
114120
effect: PropTypes.string,
115121
loadedImageProps: PropTypes.object,
122+
placeholderProps: PropTypes.object,
116123
placeholderSrc: PropTypes.string,
117124
threshold: PropTypes.number,
118125
visibleByDefault: PropTypes.bool,
@@ -126,6 +133,7 @@ LazyLoadImage.defaultProps = {
126133
delayTime: 300,
127134
effect: '',
128135
loadedImageProps: {},
136+
placeholderProps: {},
129137
placeholderSrc: '',
130138
threshold: 100,
131139
visibleByDefault: false,

‎src/components/LazyLoadImage.spec.js

+16
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,20 @@ describe('LazyLoadImage', function() {
158158

159159
expect(span.length).toEqual(0);
160160
});
161+
162+
it('renders placeholder with correct style', function() {
163+
const lazyLoadImage = mount(
164+
<LazyLoadImage
165+
placeholderSrc="lorem-ipsum.jpg"
166+
placeholderProps={{
167+
'aria-hidden': 'true',
168+
style: { display: 'block' },
169+
}} />
170+
);
171+
172+
const span = findRenderedDOMComponentWithTag(lazyLoadImage.instance(), 'span');
173+
174+
expect(span.style.getPropertyValue('display')).toEqual('block');
175+
expect(span.getAttribute('aria-hidden')).toEqual('true');
176+
});
161177
});

0 commit comments

Comments
 (0)
Please sign in to comment.