Files
lume/src/shared/image.tsx
Ren Amamiya bac70b19ec polish
2023-08-19 15:27:10 +07:00

29 lines
753 B
TypeScript

import { minidenticon } from 'minidenticons';
import { ImgHTMLAttributes, useState } from 'react';
export function Image({ src, ...props }: ImgHTMLAttributes<HTMLImageElement>) {
const [isError, setIsError] = useState(false);
if (isError || !src) {
const svgURI =
'data:image/svg+xml;utf8,' + encodeURIComponent(minidenticon(props.alt, 90, 50));
return (
<img src={svgURI} alt={props.alt} {...props} style={{ backgroundColor: '#000' }} />
);
}
return (
<img
{...props}
src={src}
onError={({ currentTarget }) => {
currentTarget.onerror = null;
setIsError(true);
}}
decoding="async"
alt="lume default img"
style={{ contentVisibility: 'auto' }}
/>
);
}