@kiwicom/orbit-components
Version:
Orbit-components is a React component library which provides developers with the easiest possible way of building Kiwi.com’s products.
135 lines (128 loc) • 4.87 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import * as React from "react";
import styled, { css } from "styled-components";
import { useRandomIdSeed } from "../hooks/useRandomId";
import { resolveHeight, resolveValue, resolvePulseAnimation } from "./helpers";
import useTheme from "../hooks/useTheme";
import defaultTheme from "../defaultTheme";
import getSpacingToken from "../common/getSpacingToken";
var StyledSvg = styled(function (_ref) {
var className = _ref.className,
children = _ref.children,
ariaLabelledby = _ref.ariaLabelledby,
dataTest = _ref.dataTest,
viewBox = _ref.viewBox;
return /*#__PURE__*/React.createElement("svg", {
"aria-labelledby": ariaLabelledby,
"data-test": dataTest,
className: className,
role: "img",
viewBox: viewBox
}, children);
}).withConfig({
displayName: "Svg__StyledSvg",
componentId: "sc-v381o6-0"
})(["", ""], function (_ref2) {
var rtl = _ref2.rtl,
width = _ref2.width,
maxHeight = _ref2.maxHeight;
return css(["height:", ";max-height:", ";width:", ";transform:", ";margin-bottom:", ";", ""], resolveHeight, resolveValue(maxHeight), resolveValue(width), rtl && "scaleX(-1)", getSpacingToken, resolvePulseAnimation);
}); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198
StyledSvg.defaultProps = {
theme: defaultTheme
};
var Rows = function Rows(_ref3) {
var count = _ref3.count,
height = _ref3.height,
offset = _ref3.offset,
rowBorderRadius = _ref3.rowBorderRadius;
return _toConsumableArray(Array(count).keys()).map(function (el) {
return /*#__PURE__*/React.createElement("rect", {
key: el,
y: "".concat(el * offset, "px"),
x: "0",
rx: rowBorderRadius,
ry: rowBorderRadius,
width: "100%",
height: height
});
});
};
var Svg = function Svg(_ref4) {
var _ref4$animate = _ref4.animate,
animate = _ref4$animate === void 0 ? true : _ref4$animate,
children = _ref4.children,
_ref4$rowBorderRadius = _ref4.rowBorderRadius,
rowBorderRadius = _ref4$rowBorderRadius === void 0 ? 3 : _ref4$rowBorderRadius,
_ref4$rowHeight = _ref4.rowHeight,
rowHeight = _ref4$rowHeight === void 0 ? 21 : _ref4$rowHeight,
_ref4$rowOffset = _ref4.rowOffset,
rowOffset = _ref4$rowOffset === void 0 ? 20 : _ref4$rowOffset,
rows = _ref4.rows,
_ref4$title = _ref4.title,
title = _ref4$title === void 0 ? "loading" : _ref4$title,
viewBox = _ref4.viewBox,
height = _ref4.height,
maxHeight = _ref4.maxHeight,
_ref4$width = _ref4.width,
width = _ref4$width === void 0 ? "100%" : _ref4$width,
props = _objectWithoutProperties(_ref4, ["animate", "children", "rowBorderRadius", "rowHeight", "rowOffset", "rows", "title", "viewBox", "height", "maxHeight", "width"]);
var _React$useState = React.useState(0),
_React$useState2 = _slicedToArray(_React$useState, 2),
calculatedHeight = _React$useState2[0],
setCalculatedHeight = _React$useState2[1];
var _useTheme = useTheme(),
rtl = _useTheme.rtl,
orbit = _useTheme.orbit;
var duration = "".concat(2, "s");
var interval = "".concat(0.5, "s");
var id = useRandomIdSeed();
var idClip = "".concat(id("clip"), "-clip");
var titleId = id("title");
React.useEffect(function () {
if (!children && rows && rowOffset) setCalculatedHeight(rows * rowOffset);
}, [rowOffset, rows, setCalculatedHeight, maxHeight]);
return /*#__PURE__*/React.createElement(StyledSvg, _extends({
ariaLabelledby: titleId,
maxHeight: maxHeight,
role: "img",
rtl: rtl,
viewBox: viewBox,
calculatedHeight: calculatedHeight,
duration: duration,
interval: interval,
animate: animate,
height: height,
width: width
}, props), /*#__PURE__*/React.createElement("title", {
id: titleId
}, title), /*#__PURE__*/React.createElement("rect", {
role: "presentation",
x: "0",
y: "0",
width: "100%",
height: "100%",
clipPath: "url(#".concat(idClip, ")"),
style: {
fill: orbit.paletteCloudDark
}
}), /*#__PURE__*/React.createElement("defs", null, /*#__PURE__*/React.createElement("clipPath", {
id: idClip
}, children || (rows ? /*#__PURE__*/React.createElement(Rows, {
count: rows,
height: rowHeight,
offset: rowOffset,
rowBorderRadius: rowBorderRadius
}) : /*#__PURE__*/React.createElement("rect", {
x: "0",
y: "0",
rx: rowBorderRadius,
ry: rowBorderRadius,
height: "100%",
width: "100%"
})))));
};
export default Svg;