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.

148 lines (147 loc) 6.03 kB
import * as React from "react"; import styled, { keyframes, css } from "styled-components"; import defaultTheme from "../defaultTheme"; import TYPE_OPTIONS from "./consts"; import { left, right } from "../utils/rtl"; const getHeight = ({ type, customSize }) => { const tokens = { [TYPE_OPTIONS.BUTTON_LOADER]: "100%", [TYPE_OPTIONS.SEARCH_LOADER]: "40px", [TYPE_OPTIONS.BOX_LOADER]: "80px", [TYPE_OPTIONS.PAGE_LOADER]: "120px", [TYPE_OPTIONS.INLINE_LOADER]: "19px" }; if (customSize) return `height: ${customSize}px`; if (!type) return null; return css(["", ":", ""], type === TYPE_OPTIONS.INLINE_LOADER ? "min-height" : "height", tokens[type]); }; const getAlign = ({ type }) => { const tokens = { [TYPE_OPTIONS.BUTTON_LOADER]: "center", [TYPE_OPTIONS.SEARCH_LOADER]: "start", [TYPE_OPTIONS.BOX_LOADER]: "center", [TYPE_OPTIONS.PAGE_LOADER]: "center", [TYPE_OPTIONS.INLINE_LOADER]: "center" }; if (!type) return null; return tokens[type]; }; // Animations const SpinnerAnimation = keyframes(["100%{transform:rotate(360deg);}"]); const LoaderAnimation = keyframes(["0%{opacity:.3;transform:translateY(0px);}20%{opacity:1;transform:translateY(-3px);}40%{opacity:.3;transform:translateY(0px);}100%{opacity:.3;transform:translateY(0px);}"]); export const StyledLoading = styled(({ children, className, dataTest }) => /*#__PURE__*/React.createElement("div", { className: className, "data-test": dataTest }, children)).withConfig({ displayName: "Loading__StyledLoading", componentId: "sc-1t7r97l-0" })(["", ""], ({ type, theme, customSize }) => css(["position:", ";top:", ";", ":", ";width:", ";", ";padding:", ";display:", ";flex-direction:", ";justify-content:", ";align-items:center;overflow:hidden;box-sizing:border-box;"], type === TYPE_OPTIONS.BUTTON_LOADER && "absolute", type === TYPE_OPTIONS.BUTTON_LOADER && "0", left, type === TYPE_OPTIONS.BUTTON_LOADER && "0", type === TYPE_OPTIONS.BUTTON_LOADER && "100%", getHeight({ type, customSize }), type !== TYPE_OPTIONS.INLINE_LOADER && theme.orbit.paddingLoading, type === TYPE_OPTIONS.INLINE_LOADER ? "inline-flex" : "flex", type === TYPE_OPTIONS.PAGE_LOADER ? "column" : "row", getAlign({ type }))); StyledLoading.defaultProps = { theme: defaultTheme }; const StyledLoadingText = styled.div.withConfig({ displayName: "Loading__StyledLoadingText", componentId: "sc-1t7r97l-1" })(["", ";"], ({ theme, type }) => css(["font-family:", ";font-size:", ";color:", ";line-height:", ";margin-top:", ";margin-", ":", ";"], theme.orbit.fontFamily, theme.orbit.fontSizeTextNormal, theme.orbit.colorTextLoading, theme.orbit.lineHeightTextNormal, type === TYPE_OPTIONS.PAGE_LOADER && theme.orbit.spaceMedium, left, type !== TYPE_OPTIONS.PAGE_LOADER && theme.orbit.spaceSmall)); StyledLoadingText.defaultProps = { theme: defaultTheme }; export const StyledSpinner = styled.svg.withConfig({ displayName: "Loading__StyledSpinner", componentId: "sc-1t7r97l-2" })(["", ";"], ({ customSize }) => css(["width:", ";height:", ";animation:", " 0.75s linear infinite;"], customSize ? `${customSize}px` : "40px", customSize ? `${customSize}px` : "40px", SpinnerAnimation)); const StyledSpinnerCircle = styled.circle.withConfig({ displayName: "Loading__StyledSpinnerCircle", componentId: "sc-1t7r97l-3" })(["", ";"], ({ type, theme, customSize }) => css(["fill:transparent;stroke:", ";stroke-width:3px;stroke-linecap:round;stroke-dasharray:", ";stroke-dashoffset:", ";"], type === TYPE_OPTIONS.BUTTON_LOADER ? "currentColor" : theme.orbit.paletteCloudDark, customSize ? `${customSize * 3 + 8}px` : "128px", customSize ? `${customSize + 24}px` : "64px")); StyledSpinnerCircle.defaultProps = { theme: defaultTheme }; const StyledLoader = styled.div.withConfig({ displayName: "Loading__StyledLoader", componentId: "sc-1t7r97l-4" })(["display:flex;justify-content:center;align-items:center;"]); const StyledLoaderCircle = styled.div.withConfig({ displayName: "Loading__StyledLoaderCircle", componentId: "sc-1t7r97l-5" })(["width:8px;height:8px;border-radius:50%;margin-", ":6px;background:", ";animation:", " 1.25s infinite ease-in-out;&:nth-child(2){animation-delay:0.1s;}&:nth-child(3){animation-delay:0.2s;margin:0;}"], right, ({ theme }) => theme.orbit.paletteCloudDark, LoaderAnimation); StyledLoaderCircle.defaultProps = { theme: defaultTheme }; const Loader = ({ type, customSize }) => { const isCircledIcon = type === TYPE_OPTIONS.BOX_LOADER || type === TYPE_OPTIONS.SEARCH_LOADER || type === TYPE_OPTIONS.INLINE_LOADER; if (customSize) { return /*#__PURE__*/React.createElement(StyledSpinner, { viewBox: `0 0 ${customSize} ${customSize}`, customSize: customSize }, /*#__PURE__*/React.createElement(StyledSpinnerCircle, { cx: "50%", cy: "50%", r: customSize ? customSize / 2 - 2 : 18, customSize: customSize })); } if (isCircledIcon) return /*#__PURE__*/React.createElement(StyledLoader, null, /*#__PURE__*/React.createElement(StyledLoaderCircle, null), /*#__PURE__*/React.createElement(StyledLoaderCircle, null), /*#__PURE__*/React.createElement(StyledLoaderCircle, null)); return /*#__PURE__*/React.createElement(StyledSpinner, { viewBox: "0 0 40 40" }, /*#__PURE__*/React.createElement(StyledSpinnerCircle, { cx: "50%", cy: "50%", r: "18", type: type })); }; const Loading = ({ loading = false, type = TYPE_OPTIONS.PAGE_LOADER, text, children, dataTest, customSize, id }) => { return /*#__PURE__*/React.createElement(React.Fragment, null, children && !loading ? children : /*#__PURE__*/React.createElement(StyledLoading, { type: type, dataTest: dataTest, id: id }, /*#__PURE__*/React.createElement(Loader, { type: type, customSize: customSize }), type !== TYPE_OPTIONS.BUTTON_LOADER && text && /*#__PURE__*/React.createElement(StyledLoadingText, { type: type }, text))); }; Loading.displayName = "Loading"; export default Loading;