import React from 'react'; import clsx from 'clsx'; import { ComponentLoader } from 'app/components/ui/loader'; import { SKIN_LIGHT } from 'app/components/ui'; import styles from './imageLoader.scss'; export default class ImageLoader extends React.Component< { src: string; alt: string; ratio: number; // width:height ratio onLoad?: Function; }, { isLoading: boolean; } > { state = { isLoading: true, }; componentDidMount() { this.preloadImage(); } preloadImage() { const img = new Image(); img.onload = () => this.imageLoaded(); img.onerror = () => this.preloadImage(); img.src = this.props.src; } imageLoaded() { this.setState({ isLoading: false }); if (this.props.onLoad) { this.props.onLoad(); } } render() { const { isLoading } = this.state; const { src, alt, ratio } = this.props; return (
{isLoading && (
)}
{alt}
); } }