UNPKG

@eccenca/gui-elements

Version:

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

87 lines 5.26 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; }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; import React, { useEffect, useState } from "react"; import { CLASSPREFIX as eccgui } from "../../configuration/constants.js"; import Spinner from "../Spinner/Spinner.js"; /** * Display iframe but shows a spinner as long as it is not loaded. */ export var Iframe = React.forwardRef(function (_a, ref) { var title = _a.title, _b = _a.className, className = _b === void 0 ? "" : _b, useViewportHeight = _a.useViewportHeight, _c = _a.useAvailableSpace, useAvailableSpace = _c === void 0 ? false : _c, _d = _a.useContentHeight, useContentHeight = _d === void 0 ? false : _d, _e = _a.backgroundColor, backgroundColor = _e === void 0 ? "" : _e, _f = _a.htmlIframeProps, htmlIframeProps = _f === void 0 ? {} : _f, otherReactProps = __rest(_a, ["title", "className", "useViewportHeight", "useAvailableSpace", "useContentHeight", "backgroundColor", "htmlIframeProps"]); var _g = __read(useState(false), 2), isLoaded = _g[0], setIsLoaded = _g[1]; var _h = __read(useState(undefined), 2), contentHeight = _h[0], setContentHeight = _h[1]; var newRef = React.useRef(null); useEffect(function () { var _a, _b, _c, _d, _e, _f, _g, _h, _j; var iframeRef = ref !== null && ref !== void 0 ? ref : newRef; if (iframeRef && "current" in iframeRef && iframeRef.current) { if (!!backgroundColor && isLoaded) { var iframeDocStyle = (_c = (_b = (_a = iframeRef === null || iframeRef === void 0 ? void 0 : iframeRef.current) === null || _a === void 0 ? void 0 : _a.contentDocument) === null || _b === void 0 ? void 0 : _b.documentElement) === null || _c === void 0 ? void 0 : _c.style; var iframeBodyStyle = (_f = (_e = (_d = iframeRef === null || iframeRef === void 0 ? void 0 : iframeRef.current) === null || _d === void 0 ? void 0 : _d.contentDocument) === null || _e === void 0 ? void 0 : _e.body) === null || _f === void 0 ? void 0 : _f.style; if (iframeDocStyle && iframeBodyStyle) { iframeDocStyle.backgroundColor = backgroundColor; iframeBodyStyle.backgroundColor = backgroundColor; } } setContentHeight((_j = (_h = (_g = iframeRef.current.contentWindow) === null || _g === void 0 ? void 0 : _g.document) === null || _h === void 0 ? void 0 : _h.body) === null || _j === void 0 ? void 0 : _j.scrollHeight); } }, [ref, isLoaded, backgroundColor]); var classNames = "".concat(eccgui, "-iframe") + (useViewportHeight ? " ".concat(eccgui, "-iframe--").concat(useViewportHeight, "height") : "") + (useAvailableSpace ? " ".concat(eccgui, "-iframe--useavailablespace") : ""); var _j = htmlIframeProps.onLoad, onLoad = _j === void 0 ? function () { } : _j, style = htmlIframeProps.style, otherOriginalIframeProps = __rest(htmlIframeProps, ["onLoad", "style"]); return (React.createElement("div", { className: classNames }, !isLoaded && React.createElement(Spinner, null), React.createElement("iframe", __assign({ className: className !== null && className !== void 0 ? className : undefined, ref: ref !== null && ref !== void 0 ? ref : newRef, title: title }, otherOriginalIframeProps, otherReactProps, { onLoad: function (e) { setIsLoaded(true); onLoad(e); }, style: __assign(__assign(__assign({}, (style !== null && style !== void 0 ? style : {})), (!isLoaded ? { visibility: "hidden", position: "absolute", left: "-10000em", } : {})), (useContentHeight && !!contentHeight ? { height: "".concat(contentHeight, "px"), } : {})), scrolling: useContentHeight && !!contentHeight ? "no" : "yes" })))); }); export default Iframe; //# sourceMappingURL=Iframe.js.map