fix image fallback bug

This commit is contained in:
Ren Amamiya
2023-06-22 17:36:12 +07:00
parent a71e2991c2
commit 36888221ff
28 changed files with 151 additions and 52 deletions

View File

@@ -1,15 +1,19 @@
import { DEFAULT_AVATAR } from "@stores/constants";
import { ImgHTMLAttributes } from "react";
export function Image(props, fallback?) {
const addImageFallback = (event: { currentTarget: { src: string } }) => {
event.currentTarget.src = fallback || DEFAULT_AVATAR;
};
interface Props extends ImgHTMLAttributes<any> {
fallback: string;
}
export function Image({ src, fallback, ...props }: Props) {
return (
<img
{...props}
src={src || fallback}
onError={({ currentTarget }) => {
currentTarget.onerror = null;
currentTarget.src = fallback;
}}
decoding="async"
onError={addImageFallback}
alt="lume default img"
style={{ contentVisibility: "auto" }}
/>