Skip to content

Commit

Permalink
Add placeholderProps prop
Browse files Browse the repository at this point in the history
  • Loading branch information
Aljullu committed Aug 7, 2019
1 parent 1b72948 commit 9fc73b0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export default MyImage;
| effect | `String` | | Name of the effect to use. Please, read next section with an explanation on how to use them. |
| 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. |
| placeholder | `ReactClass` | `<span>` | React element to use as a placeholder. |
| placeholderProps | `Object` | | Props assigned to the placeholder <span>, useful to overwrite the style or assign other HTML attributes. |
| placeholderSrc | `String` | | Image src to display while the image is not visible or loaded. |
| threshold | `Number` | 100 | Threshold in pixels. So the image starts loading before it appears in the viewport. |
| visibleByDefault | `Boolean` | false | Whether the image must be visible from the beginning. |
Expand Down
30 changes: 19 additions & 11 deletions src/components/LazyLoadImage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ class LazyLoadImage extends React.Component {

getImg() {
const { afterLoad, beforeLoad, delayMethod, delayTime, effect,
loadedImageProps, placeholder, placeholderSrc, scrollPosition, threshold,
visibleByDefault, wrapperClassName, ...imgProps } = this.props;
loadedImageProps, placeholder, placeholderProps, placeholderSrc,
scrollPosition, threshold, visibleByDefault, wrapperClassName,
...imgProps } = this.props;
const { loaded } = this.state;
const loadedProps = loaded ? loadedImageProps : null;

Expand Down Expand Up @@ -65,26 +66,31 @@ class LazyLoadImage extends React.Component {
}

getWrappedLazyLoadImage(lazyLoadImage) {
const { effect, height, placeholderSrc,
const { effect, height, placeholderProps, placeholderSrc,
width, wrapperClassName } = this.props;
const { loaded } = this.state;

const loadedClassName = loaded ?
' lazy-load-image-loaded' :
'';
const props = {
...placeholderProps,
style: {
backgroundImage: loaded ? '' : 'url( ' + placeholderSrc + ')',
backgroundSize: loaded ? '' : '100% 100%',
color: 'transparent',
display: 'inline-block',
height: height,
width: width,
...placeholderProps.style,
},
};

return (
<span
className={wrapperClassName + ' lazy-load-image-background ' +
effect + loadedClassName}
style={{
backgroundImage: loaded ? '' : 'url( ' + placeholderSrc + ')',
backgroundSize: loaded ? '' : '100% 100%',
color: 'transparent',
display: 'inline-block',
height: height,
width: width,
}}>
{ ...props }>
{lazyLoadImage}
</span>
);
Expand Down Expand Up @@ -113,6 +119,7 @@ LazyLoadImage.propTypes = {
delayTime: PropTypes.number,
effect: PropTypes.string,
loadedImageProps: PropTypes.object,
placeholderProps: PropTypes.object,
placeholderSrc: PropTypes.string,
threshold: PropTypes.number,
visibleByDefault: PropTypes.bool,
Expand All @@ -126,6 +133,7 @@ LazyLoadImage.defaultProps = {
delayTime: 300,
effect: '',
loadedImageProps: {},
placeholderProps: {},
placeholderSrc: '',
threshold: 100,
visibleByDefault: false,
Expand Down
16 changes: 16 additions & 0 deletions src/components/LazyLoadImage.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,20 @@ describe('LazyLoadImage', function() {

expect(span.length).toEqual(0);
});

it('renders placeholder with correct style', function() {
const lazyLoadImage = mount(
<LazyLoadImage
placeholderSrc="lorem-ipsum.jpg"
placeholderProps={{
'aria-hidden': 'true',
style: { display: 'block' },
}} />
);

const span = findRenderedDOMComponentWithTag(lazyLoadImage.instance(), 'span');

expect(span.style.getPropertyValue('display')).toEqual('block');
expect(span.getAttribute('aria-hidden')).toEqual('true');
});
});

0 comments on commit 9fc73b0

Please sign in to comment.