@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.
125 lines • 3.31 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
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";
const StyledSvg = styled(({
className,
children,
ariaLabelledby,
dataTest,
id,
viewBox
}) => /*#__PURE__*/React.createElement("svg", {
"aria-labelledby": ariaLabelledby,
"data-test": dataTest,
id: id,
className: className,
role: "img",
viewBox: viewBox
}, children)).withConfig({
displayName: "Svg__StyledSvg",
componentId: "sc-3u4dde-0"
})(["", ""], ({
rtl,
width,
duration,
animate,
interval,
maxHeight
}) => css(["height:", ";max-height:", ";width:", ";transform:", ";margin-bottom:", ";", ";"], resolveHeight, resolveValue(maxHeight), resolveValue(width), rtl && `scaleX(-1)`, getSpacingToken, resolvePulseAnimation({
duration,
interval,
animate
})));
StyledSvg.defaultProps = {
theme: defaultTheme
};
const Rows = ({
count,
height,
offset,
rowBorderRadius
}) => {
return /*#__PURE__*/React.createElement(React.Fragment, null, Array.from(Array(count).keys()).map(el => /*#__PURE__*/React.createElement("rect", {
key: el,
y: `${el * offset}px`,
x: "0",
rx: rowBorderRadius,
ry: rowBorderRadius,
width: "100%",
height: height
})));
};
const Svg = ({
animate = true,
children,
rowBorderRadius = 3,
rowHeight = 21,
rowOffset = 20,
rows,
color = "paletteCloudNormal",
title = "loading",
viewBox,
height,
maxHeight,
width = "100%",
...props
}) => {
const [calculatedHeight, setCalculatedHeight] = React.useState(0);
const {
rtl,
orbit
} = useTheme();
const duration = `${2}s`;
const interval = `${0.5}s`;
const id = useRandomIdSeed();
const idClip = `${id("clip")}-clip`;
const titleId = id("title");
React.useEffect(() => {
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(#${idClip})`,
style: {
fill: orbit[color]
}
}), /*#__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;