2023-05-08 05:12:44 -04:00
|
|
|
import React, { useCallback, useState } from 'react';
|
2023-05-08 21:11:56 -04:00
|
|
|
import { Blurhash } from './blurhash';
|
2023-05-08 05:12:44 -04:00
|
|
|
import classNames from 'classnames';
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
src: string;
|
|
|
|
srcSet?: string;
|
|
|
|
blurhash?: string;
|
|
|
|
className?: string;
|
2023-05-09 13:02:12 -04:00
|
|
|
};
|
2023-05-08 05:12:44 -04:00
|
|
|
|
2023-05-09 13:02:12 -04:00
|
|
|
export const Image: React.FC<Props> = ({
|
|
|
|
src,
|
|
|
|
srcSet,
|
|
|
|
blurhash,
|
|
|
|
className,
|
|
|
|
}) => {
|
2023-05-08 05:12:44 -04:00
|
|
|
const [loaded, setLoaded] = useState(false);
|
|
|
|
|
|
|
|
const handleLoad = useCallback(() => {
|
|
|
|
setLoaded(true);
|
|
|
|
}, [setLoaded]);
|
|
|
|
|
|
|
|
return (
|
2023-05-09 13:02:12 -04:00
|
|
|
<div
|
|
|
|
className={classNames('image', { loaded }, className)}
|
|
|
|
role='presentation'
|
|
|
|
>
|
2023-05-08 05:12:44 -04:00
|
|
|
{blurhash && <Blurhash hash={blurhash} className='image__preview' />}
|
|
|
|
<img src={src} srcSet={srcSet} alt='' onLoad={handleLoad} />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|