forked from mastodon/mastodon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove react-imageloader (mastodon#3423)
* Remove react-imageloader * add eslint-disable-line * improve image loading experience * remove unneeded import * use PureComponent * Use componentWillMount instead of constructor
- Loading branch information
1 parent
e49dc6a
commit c00ead8
Showing
5 changed files
with
57 additions
and
12 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
app/javascript/mastodon/features/ui/components/image_loader.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters