image.js 827 B

123456789101112131415161718192021222324252627282930313233
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import Blurhash from './blurhash';
  4. import classNames from 'classnames';
  5. export default class Image extends React.PureComponent {
  6. static propTypes = {
  7. src: PropTypes.string,
  8. srcSet: PropTypes.string,
  9. blurhash: PropTypes.string,
  10. className: PropTypes.string,
  11. };
  12. state = {
  13. loaded: false,
  14. };
  15. handleLoad = () => this.setState({ loaded: true });
  16. render () {
  17. const { src, srcSet, blurhash, className } = this.props;
  18. const { loaded } = this.state;
  19. return (
  20. <div className={classNames('image', { loaded }, className)} role='presentation'>
  21. {blurhash && <Blurhash hash={blurhash} className='image__preview' />}
  22. <img src={src} srcSet={srcSet} alt='' onLoad={this.handleLoad} />
  23. </div>
  24. );
  25. }
  26. }