@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.
241 lines (240 loc) • 11.2 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
exports.__esModule = true;
exports.default = exports.AlertButton = void 0;
var React = _interopRequireWildcard(require("react"));
var _styledComponents = _interopRequireWildcard(require("styled-components"));
var _orbitDesignTokens = require("@kiwicom/orbit-design-tokens");
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 _Heading = require("../Heading");
var _mediaQuery = _interopRequireDefault(require("../utils/mediaQuery"));
var _AlertButton = _interopRequireDefault(require("./AlertButton"));
exports.AlertButton = _AlertButton.default;
const getTypeToken = name => ({
theme,
type,
suppressed
}) => {
const tokens = {
[_consts.TOKENS.colorIconAlert]: {
[_consts.TYPE_OPTIONS.INFO]: theme.orbit.paletteBlueNormal,
[_consts.TYPE_OPTIONS.SUCCESS]: theme.orbit.paletteGreenNormal,
[_consts.TYPE_OPTIONS.WARNING]: theme.orbit.paletteOrangeNormal,
[_consts.TYPE_OPTIONS.CRITICAL]: theme.orbit.paletteRedNormal
},
[_consts.TOKENS.backgroundAlert]: {
[_consts.TYPE_OPTIONS.INFO]: suppressed ? theme.orbit.paletteCloudLight : theme.orbit.backgroundAlertInfo,
[_consts.TYPE_OPTIONS.SUCCESS]: suppressed ? theme.orbit.paletteCloudLight : theme.orbit.backgroundAlertSuccess,
[_consts.TYPE_OPTIONS.WARNING]: suppressed ? theme.orbit.paletteCloudLight : theme.orbit.backgroundAlertWarning,
[_consts.TYPE_OPTIONS.CRITICAL]: suppressed ? theme.orbit.paletteCloudLight : theme.orbit.backgroundAlertCritical
},
// TODO: create token
[_consts.TOKENS.colorTextLinkAlertHover]: {
[_consts.TYPE_OPTIONS.INFO]: theme.orbit.paletteBlueDarkHover,
[_consts.TYPE_OPTIONS.SUCCESS]: theme.orbit.paletteGreenDarkHover,
[_consts.TYPE_OPTIONS.WARNING]: theme.orbit.paletteOrangeDarkHover,
[_consts.TYPE_OPTIONS.CRITICAL]: theme.orbit.paletteRedDarkActive
},
// TODO: create token
[_consts.TOKENS.colorTextLinkAlertFocus]: {
[_consts.TYPE_OPTIONS.INFO]: (0, _orbitDesignTokens.convertHexToRgba)(theme.orbit.paletteBlueDarkHover, 10),
[_consts.TYPE_OPTIONS.SUCCESS]: (0, _orbitDesignTokens.convertHexToRgba)(theme.orbit.paletteGreenDarkHover, 10),
[_consts.TYPE_OPTIONS.WARNING]: (0, _orbitDesignTokens.convertHexToRgba)(theme.orbit.paletteOrangeDarkHover, 10),
[_consts.TYPE_OPTIONS.CRITICAL]: (0, _orbitDesignTokens.convertHexToRgba)(theme.orbit.paletteRedDarkActive, 10)
},
[_consts.TOKENS.colorBorderAlert]: {
[_consts.TYPE_OPTIONS.INFO]: suppressed ? theme.orbit.paletteCloudNormal : theme.orbit.paletteBlueLightHover,
[_consts.TYPE_OPTIONS.SUCCESS]: suppressed ? theme.orbit.paletteCloudNormal : theme.orbit.paletteGreenLightHover,
[_consts.TYPE_OPTIONS.WARNING]: suppressed ? theme.orbit.paletteCloudNormal : theme.orbit.paletteOrangeLightHover,
[_consts.TYPE_OPTIONS.CRITICAL]: suppressed ? theme.orbit.paletteCloudNormal : theme.orbit.paletteRedLightHover
},
[_consts.TOKENS.colorAccentBorder]: {
[_consts.TYPE_OPTIONS.INFO]: theme.orbit.paletteBlueNormal,
[_consts.TYPE_OPTIONS.SUCCESS]: theme.orbit.paletteGreenNormal,
[_consts.TYPE_OPTIONS.WARNING]: theme.orbit.paletteOrangeNormal,
[_consts.TYPE_OPTIONS.CRITICAL]: theme.orbit.paletteRedNormal
}
};
return tokens[name][type];
};
const StyledIcon = ({
icon,
type
}) => {
// Icon should be boolean and TRUE
if (typeof icon === "boolean" && icon) {
if (type === _consts.TYPE_OPTIONS.INFO) {
return /*#__PURE__*/React.createElement(_InformationCircle.default, {
size: "small"
});
}
if (type === _consts.TYPE_OPTIONS.SUCCESS) {
return /*#__PURE__*/React.createElement(_Check.default, {
size: "small"
});
}
if (type === _consts.TYPE_OPTIONS.WARNING) {
return /*#__PURE__*/React.createElement(_Alert.default, {
size: "small"
});
}
if (type === _consts.TYPE_OPTIONS.CRITICAL) {
return /*#__PURE__*/React.createElement(_AlertCircle.default, {
size: "small"
});
}
}
if ( /*#__PURE__*/React.isValidElement(icon) && typeof icon !== "boolean") {
// @ts-expect-error TODO
return /*#__PURE__*/React.cloneElement(icon, {
size: "small"
});
}
return /*#__PURE__*/React.createElement(React.Fragment, null, icon);
};
const StyledAlert = _styledComponents.default.div.withConfig({
displayName: "Alert__StyledAlert",
componentId: "sc-vguv1m-0"
})(["", ""], ({
theme,
closable
}) => (0, _styledComponents.css)(["position:relative;display:flex;width:100%;border-radius:", ";border:1px solid ", ";background:", ";color:", ";font-family:", ";font-size:", ";line-height:", ";box-sizing:border-box;margin-bottom:", ";border-top:3px solid ", ";padding:", ";", " ", ""], theme.orbit.borderRadiusLarge, getTypeToken(_consts.TOKENS.colorBorderAlert), getTypeToken(_consts.TOKENS.backgroundAlert), theme.orbit.paletteInkDark, theme.orbit.fontFamily, theme.orbit.fontSizeTextNormal, theme.orbit.lineHeightTextNormal, _getSpacingToken.default, getTypeToken(_consts.TOKENS.colorAccentBorder), closable ? (0, _rtl.rtlSpacing)(`${theme.orbit.spaceSmall} ${theme.orbit.spaceLarge} ${theme.orbit.spaceSmall} ${theme.orbit.spaceSmall}`) : theme.orbit.spaceSmall, _mediaQuery.default.largeMobile((0, _styledComponents.css)(["border-top:1px solid ", ";border-", ":3px solid ", ";"], getTypeToken(_consts.TOKENS.colorBorderAlert), _rtl.left, getTypeToken(_consts.TOKENS.colorAccentBorder))), _mediaQuery.default.tablet((0, _styledComponents.css)(["border-radius:", ";"], theme.orbit.borderRadiusNormal))));
StyledAlert.defaultProps = {
theme: _defaultTheme.default
};
const StyledIconContainer = _styledComponents.default.div.withConfig({
displayName: "Alert__StyledIconContainer",
componentId: "sc-vguv1m-1"
})(["", ""], ({
theme,
inlineActions
}) => (0, _styledComponents.css)(["flex-shrink:0;margin:", ";color:", ";display:", ";align-items:", ";line-height:1;", ""], (0, _rtl.rtlSpacing)(`0 ${theme.orbit.spaceXSmall} 0 0`), getTypeToken(_consts.TOKENS.colorIconAlert), inlineActions && "flex", inlineActions && "center", _mediaQuery.default.tablet((0, _styledComponents.css)(["margin:", ";svg{width:20px;height:20px;}"], (0, _rtl.rtlSpacing)(`0 ${theme.orbit.spaceXSmall} 0 0`)({
theme
})))));
StyledIconContainer.defaultProps = {
theme: _defaultTheme.default
};
const StyledContentWrapper = _styledComponents.default.div.withConfig({
displayName: "Alert__StyledContentWrapper",
componentId: "sc-vguv1m-2"
})(["", ""], ({
hasTitle,
inlineActions
}) => (0, _styledComponents.css)(["flex:1;display:flex;flex-direction:", ";align-items:", ";justify-content:", ";"], hasTitle && inlineActions ? "row" : "column", !hasTitle && "center", inlineActions && "space-between"));
const StyledTitle = _styledComponents.default.div.withConfig({
displayName: "Alert__StyledTitle",
componentId: "sc-vguv1m-3"
})(["", ""], ({
theme,
hasChildren,
inlineActions
}) => (0, _styledComponents.css)(["color:", ";display:flex;align-items:center;min-height:20px;margin-bottom:", ";font-weight:", ";"], theme.orbit.paletteInkDark, hasChildren && (inlineActions ? "0" : theme.orbit.spaceXXSmall), theme.orbit.fontWeightBold));
StyledTitle.defaultProps = {
theme: _defaultTheme.default
};
const StyledContent = _styledComponents.default.div.withConfig({
displayName: "Alert__StyledContent",
componentId: "sc-vguv1m-4"
})(["", ""], ({
inlineActions,
theme
}) => (0, _styledComponents.css)(["display:flex;align-items:center;min-height:20px;width:", ";& a:not([class]),& ", "{", ";}& ", ",", ",", "{color:", ";}"], !inlineActions && "100%", _TextLink.StyledTextLink, _TextLink.getLinkStyle, _ListItem.Item, _Text.StyledText, _Heading.StyledHeading, theme.orbit.paletteInkDark));
StyledContent.defaultProps = {
theme: _defaultTheme.default
};
const CloseContainer = _styledComponents.default.div.withConfig({
displayName: "Alert__CloseContainer",
componentId: "sc-vguv1m-5"
})(["", ""], ({
theme,
hasChildren
}) => (0, _styledComponents.css)(["position:absolute;top:", ";margin-top:", ";", ":0;margin-", ":", ";"], hasChildren ? 0 : "50%", !hasChildren && `-${theme.orbit.widthIconSmall}`, _rtl.right, _rtl.right, !hasChildren && theme.orbit.spaceXSmall));
CloseContainer.defaultProps = {
theme: _defaultTheme.default
};
const AlertCloseButton = ({
hasChildren,
dataTest,
onClick,
labelClose,
icon
}) => {
return /*#__PURE__*/React.createElement(CloseContainer, {
hasChildren: hasChildren
}, /*#__PURE__*/React.createElement(_ButtonLink.default, {
dataTest: dataTest,
onClick: onClick,
size: "small",
iconLeft: icon,
type: "secondary",
title: labelClose
}));
};
const Alert = props => {
const {
type = _consts.TYPE_OPTIONS.INFO,
title,
icon,
closable,
onClose,
children,
dataTest,
id,
spaceAfter,
suppressed,
inlineActions,
labelClose
} = props;
return /*#__PURE__*/React.createElement(StyledAlert, {
type: type,
icon: icon,
id: id,
suppressed: suppressed,
closable: closable,
"data-test": dataTest,
spaceAfter: spaceAfter
}, icon && /*#__PURE__*/React.createElement(StyledIconContainer, {
type: type,
inlineActions: !!inlineActions
}, /*#__PURE__*/React.createElement(StyledIcon, {
type: type,
icon: icon
})), /*#__PURE__*/React.createElement(StyledContentWrapper, {
hasTitle: !!title,
inlineActions: !!inlineActions
}, title && /*#__PURE__*/React.createElement(StyledTitle, {
hasChildren: !!children,
inlineActions: !!inlineActions
}, title), children && !inlineActions && /*#__PURE__*/React.createElement(StyledContent, {
$type: type,
$noUnderline: true
}, children), inlineActions && /*#__PURE__*/React.createElement(StyledContent, {
$type: type,
inlineActions: !!inlineActions,
$noUnderline: true
}, inlineActions)), closable && /*#__PURE__*/React.createElement(AlertCloseButton, {
hasChildren: !!children,
dataTest: _consts.CLOSE_BUTTON_DATA_TEST,
onClick: onClose,
labelClose: labelClose,
icon: /*#__PURE__*/React.createElement(_Close.default, {
size: "small",
color: type
})
}));
};
var _default = Alert;
exports.default = _default;