import type { FC, ReactElement } from "react"; import { useMemo } from "react"; import ContentLoader from "react-content-loader"; import type { IContentLoaderProps } from "react-content-loader"; import type { IContentPlaceholder } from "./config/types"; import { getLinesProp } from "./utils/index"; import type { IComponentBaseProps } from "../../types/components/index"; import { useCN } from "../../utils/react/useCN"; import "./style.pcss"; const loaderModule = ContentLoader as FC & { default?: FC; }; const Loader: FC = process.env.NODE_ENV === "test" && loaderModule.default ? loaderModule.default : loaderModule; /** * Компонент заглушки * @returns {ReactElement} */ const ContentPlaceholder: FC = ({ baseClass = "contentPlaceholder", children = null, extraClasses = {}, extraAttrs = {}, speed = 2, backgroundColor = "#ecebeb", foregroundColor = "#f3f3f3", width = 400, height = 264, preserveAspectRatio = "none", lines = 5, linesHeight = 40, linesBorderRadius = 8, linesMargin = 16, linesWidth = [ 400, 360, 320, 380, 240 ], }): ReactElement => { const { getCN } = useCN(baseClass); const linesChildren = useMemo(() => { let offsetY = 0; return new Array(lines) .fill("") .map((_line, index) => { const mb = getLinesProp(linesMargin, index); const height = getLinesProp(linesHeight, index); const width = getLinesProp(linesWidth, index); const br = getLinesProp(linesBorderRadius, index); const y = offsetY; offsetY += height + mb; const lineKey = `line_${y}_${width}_${height}_${br}`; return ( ); }); }, [ lines, linesMargin, linesBorderRadius, linesWidth, linesHeight ]); return (
{children || linesChildren}
); }; export type { IContentPlaceholder }; export { ContentPlaceholder };