All files / src/sections/PayBuilder/components/loaders WithPlaceholder.tsx

100% Statements 14/14
66.66% Branches 12/18
100% Functions 2/2
100% Lines 14/14

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                          45x               1341x 1341x 1341x   1341x 111x 111x 111x 111x 111x   111x 37x 37x           1341x                                                                  
import FadeUpWrapper from "@components/animation/FadeUpWrapper";
import { Box, Skeleton } from "@mui/material";
import { CSSProperties, useEffect, useRef } from "react";
 
interface Props {
  children: React.ReactNode;
  isLoading: boolean;
  width?: string | number;
  childrenStyle?: CSSProperties;
  index: number;
  height?: string | number;
}
 
export const WithPlaceholder = ({
  children,
  isLoading,
  width,
  childrenStyle = {},
  index,
  height,
}: Props) => {
  const ref = useRef<HTMLDivElement>();
  const childrenHeight = useRef(0);
  const childrenWidth = useRef<number | string>(0);
 
  useEffect(() => {
    Eif (ref.current) {
      const clientRect = ref.current.getBoundingClientRect();
      Eif (clientRect) {
        childrenHeight.current = clientRect.height;
        childrenWidth.current = clientRect.width;
 
        if (childrenStyle.display === "none") {
          childrenHeight.current = 224;
          childrenWidth.current = "100%";
        }
      }
    }
  }, [ref]);
 
  return (
    <Box>
      <Box
        ref={ref}
        sx={
          isLoading
            ? {
                visibility: "hidden",
                position: "absolute",
                zIndex: "-1",
                ...childrenStyle,
              }
            : {}
        }
      >
        {children}
      </Box>
      {isLoading && (
        <FadeUpWrapper x={0} delay={index * 200}>
          <Skeleton
            variant="rectangular"
            width={width ?? childrenWidth.current}
            height={height ?? childrenHeight.current ?? 0}
            sx={{
              borderRadius: "12px",
              visibility: isLoading ? "visible" : "hidden",
            }}
          />
        </FadeUpWrapper>
      )}
    </Box>
  );
};