diff --git a/README.md b/README.md index be021d9..3ca1b08 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,9 @@ export default MyImage; | delayMethod | `String` | `throttle` | Method from lodash to use to delay the scroll/resize events. It can be `throttle` or `debounce`. | | delayTime | `Number` | 300 | Time in ms sent to the delayMethod. | | 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 `` 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` | `` | React element to use as a placeholder. | +| placeholderProps | `Object` | | Props assigned to the placeholder , 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. | diff --git a/src/components/LazyLoadImage.jsx b/src/components/LazyLoadImage.jsx index 2fcf9e2..d8dd9ad 100644 --- a/src/components/LazyLoadImage.jsx +++ b/src/components/LazyLoadImage.jsx @@ -28,10 +28,18 @@ class LazyLoadImage extends React.Component { getImg() { const { afterLoad, beforeLoad, delayMethod, delayTime, effect, - 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; - return ; + return ( + + ); } getLazyLoadImage(image) { @@ -58,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 ( + { ...props }> {lazyLoadImage} ); @@ -105,6 +118,8 @@ LazyLoadImage.propTypes = { delayMethod: PropTypes.string, delayTime: PropTypes.number, effect: PropTypes.string, + loadedImageProps: PropTypes.object, + placeholderProps: PropTypes.object, placeholderSrc: PropTypes.string, threshold: PropTypes.number, visibleByDefault: PropTypes.bool, @@ -117,6 +132,8 @@ LazyLoadImage.defaultProps = { delayMethod: 'throttle', delayTime: 300, effect: '', + loadedImageProps: {}, + placeholderProps: {}, placeholderSrc: '', threshold: 100, visibleByDefault: false, diff --git a/src/components/LazyLoadImage.spec.js b/src/components/LazyLoadImage.spec.js index 2515579..96e8e25 100644 --- a/src/components/LazyLoadImage.spec.js +++ b/src/components/LazyLoadImage.spec.js @@ -99,6 +99,22 @@ describe('LazyLoadImage', function() { expect(loadedWrapper.style.getPropertyValue('background-size')).toEqual(''); }); + it('sets correct props to the loaded image', function() { + const lazyLoadImage = mount( + + ); + + let img = findRenderedDOMComponentWithTag(lazyLoadImage.instance(), 'img'); + + expect(img.className).toEqual('lorem'); + + Simulate.load(img); + + img = findRenderedDOMComponentWithTag(lazyLoadImage.instance(), 'img'); + + expect(img.className).toEqual('ipsum'); + }); + it('adds the effect class', function() { const lazyLoadImage = mount( @@ -142,4 +158,20 @@ describe('LazyLoadImage', function() { expect(span.length).toEqual(0); }); + + it('renders placeholder with correct style', function() { + const lazyLoadImage = mount( + + ); + + const span = findRenderedDOMComponentWithTag(lazyLoadImage.instance(), 'span'); + + expect(span.style.getPropertyValue('display')).toEqual('block'); + expect(span.getAttribute('aria-hidden')).toEqual('true'); + }); });