All files / app/features/dashboard/Hero HeroImage.tsx

0% Statements 0/14
100% Branches 0/0
0% Functions 0/5
0% Lines 0/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 73 74 75 76                                                                                                                                                       
import { styled } from "@mui/material";
import React from "react";
 
const ImageOuterContainer = styled("div")(() => ({
  position: "absolute",
  top: 0,
  left: 0,
  width: "100%",
  height: "100%",
  overflow: "hidden",
  display: "block",
}));
 
const PlaceholderLayer = styled("div")<{ placeholderSrc: string }>(
  ({ placeholderSrc }) => ({
    position: "absolute",
    top: 0,
    left: 0,
    width: "100%",
    height: "100%",
    backgroundImage: `url(${placeholderSrc})`,
    backgroundSize: "cover",
    backgroundPosition: "center",
    zIndex: -1,
  }),
);
 
const StyledActualImage = styled("img")(() => ({
  position: "absolute",
  top: 0,
  left: 0,
  width: "100%",
  height: "100%",
  display: "block",
  objectFit: "cover",
  objectPosition: "50% 50%",
  zIndex: 0,
}));
 
interface ImageWidth {
  width: number;
  fileName: string;
}
 
interface HeroImageProps {
  alt: string;
  placeHolderSrc: string;
  imageWidths: ImageWidth[];
}
 
const HeroImage: React.FC<HeroImageProps> = ({
  alt,
  placeHolderSrc,
  imageWidths,
}) => {
  const srcset = imageWidths
    .map(({ width, fileName }) => `${fileName} ${width}w`)
    .join(", ");
 
  const primarySrc = `${imageWidths[imageWidths.length - 1].fileName}`;
 
  return (
    <ImageOuterContainer>
      <PlaceholderLayer placeholderSrc={placeHolderSrc} />
      <StyledActualImage
        src={primarySrc}
        srcSet={srcset}
        sizes="100vw"
        alt={alt}
      />
    </ImageOuterContainer>
  );
};
 
export default HeroImage;