|
| 1 | +import classNames from 'classnames'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import React from 'react'; |
| 4 | + |
| 5 | +import ImagePreloader from '../ImagePreloader'; |
| 6 | +import { isVignetteUrl, vignette } from '../../utils/vignette'; |
| 7 | + |
| 8 | +const LAZY_IMAGE_SIZE = 5; |
| 9 | + |
| 10 | +class Image extends React.Component { |
| 11 | + static propTypes = { |
| 12 | + alt: PropTypes.string.isRequired, |
| 13 | + className: PropTypes.string, |
| 14 | + disableLazy: PropTypes.bool, |
| 15 | + src: PropTypes.string.isRequired, |
| 16 | + srcSet: PropTypes.string, |
| 17 | + }; |
| 18 | + |
| 19 | + static defaultProps = { |
| 20 | + className: undefined, |
| 21 | + disableLazy: false, |
| 22 | + srcSet: undefined, |
| 23 | + }; |
| 24 | + |
| 25 | + state = { |
| 26 | + src: this.props.src, |
| 27 | + isLimbo: false, |
| 28 | + }; |
| 29 | + |
| 30 | + // When the src changes first replace the src with a temp image so it doesn't stall displaying |
| 31 | + // the old image |
| 32 | + // https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html#when-to-use-derived-state |
| 33 | + static getDerivedStateFromProps(props, state) { |
| 34 | + // when only the src changes we are in "limbo" mode |
| 35 | + return { |
| 36 | + isLimbo: props.src !== state.src, |
| 37 | + }; |
| 38 | + } |
| 39 | + |
| 40 | + // after the component updates once we want to |
| 41 | + componentDidUpdate() { |
| 42 | + if (this.props.src !== this.state.src) { |
| 43 | + // this is one of the rare cases to conditionally call setState after a component update |
| 44 | + // this allows the images to be removed from the DOM properly |
| 45 | + // eslint-disable-next-line react/no-did-update-set-state |
| 46 | + this.setState(() => ({ src: this.props.src })); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Create a super low resolution image that will automatically be blurred in most browsers |
| 52 | + */ |
| 53 | + getLowResSrcFromVignette() { |
| 54 | + return vignette(this.props.src).withSmart(LAZY_IMAGE_SIZE, LAZY_IMAGE_SIZE).get(); |
| 55 | + } |
| 56 | + |
| 57 | + renderPlainImage() { |
| 58 | + const { src, alt, className, disableLazy, ...rest } = this.props; |
| 59 | + |
| 60 | + return <img className={classNames(className)} src={src} alt={alt} {...rest} />; |
| 61 | + } |
| 62 | + |
| 63 | + renderVignetteImage() { |
| 64 | + const { src: _skip1, srcSet: _skip2, alt, className, disableLazy, ...rest } = this.props; |
| 65 | + |
| 66 | + if (disableLazy) { |
| 67 | + return this.renderPlainImage(); |
| 68 | + } |
| 69 | + |
| 70 | + return ( |
| 71 | + <ImagePreloader src={this.state.src} srcSet={this.props.srcSet}> |
| 72 | + {({ src, srcSet, state }) => { |
| 73 | + // we will not test the functionality of ImagePreloader here |
| 74 | + /* istanbul ignore next */ |
| 75 | + if (state !== ImagePreloader.STATE.PENDING) { |
| 76 | + return <img className={classNames(className)} src={src} srcSet={srcSet} alt={alt} {...rest} />; |
| 77 | + } |
| 78 | + |
| 79 | + // if the image is loading, render low quality image |
| 80 | + return <img className={classNames(className)} src={this.getLowResSrcFromVignette()} alt={alt} {...rest} />; |
| 81 | + }} |
| 82 | + </ImagePreloader> |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + render() { |
| 87 | + const { src, isLimbo } = this.state; |
| 88 | + |
| 89 | + if (isVignetteUrl(src)) { |
| 90 | + // Limbo state happens when only the src and/or srcset is changed |
| 91 | + // there is no standard on how to handle the state of the image when the src is changed across browsers |
| 92 | + // lets just remove the entire node from html when in limbo |
| 93 | + return ( |
| 94 | + <React.Fragment> |
| 95 | + {!isLimbo && this.renderVignetteImage()} |
| 96 | + {/* // support no-js and SSR */} |
| 97 | + <noscript> |
| 98 | + {this.renderPlainImage()} |
| 99 | + </noscript> |
| 100 | + </React.Fragment> |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + // if the image is not a Vignette one, just render it and don't care |
| 105 | + return this.renderPlainImage(); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +export default Image; |
0 commit comments