import { Fragment, useEffect, useMemo } from "@wordpress/element"; import { SmartImageCaption } from "./smartImgTemplate"; const RenderSmartImage = ({ attributes, setAttributes, setIsPreloader }) => { const { uniqueId, selectImage, imageSize, doubleResolutionRetina, lazyLoad, aspectRatio, enableLink, linkType, buttonLabel, buttonPosition, imageShapeSet, imgTextPosition, imgAnimationNormal, imgMaskingEnable, imgHoverOverlayEnable, imageAltText, hideOnDesktop, hideOnTablet, hideOnMobile, smartImgBgEnable, imgAlignment } = attributes; const imgSelectSizeObj = useMemo(() => { return selectImage?.media_details?.sizes?.[imageSize] || null; // eslint-disable-next-line react-hooks/exhaustive-deps }, [selectImage.id, imageSize]); const imgSrcSetRetina = useMemo(() => { if (!selectImage?.srcset) { return null; } if (!doubleResolutionRetina) { return selectImage.srcset; } return selectImage.srcset .split(",") .map((src) => src.trim()) .filter((src) => { const size = parseInt(src.match(/(\d+)w$/)?.[1] || 0, 10); return size >= 600; // keep only >=600px }) .join(", "); }, [selectImage?.srcset, doubleResolutionRetina]); useEffect(() => { if (imgSelectSizeObj?.source_url || selectImage?.url) { setIsPreloader(false); } else { setIsPreloader(true); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [imgSelectSizeObj?.source_url, selectImage?.url]); const ImgLinkTag = enableLink && "full-img" === linkType ? "a" : Fragment; const visibilityClass = `${hideOnDesktop ? " sp-hide-on-desktop " : ""}${ hideOnTablet ? " sp-hide-on-tablet " : "" }${hideOnMobile ? " sp-hide-on-mobile" : ""}`; const imgMaskClass = imgMaskingEnable && "custom" === imageShapeSet ? " sp-custom-mask" : ""; return ( <>
{imageAltText}
{imgHoverOverlayEnable && (
)}
{enableLink && "button" === linkType && (
{buttonLabel}
)} {"over-img" === imgTextPosition && ( )}
{"over-img" !== imgTextPosition && ( )}
); }; export default RenderSmartImage;