UNPKG

@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.

123 lines (118 loc) 3.55 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; const _excluded = ["animate", "children", "rowBorderRadius", "rowHeight", "rowOffset", "rows", "title", "viewBox", "height", "maxHeight", "width"]; 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, viewBox }) => /*#__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" })(["", ""], ({ rtl, width, maxHeight }) => 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 }; const Rows = ({ count, height, offset, rowBorderRadius }) => { return [...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 = (_ref) => { let { animate = true, children, rowBorderRadius = 3, rowHeight = 21, rowOffset = 20, rows, title = "loading", viewBox, height, maxHeight, width = "100%" } = _ref, props = _objectWithoutProperties(_ref, _excluded); 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.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;