import React, { useCallback, useEffect, useRef } from "react"; import SVG from "react-inlinesvg"; import Color, { ColorLike } from "color"; import { CLASSPREFIX as eccgui } from "../../configuration/constants"; import { BadgeProps } from "../Badge/Badge"; import { IconProps } from "../Icon/Icon"; import { TestIconProps } from "../Icon/TestIcon"; import Tooltip, { TooltipProps } from "../Tooltip/Tooltip"; import decideContrastColorValue from "./../../common/utils/colorDecideContrastvalue"; export interface DepictionProps extends React.HTMLAttributes { /** * Image that should be used as depiction. */ image: React.ReactElement< IconProps | TestIconProps | React.ImgHTMLAttributes | React.SVGProps >; /** * In case you use an SVG encoded as a data URL in the `` element, then it is transformed to a inline SVG inside the DOM tree. * Should be work with Base64 and URL encoded data URIs. */ forceInlineSvg?: boolean; /** * Sets the height of the depiction, not the dimension (width x height). */ size?: "tiny" | "small" | "medium" | "large" | "xlarge" | "source"; /** * Resizing strategy for image to match the given ratio. * * contain: image is fully visible in the depiction * * cover: the image fully covers the depition area but it may displayed only partially * * stretch: image is streched to fill the depiction area */ resizing?: "contain" | "cover" | "stretch"; /** * Aspect ration of the depiction. */ ratio?: "1:1" | "source"; // | "3:2" | "5:4" | "16:9" /** * Use a fully rounded shape on the depiction edges. * Combined with `ratio="1:1"` its displayed within a circular shape. */ rounded?: boolean; /** * Color that is used for the depiction background. * This may be important if you use PNG, SVG or other image types that can have transparent background areas. */ backgroundColor?: ColorLike | "light" | "dark"; /** * The depiction is displayed with a border around it. */ border?: boolean; /** * Add padding around the image inside the depiction. * The amount of padding is defined relative to the depiction size, so a small padding on a small depiction is displayed smaller than a small padding on a large depiction. */ padding?: "none" | "tiny" | "small" | "medium" | "large"; /** * Reduce opacity to let it appear as inactive. * Even if it is no form control element it could be used inside one. * Use this property if the `disabled` state there is not adapted automatically to the depiction. */ disabled?: boolean; /** * Description of the depiction. */ caption?: string | React.JSX.Element; /** * How is the caption displayed. */ captionPosition?: "none" | "tooltip"; /** * In case of `captionPosition="tooltip"` this can be used to set the properties of the Tooltip element. */ tooltipProps?: TooltipProps; /** * Attach a `` element to the depiction. */ badge?: React.ReactElement; } /** * Display a graphical representation and attache a caption or a badge to it. */ export function Depiction({ className = "", image, forceInlineSvg = false, size = "medium", resizing = "cover", ratio = "source", caption, captionPosition = "none", backgroundColor, border, rounded, padding = "none", disabled, badge, tooltipProps, ...otherFigureProps }: DepictionProps) { const containerRef = useRef(null); let styleDepictionColors = Object.create(null); if (!!backgroundColor && backgroundColor !== "light" && backgroundColor !== "dark") { try { const color = Color(backgroundColor); styleDepictionColors = { [`--${eccgui}-depiction-background`]: color.rgb().toString(), [`--${eccgui}-depiction-color`]: decideContrastColorValue({ testColor: color }), }; } catch { // eslint-disable-next-line no-console console.warn("Received invalid background color for depiction: " + backgroundColor); } } const updateSvgResizing = React.useCallback( (el: SVGElement) => { let preserveAspectRatio = ""; switch (resizing) { case "cover": preserveAspectRatio = "xMidYMid slice"; break; case "stretch": preserveAspectRatio = "none"; break; } el.setAttribute("preserveAspectRatio", preserveAspectRatio); }, [resizing], ); const inlineSvgCall = useCallback( (svgElement: SVGElement) => { if (svgElement) { updateSvgResizing(svgElement); } }, [updateSvgResizing], ); useEffect(() => { // Resize element after every render const svgElement = containerRef.current!.getElementsByTagName("svg"); if (svgElement.length > 0) { updateSvgResizing(svgElement[0]); } }); let depiction = image; if ( forceInlineSvg && image.type === "img" && "src" in image.props && !!image.props.src && image.props.src.startsWith("data:image/svg+xml") ) { depiction = ( {image} ); } const depictionContainer = (
{depiction}
); return (
{captionPosition === "tooltip" && !!caption ? ( {depictionContainer} ) : ( depictionContainer )} {!!caption && (
{caption}
)} {badge}
); }