UNPKG

@eccenca/gui-elements

Version:

GUI elements based on other libraries, usable in React application, written in Typescript.

99 lines 5.71 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import React, { useCallback, useEffect, useRef } from "react"; import SVG from "react-inlinesvg"; import Color from "color"; import { CLASSPREFIX as eccgui } from "../../configuration/constants.js"; import Tooltip from "../Tooltip/Tooltip.js"; import decideContrastColorValue from "./../../common/utils/colorDecideContrastvalue.js"; /** * Display a graphical representation and attache a caption or a badge to it. */ export function Depiction(_a) { var _b; var _c = _a.className, className = _c === void 0 ? "" : _c, image = _a.image, _d = _a.forceInlineSvg, forceInlineSvg = _d === void 0 ? false : _d, _e = _a.size, size = _e === void 0 ? "medium" : _e, _f = _a.resizing, resizing = _f === void 0 ? "cover" : _f, _g = _a.ratio, ratio = _g === void 0 ? "source" : _g, caption = _a.caption, _h = _a.captionPosition, captionPosition = _h === void 0 ? "none" : _h, backgroundColor = _a.backgroundColor, border = _a.border, rounded = _a.rounded, _j = _a.padding, padding = _j === void 0 ? "none" : _j, disabled = _a.disabled, badge = _a.badge, tooltipProps = _a.tooltipProps, otherFigureProps = __rest(_a, ["className", "image", "forceInlineSvg", "size", "resizing", "ratio", "caption", "captionPosition", "backgroundColor", "border", "rounded", "padding", "disabled", "badge", "tooltipProps"]); var containerRef = useRef(null); var styleDepictionColors = Object.create(null); if (!!backgroundColor && backgroundColor !== "light" && backgroundColor !== "dark") { try { var color = Color(backgroundColor); styleDepictionColors = (_b = {}, _b["--".concat(eccgui, "-depiction-background")] = color.rgb().toString(), _b["--".concat(eccgui, "-depiction-color")] = decideContrastColorValue({ testColor: color }), _b); } catch (ex) { // eslint-disable-next-line no-console console.warn("Received invalid background color for depiction: " + backgroundColor); } } var updateSvgResizing = React.useCallback(function (el) { var preserveAspectRatio = ""; switch (resizing) { case "cover": preserveAspectRatio = "xMidYMid slice"; break; case "stretch": preserveAspectRatio = "none"; break; } el.setAttribute("preserveAspectRatio", preserveAspectRatio); }, [resizing]); var inlineSvgCall = useCallback(function (svgElement) { if (svgElement) { updateSvgResizing(svgElement); } }, [updateSvgResizing]); useEffect(function () { // Resize element after every render var svgElement = containerRef.current.getElementsByTagName("svg"); if (svgElement.length > 0) { updateSvgResizing(svgElement[0]); } }); var depiction = image; if (forceInlineSvg && image.type === "img" && "src" in image.props && !!image.props.src && image.props.src.startsWith("data:image/svg+xml")) { depiction = (React.createElement(SVG, { src: image.props.src, innerRef: inlineSvgCall }, image)); } var depictionContainer = (React.createElement("div", { ref: containerRef, className: "".concat(eccgui, "-depiction__image") + " ".concat(eccgui, "-depiction__image--").concat(size) + " ".concat(eccgui, "-depiction__image--").concat(resizing, "-sizing") + " ".concat(eccgui, "-depiction__image--ratio-").concat(ratio.replace(":", "to")) + (backgroundColor === "light" || backgroundColor === "dark" ? " ".concat(eccgui, "-depiction__image--color-").concat(backgroundColor) : "") + (backgroundColor ? " ".concat(eccgui, "-depiction__image--color-config") : "") + (border ? " ".concat(eccgui, "-depiction__image--hasborder") : "") + (rounded ? " ".concat(eccgui, "-depiction__image--roundedborder") : "") + (padding && padding !== "none" ? " ".concat(eccgui, "-depiction__image--padding-").concat(padding) : "") + (disabled ? " ".concat(eccgui, "-depiction__image--disabled") : ""), style: styleDepictionColors }, depiction)); return (React.createElement("figure", __assign({ className: "".concat(eccgui, "-depiction") + (className ? " ".concat(className) : "") }, otherFigureProps), captionPosition === "tooltip" && !!caption ? (React.createElement(Tooltip, __assign({ content: caption, size: "medium" }, tooltipProps), depictionContainer)) : (depictionContainer), !!caption && (React.createElement("figcaption", { className: "".concat(eccgui, "-depiction__caption") + " ".concat(eccgui, "-depiction__caption--").concat(captionPosition) }, caption)), badge)); } //# sourceMappingURL=Depiction.js.map