2017-11-20 01:47:23 +05:30
|
|
|
import React from 'react';
|
|
|
|
import classNames from 'classnames';
|
2019-12-08 00:32:00 +05:30
|
|
|
import { ComponentLoader } from 'app/components/ui/loader';
|
|
|
|
import { SKIN_LIGHT } from 'app/components/ui';
|
2017-11-20 01:47:23 +05:30
|
|
|
|
|
|
|
import styles from './imageLoader.scss';
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
export default class ImageLoader extends React.Component<
|
|
|
|
{
|
2019-12-07 16:58:52 +05:30
|
|
|
src: string;
|
|
|
|
alt: string;
|
|
|
|
ratio: number; // width:height ratio
|
|
|
|
onLoad?: Function;
|
2019-11-27 14:33:32 +05:30
|
|
|
},
|
|
|
|
{
|
2019-12-07 16:58:52 +05:30
|
|
|
isLoading: boolean;
|
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
> {
|
|
|
|
state = {
|
|
|
|
isLoading: true,
|
|
|
|
};
|
2017-11-20 01:47:23 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
componentWillMount() {
|
|
|
|
this.preloadImage();
|
|
|
|
}
|
2017-11-20 01:47:23 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
preloadImage() {
|
|
|
|
const img = new Image();
|
|
|
|
img.onload = () => this.imageLoaded();
|
|
|
|
img.onerror = () => this.preloadImage();
|
|
|
|
img.src = this.props.src;
|
|
|
|
}
|
2017-11-20 01:47:23 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
imageLoaded() {
|
|
|
|
this.setState({ isLoading: false });
|
2017-11-20 01:47:23 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
if (this.props.onLoad) {
|
|
|
|
this.props.onLoad();
|
2017-11-20 01:47:23 +05:30
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
}
|
2017-11-20 01:47:23 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
render() {
|
|
|
|
const { isLoading } = this.state;
|
|
|
|
const { src, alt, ratio } = this.props;
|
2017-11-20 01:47:23 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return (
|
|
|
|
<div className={styles.container}>
|
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
height: 0,
|
|
|
|
paddingBottom: `${ratio * 100}%`,
|
|
|
|
}}
|
|
|
|
/>
|
2017-11-20 01:47:23 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
{isLoading && (
|
|
|
|
<div className={styles.loader}>
|
|
|
|
<ComponentLoader skin={SKIN_LIGHT} />
|
|
|
|
</div>
|
|
|
|
)}
|
2017-11-20 01:47:23 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
<div
|
|
|
|
className={classNames(styles.image, {
|
|
|
|
[styles.imageLoaded]: !isLoading,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
<img src={src} alt={alt} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2017-11-20 01:47:23 +05:30
|
|
|
}
|