diff options
author | Nolan Lawson <nolan@nolanlawson.com> | 2017-05-31 08:07:25 -0700 |
---|---|---|
committer | Eugen Rochko <eugen@zeonfederated.com> | 2017-05-31 17:07:25 +0200 |
commit | c00ead8a72dd738013b8a132eff7de4959e59670 (patch) | |
tree | b936aa0906b8850047557677f428edd9e873391c /app/javascript/mastodon/features/ui/components/image_loader.js | |
parent | e49dc6a06eb18af023da82d60e485cd3c382fc96 (diff) |
Remove react-imageloader (#3423)
* Remove react-imageloader * add eslint-disable-line * improve image loading experience * remove unneeded import * use PureComponent * Use componentWillMount instead of constructor
Diffstat (limited to 'app/javascript/mastodon/features/ui/components/image_loader.js')
-rw-r--r-- | app/javascript/mastodon/features/ui/components/image_loader.js | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/app/javascript/mastodon/features/ui/components/image_loader.js b/app/javascript/mastodon/features/ui/components/image_loader.js new file mode 100644 index 000000000..af2870517 --- /dev/null +++ b/app/javascript/mastodon/features/ui/components/image_loader.js @@ -0,0 +1,45 @@ +import React from 'react'; +import PropTypes from 'prop-types'; + +class ImageLoader extends React.PureComponent { + + static propTypes = { + src: PropTypes.string.isRequired, + } + + state = { + loading: true, + error: false, + } + + componentWillMount() { + this.loadImage(this.props.src); + } + + componentWillReceiveProps(props) { + this.loadImage(props.src); + } + + loadImage(src) { + const image = new Image(); + image.onerror = () => this.setState({loading: false, error: true}); + image.onload = () => this.setState({loading: false, error: false}); + image.src = src; + this.lastSrc = src; + this.setState({loading: true}); + } + + render() { + const { src } = this.props; + const { loading, error } = this.state; + + // TODO: handle image error state + + const imageClass = `image-loader__img ${loading ? 'image-loader__img-loading' : ''}`; + + return <img className={imageClass} src={src} />; // eslint-disable-line jsx-a11y/img-has-alt + } + +} + +export default ImageLoader; |