Added onClick to Img component
This commit is contained in:
parent
f51f7322fe
commit
067f66b146
|
@ -2,41 +2,40 @@ import { UploadImageFragment } from "graphql/generated";
|
||||||
import { getAssetURL, getImgSizesByQuality, ImageQuality } from "helpers/img";
|
import { getAssetURL, getImgSizesByQuality, ImageQuality } from "helpers/img";
|
||||||
import { Immutable } from "helpers/types";
|
import { Immutable } from "helpers/types";
|
||||||
import { ImageProps } from "next/image";
|
import { ImageProps } from "next/image";
|
||||||
|
import { MouseEventHandler } from "react";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
className?: string;
|
className?: string;
|
||||||
image?: UploadImageFragment | string;
|
image?: UploadImageFragment | string;
|
||||||
quality?: ImageQuality;
|
quality?: ImageQuality;
|
||||||
alt?: ImageProps["alt"];
|
alt?: ImageProps["alt"];
|
||||||
|
onClick?: MouseEventHandler<HTMLImageElement>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Img(props: Immutable<Props>): JSX.Element {
|
export default function Img(props: Immutable<Props>): JSX.Element {
|
||||||
if (typeof props.image === "string") {
|
const {
|
||||||
|
className,
|
||||||
|
image,
|
||||||
|
quality = ImageQuality.Small,
|
||||||
|
alt,
|
||||||
|
onClick,
|
||||||
|
} = props;
|
||||||
|
|
||||||
|
if (typeof image === "string") {
|
||||||
|
return (
|
||||||
|
<img className={className} src={image} alt={alt ?? ""} loading="lazy" />
|
||||||
|
);
|
||||||
|
} else if (image?.width && image.height) {
|
||||||
|
const imgSize = getImgSizesByQuality(image.width, image.height, quality);
|
||||||
return (
|
return (
|
||||||
<img
|
<img
|
||||||
className={props.className}
|
className={className}
|
||||||
src={props.image}
|
src={getAssetURL(image.url, quality)}
|
||||||
alt={props.alt ?? ""}
|
alt={alt ?? image.alternativeText ?? ""}
|
||||||
loading="lazy"
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
} else if (props.image?.width && props.image.height) {
|
|
||||||
const imgSize = getImgSizesByQuality(
|
|
||||||
props.image.width,
|
|
||||||
props.image.height,
|
|
||||||
props.quality ?? ImageQuality.Small
|
|
||||||
);
|
|
||||||
return (
|
|
||||||
<img
|
|
||||||
className={props.className}
|
|
||||||
src={getAssetURL(
|
|
||||||
props.image.url,
|
|
||||||
props.quality ? props.quality : ImageQuality.Small
|
|
||||||
)}
|
|
||||||
alt={props.alt ?? props.image.alternativeText ?? ""}
|
|
||||||
width={imgSize.width}
|
width={imgSize.width}
|
||||||
height={imgSize.height}
|
height={imgSize.height}
|
||||||
loading="lazy"
|
loading="lazy"
|
||||||
|
onClick={onClick}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue