|
| 1 | +import React from 'react'; |
| 2 | +import { PropTypes } from 'prop-types'; |
| 3 | + |
| 4 | +class LazyLoadImage extends React.Component { |
| 5 | + constructor(props) { |
| 6 | + super(props); |
| 7 | + |
| 8 | + this.state = { |
| 9 | + visible: false |
| 10 | + } |
| 11 | + |
| 12 | + this.previousBoundingBox = { |
| 13 | + bottom: -1, |
| 14 | + top: -1 |
| 15 | + }; |
| 16 | + } |
| 17 | + |
| 18 | + componentDidMount() { |
| 19 | + this.updateVisibility(); |
| 20 | + } |
| 21 | + |
| 22 | + componentWillReceiveProps(nextProps) { |
| 23 | + this.updateVisibility(nextProps.scrollPosition); |
| 24 | + } |
| 25 | + |
| 26 | + componentDidUpdate(prevProps, prevState) { |
| 27 | + if (this.refs.placeholder) { |
| 28 | + const boundingBox = { |
| 29 | + bottom: this.refs.placeholder.offsetTop + |
| 30 | + this.refs.placeholder.offsetHeight, |
| 31 | + top: this.refs.placeholder.offsetTop |
| 32 | + }; |
| 33 | + |
| 34 | + if (this.previousBoundingBox.bottom !== boundingBox.bottom || |
| 35 | + this.previousBoundingBox.top !== boundingBox.top) { |
| 36 | + this.updateVisibility(); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + updateVisibility(scrollPosition = this.props.scrollPosition) { |
| 42 | + if (this.state.visible) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + this.setState({ |
| 47 | + visible: this.refs.image ? true : this.isImageInViewport(scrollPosition) |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + isImageInViewport(scrollPosition) { |
| 52 | + if (!this.refs.placeholder) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + const { threshold } = this.props; |
| 57 | + const boundingBox = { |
| 58 | + bottom: this.refs.placeholder.offsetTop + |
| 59 | + this.refs.placeholder.offsetHeight, |
| 60 | + top: this.refs.placeholder.offsetTop |
| 61 | + }; |
| 62 | + const viewport = { |
| 63 | + bottom: scrollPosition + window.innerHeight, |
| 64 | + top: scrollPosition |
| 65 | + }; |
| 66 | + |
| 67 | + this.previousBoundingBox = boundingBox; |
| 68 | + |
| 69 | + return Boolean(viewport.top - threshold <= boundingBox.bottom && |
| 70 | + viewport.bottom + threshold >= boundingBox.top); |
| 71 | + } |
| 72 | + |
| 73 | + render() { |
| 74 | + const { scrollPosition, threshold, ...props } = this.props; |
| 75 | + |
| 76 | + if (!this.state.visible) { |
| 77 | + const { className, height, width } = this.props; |
| 78 | + return ( |
| 79 | + <span className={'lazy-load-image-placeholder ' + className} |
| 80 | + ref="placeholder" |
| 81 | + style={{height, width}}> |
| 82 | + </span> |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + return ( |
| 87 | + <img |
| 88 | + {...props} |
| 89 | + ref="image" /> |
| 90 | + ); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +LazyLoadImage.propTypes = { |
| 95 | + scrollPosition: PropTypes.number.isRequired, |
| 96 | + className: PropTypes.string, |
| 97 | + height: PropTypes.number, |
| 98 | + threshold: PropTypes.number, |
| 99 | + width: PropTypes.number |
| 100 | +}; |
| 101 | + |
| 102 | +LazyLoadImage.defaultProps = { |
| 103 | + className: '', |
| 104 | + height: 0, |
| 105 | + threshold: 100, |
| 106 | + width: 0 |
| 107 | +}; |
| 108 | + |
| 109 | +export default LazyLoadImage; |
0 commit comments