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.

275 lines (241 loc) 12.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var React = _interopRequireWildcard(require("react")); var _styledComponents = _interopRequireDefault(require("styled-components")); var _defaultTheme = _interopRequireDefault(require("../defaultTheme")); var _InformationCircle = _interopRequireDefault(require("../icons/InformationCircle")); var _Check = _interopRequireDefault(require("../icons/Check")); var _Alert = _interopRequireDefault(require("../icons/Alert")); var _AlertCircle = _interopRequireDefault(require("../icons/AlertCircle")); var _Close = _interopRequireDefault(require("../icons/Close")); var _ButtonLink = _interopRequireDefault(require("../ButtonLink")); var _TextLink = require("../TextLink"); var _consts = require("./consts"); var _rtl = require("../utils/rtl"); var _getSpacingToken = _interopRequireDefault(require("../common/getSpacingToken")); var _ListItem = require("../List/ListItem"); var _Text = require("../Text"); var _Dictionary = require("../Dictionary"); var _Translate = require("../Translate"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var getTypeToken = function getTypeToken(name) { return function (_ref) { var _TOKENS$colorIconAler, _TOKENS$backgroundAle, _TOKENS$colorTextAler, _TOKENS$colorTextLink, _tokens; var theme = _ref.theme, type = _ref.type; var tokens = (_tokens = {}, _defineProperty(_tokens, _consts.TOKENS.colorIconAlert, (_TOKENS$colorIconAler = {}, _defineProperty(_TOKENS$colorIconAler, _consts.TYPE_OPTIONS.INFO, theme.orbit.colorAlertIconInfo), _defineProperty(_TOKENS$colorIconAler, _consts.TYPE_OPTIONS.SUCCESS, theme.orbit.colorAlertIconSuccess), _defineProperty(_TOKENS$colorIconAler, _consts.TYPE_OPTIONS.WARNING, theme.orbit.colorAlertIconWarning), _defineProperty(_TOKENS$colorIconAler, _consts.TYPE_OPTIONS.CRITICAL, theme.orbit.colorAlertIconCritical), _TOKENS$colorIconAler)), _defineProperty(_tokens, _consts.TOKENS.backgroundAlert, (_TOKENS$backgroundAle = {}, _defineProperty(_TOKENS$backgroundAle, _consts.TYPE_OPTIONS.INFO, theme.orbit.backgroundAlertInfo), _defineProperty(_TOKENS$backgroundAle, _consts.TYPE_OPTIONS.SUCCESS, theme.orbit.backgroundAlertSuccess), _defineProperty(_TOKENS$backgroundAle, _consts.TYPE_OPTIONS.WARNING, theme.orbit.backgroundAlertWarning), _defineProperty(_TOKENS$backgroundAle, _consts.TYPE_OPTIONS.CRITICAL, theme.orbit.backgroundAlertCritical), _TOKENS$backgroundAle)), _defineProperty(_tokens, _consts.TOKENS.colorTextAlert, (_TOKENS$colorTextAler = {}, _defineProperty(_TOKENS$colorTextAler, _consts.TYPE_OPTIONS.INFO, theme.orbit.colorTextAlertInfo), _defineProperty(_TOKENS$colorTextAler, _consts.TYPE_OPTIONS.SUCCESS, theme.orbit.colorTextAlertSuccess), _defineProperty(_TOKENS$colorTextAler, _consts.TYPE_OPTIONS.WARNING, theme.orbit.colorTextAlertWarning), _defineProperty(_TOKENS$colorTextAler, _consts.TYPE_OPTIONS.CRITICAL, theme.orbit.colorTextAlertCritical), _TOKENS$colorTextAler)), _defineProperty(_tokens, _consts.TOKENS.colorTextLinkAlertHover, (_TOKENS$colorTextLink = {}, _defineProperty(_TOKENS$colorTextLink, _consts.TYPE_OPTIONS.INFO, theme.orbit.paletteBlueDarkHover), _defineProperty(_TOKENS$colorTextLink, _consts.TYPE_OPTIONS.SUCCESS, theme.orbit.paletteGreenDarkHover), _defineProperty(_TOKENS$colorTextLink, _consts.TYPE_OPTIONS.WARNING, theme.orbit.paletteOrangeDarkHover), _defineProperty(_TOKENS$colorTextLink, _consts.TYPE_OPTIONS.CRITICAL, theme.orbit.paletteRedDarkActive), _TOKENS$colorTextLink)), _tokens); return tokens[name][type]; }; }; var Icon = function Icon(_ref2) { var icon = _ref2.icon, type = _ref2.type; // Icon should be boolean and TRUE if (typeof icon === "boolean" && icon) { if (type === _consts.TYPE_OPTIONS.INFO) { return React.createElement(_InformationCircle.default, null); } if (type === _consts.TYPE_OPTIONS.SUCCESS) { return React.createElement(_Check.default, null); } if (type === _consts.TYPE_OPTIONS.WARNING) { return React.createElement(_Alert.default, null); } if (type === _consts.TYPE_OPTIONS.CRITICAL) { return React.createElement(_AlertCircle.default, null); } } return icon; }; var StyledDiv = function StyledDiv(_ref3) { var className = _ref3.className, children = _ref3.children, dataTest = _ref3.dataTest; return React.createElement("div", { className: className, "data-test": dataTest }, children); }; var StyledAlert = (0, _styledComponents.default)(StyledDiv).withConfig({ displayName: "Alert__StyledAlert", componentId: "svgppc-0" })(["position:relative;display:flex;width:100%;padding:", ";border-radius:", ";background:", ";color:", ";font-family:", ";font-size:", ";box-sizing:border-box;margin-bottom:", ";"], function (_ref4) { var theme = _ref4.theme, icon = _ref4.icon, closable = _ref4.closable; return (0, _rtl.rtlSpacing)(closable ? icon && "".concat(theme.orbit.paddingAlert, " ").concat(theme.orbit.spaceXXLarge, " ").concat(theme.orbit.paddingAlert, " ").concat(theme.orbit.paddingAlert) || "".concat(theme.orbit.paddingAlert, " ").concat(theme.orbit.spaceXXLarge, " ").concat(theme.orbit.paddingAlert, " ").concat(theme.orbit.paddingAlert) : icon && "".concat(theme.orbit.paddingAlert, " ").concat(theme.orbit.paddingAlert, " ").concat(theme.orbit.paddingAlert, " ").concat(theme.orbit.paddingAlert) || "".concat(theme.orbit.paddingAlert)); }, function (_ref5) { var theme = _ref5.theme; return theme.orbit.borderRadiusNormal; }, getTypeToken(_consts.TOKENS.backgroundAlert), getTypeToken(_consts.TOKENS.colorTextAlert), function (_ref6) { var theme = _ref6.theme; return theme.orbit.fontFamily; }, function (_ref7) { var theme = _ref7.theme; return theme.orbit.fontSizeTextNormal; }, _getSpacingToken.default); StyledAlert.defaultProps = { theme: _defaultTheme.default }; var IconContainer = (0, _styledComponents.default)(StyledDiv).withConfig({ displayName: "Alert__IconContainer", componentId: "svgppc-1" })(["flex-shrink:0;margin:", ";color:", ";display:", ";align-items:", ";"], function (_ref8) { var theme = _ref8.theme; return (0, _rtl.rtlSpacing)("0 ".concat(theme.orbit.spaceSmall, " 0 0")); }, getTypeToken(_consts.TOKENS.colorIconAlert), function (_ref9) { var inlineActions = _ref9.inlineActions; return inlineActions && "flex"; }, function (_ref10) { var inlineActions = _ref10.inlineActions; return inlineActions && "center"; }); IconContainer.defaultProps = { theme: _defaultTheme.default }; var ContentWrapper = (0, _styledComponents.default)(StyledDiv).withConfig({ displayName: "Alert__ContentWrapper", componentId: "svgppc-2" })(["flex:1;display:flex;flex-direction:", ";align-items:", ";justify-content:", ";"], function (_ref11) { var title = _ref11.title, inlineActions = _ref11.inlineActions; return title && (inlineActions ? "row" : "column"); }, function (_ref12) { var title = _ref12.title; return !title && "center"; }, function (_ref13) { var inlineActions = _ref13.inlineActions; return inlineActions && "space-between"; }); var Title = (0, _styledComponents.default)(StyledDiv).withConfig({ displayName: "Alert__Title", componentId: "svgppc-3" })(["display:flex;align-items:center;margin-bottom:", ";font-weight:", ";line-height:", ";min-height:", ";"], function (_ref14) { var theme = _ref14.theme, hasChildren = _ref14.hasChildren, inlineActions = _ref14.inlineActions; return hasChildren && (inlineActions ? "0" : theme.orbit.spaceXSmall); }, function (_ref15) { var theme = _ref15.theme; return theme.orbit.fontWeightBold; }, function (_ref16) { var theme = _ref16.theme; return theme.orbit.lineHeightHeading; }, function (_ref17) { var theme = _ref17.theme; return theme.orbit.heightIconMedium; }); Title.defaultProps = { theme: _defaultTheme.default }; var Content = (0, _styledComponents.default)(StyledDiv).withConfig({ displayName: "Alert__Content", componentId: "svgppc-4" })(["display:block;margin-bottom:", ";line-height:", ";& a,& ", "{color:", ";font-weight:", ";transition:color ", " ease-in-out;&:hover,&:focus,&:active{color:", ";}}& ", ",", "{color:", ";}"], function (_ref18) { var theme = _ref18.theme, title = _ref18.title, inlineActions = _ref18.inlineActions; return title && (inlineActions ? "0" : theme.orbit.spaceXXSmall); }, function (_ref19) { var theme = _ref19.theme; return theme.orbit.lineHeightText; }, _TextLink.StyledTextLink, getTypeToken(_consts.TOKENS.colorTextAlert), function (_ref20) { var theme = _ref20.theme; return theme.orbit.fontWeightMedium; }, function (_ref21) { var theme = _ref21.theme; return theme.orbit.durationFast; }, getTypeToken(_consts.TOKENS.colorTextLinkAlertHover), _ListItem.Item, _Text.StyledText, getTypeToken(_consts.TOKENS.colorTextAlert)); Content.defaultProps = { theme: _defaultTheme.default }; var CloseContainer = (0, _styledComponents.default)(StyledDiv).withConfig({ displayName: "Alert__CloseContainer", componentId: "svgppc-5" })(["position:absolute;top:", ";margin-top:", ";", ":0;margin-", ":", ";"], function (_ref22) { var hasChildren = _ref22.hasChildren; return hasChildren ? 0 : "50%"; }, function (_ref23) { var hasChildren = _ref23.hasChildren, theme = _ref23.theme; return !hasChildren && "-".concat(theme.orbit.widthIconSmall); }, _rtl.right, _rtl.right, function (_ref24) { var hasChildren = _ref24.hasChildren, theme = _ref24.theme; return !hasChildren && theme.orbit.spaceXSmall; }); CloseContainer.defaultProps = { theme: _defaultTheme.default }; var AlertCloseButton = function AlertCloseButton(_ref25) { var hasChildren = _ref25.hasChildren, dataTest = _ref25.dataTest, onClick = _ref25.onClick, icon = _ref25.icon; var dictionary = React.useContext(_Dictionary.DictionaryContext); return React.createElement(CloseContainer, { hasChildren: hasChildren }, React.createElement(_ButtonLink.default, { dataTest: dataTest, onClick: onClick, size: "small", icon: icon, transparent: true, title: (0, _Translate.pureTranslate)(dictionary, "button_close") })); }; var Alert = function Alert(props) { var _props$type = props.type, type = _props$type === void 0 ? _consts.TYPE_OPTIONS.INFO : _props$type, title = props.title, closable = props.closable, icon = props.icon, _props$onClose = props.onClose, onClose = _props$onClose === void 0 ? function () {} : _props$onClose, children = props.children, dataTest = props.dataTest, spaceAfter = props.spaceAfter, _props$inlineActions = props.inlineActions, inlineActions = _props$inlineActions === void 0 ? false : _props$inlineActions; return React.createElement(StyledAlert, { type: type, icon: icon, closable: closable, dataTest: dataTest, spaceAfter: spaceAfter }, icon && React.createElement(IconContainer, { type: type, inlineActions: inlineActions }, React.createElement(Icon, { type: type, icon: icon })), React.createElement(ContentWrapper, { title: title, inlineActions: inlineActions }, title && React.createElement(Title, { hasChildren: children, inlineActions: inlineActions }, title), children && !inlineActions && React.createElement(Content, { title: title, type: type }, children), inlineActions && React.createElement(Content, { title: title, type: type, inlineActions: inlineActions }, inlineActions)), closable && React.createElement(AlertCloseButton, { hasChildren: children, dataTest: _consts.CLOSE_BUTTON_DATA_TEST, onClick: onClose, icon: React.createElement(_Close.default, { size: "small", color: type }) })); }; var _default = Alert; exports.default = _default;