File size: 1,849 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
import React from "react";
import NextImage, { StaticImageData } from "next/image";
import classes from "./index.module.scss";
import cssVariables from "../../../cssVariables";
import { Props } from "..";
const { breakpoints } = cssVariables;
export const Image: React.FC<Props> = (props) => {
const {
imgClassName,
onClick,
onLoad: onLoadFromProps,
resource,
priority,
fill,
src: srcFromProps,
alt: altFromProps,
} = props;
const [isLoading, setIsLoading] = React.useState(true);
let width: number | undefined;
let height: number | undefined;
let alt = altFromProps;
let src: StaticImageData | string = srcFromProps || "";
if (!src && resource && typeof resource !== "string") {
const {
width: fullWidth,
height: fullHeight,
filename: fullFilename,
alt: altFromResource,
} = resource;
width = fullWidth;
height = fullHeight;
alt = altFromResource;
let filename = fullFilename;
src = `https://${process.env.NEXT_PUBLIC_S3_HOSTNAME}/${process.env.NEXT_PUBLIC_S3_BUCKET}/${filename}`;
}
// NOTE: this is used by the browser to determine which image to download at different screen sizes
const sizes = Object.entries(breakpoints)
.map(([, value]) => `(max-width: ${value}px) ${value}px`)
.join(", ");
return (
<NextImage
className={[isLoading && classes.placeholder, classes.image, imgClassName]
.filter(Boolean)
.join(" ")}
src={src}
alt={alt || ""}
onClick={onClick}
onLoad={() => {
setIsLoading(false);
if (typeof onLoadFromProps === "function") {
onLoadFromProps();
}
}}
fill={fill}
width={!fill ? width : undefined}
height={!fill ? height : undefined}
sizes={sizes}
priority={priority}
/>
);
};
|