import { useEffect, useState, type ImgHTMLAttributes, type ReactNode } from "react"; import { cn } from "../../lib/cn"; export type ImageFit = "contain" | "cover" | "fill" | "none" | "scale-down"; export type ImageProps = Omit, "height" | "width"> & { aspectRatio?: number | string; fallback?: ReactNode; fit?: ImageFit; frameClassName?: string; height?: number | string; responsive?: boolean; width?: number | string; }; export function Image({ alt, aspectRatio, className, fallback, fit = "cover", frameClassName, height, onError, onLoad, responsive = true, src, style, width, ...props }: ImageProps) { const [failed, setFailed] = useState(false); useEffect(() => { setFailed(false); }, [src]); return (
{failed ? ( fallback ??
Unable to load image
) : ( {alt} { setFailed(true); onError?.(event); }} onLoad={onLoad} src={src} style={{ objectFit: fit, ...style }} width={width} {...props} /> )}
); }