@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.
186 lines (185 loc) • 7.7 kB
JavaScript
;
"use client";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
exports.__esModule = true;
exports.default = exports.StyledTag = void 0;
var React = _interopRequireWildcard(require("react"));
var _styledComponents = _interopRequireWildcard(require("styled-components"));
var _defaultTheme = _interopRequireDefault(require("../defaultTheme"));
var _rtl = require("../utils/rtl");
var _CloseCircle = _interopRequireDefault(require("../icons/CloseCircle"));
var _consts = require("./consts");
var _keyMaps = _interopRequireDefault(require("../common/keyMaps"));
var _resolveColor = _interopRequireDefault(require("./helpers/resolveColor"));
var _resolveCircleColor = _interopRequireDefault(require("./helpers/resolveCircleColor"));
var _mediaQuery = _interopRequireDefault(require("../utils/mediaQuery"));
const getFontSize = ({
theme,
size
}) => {
const tokens = {
[_consts.SIZES.SMALL]: theme.orbit.fontSizeTextSmall,
[_consts.SIZES.NORMAL]: theme.orbit.fontSizeTextNormal
};
if (!size) return null;
return tokens[size];
};
const getBackgroundColor = state => ({
type,
dateTag
}) => {
const states = {
[_consts.TYPES.COLORED]: {
[_consts.STATES.DEFAULT]: (0, _resolveColor.default)({
selected: dateTag ? "paletteInkLightHover" : "paletteBlueNormal",
removable: "paletteBlueLight",
normal: "paletteBlueLight"
}),
[_consts.STATES.HOVER]: (0, _resolveColor.default)({
selected: dateTag ? "paletteInkLightActive" : "paletteBlueNormalHover",
removable: "paletteBlueLightHover",
normal: "paletteBlueLightHover"
}),
[_consts.STATES.ACTIVE]: (0, _resolveColor.default)({
selected: dateTag ? "paletteInkLightHover" : "paletteBlueNormalActive",
removable: "paletteBlueLightActive",
normal: "paletteBlueLightActive"
})
},
[_consts.TYPES.NEUTRAL]: {
[_consts.STATES.DEFAULT]: (0, _resolveColor.default)({
selected: dateTag ? "paletteInkLightHover" : "paletteBlueNormal",
removable: "paletteCloudNormal",
normal: "paletteCloudNormal"
}),
[_consts.STATES.HOVER]: (0, _resolveColor.default)({
selected: dateTag ? "paletteInkLightActive" : "paletteBlueNormalHover",
removable: "paletteCloudNormalHover",
normal: "paletteCloudNormalHover"
}),
[_consts.STATES.ACTIVE]: (0, _resolveColor.default)({
selected: dateTag ? "paletteInkLightHover" : "paletteBlueNormalActive",
removable: "paletteCloudNormalActive",
normal: "paletteCloudNormalActive"
})
}
};
return states[type][state];
};
const getLineHeight = ({
theme,
size
}) => {
const tokens = {
[_consts.SIZES.SMALL]: theme.orbit.lineHeightTextSmall,
[_consts.SIZES.NORMAL]: theme.orbit.lineHeightTextNormal
};
if (!size) return null;
return tokens[size];
};
const CloseContainer = _styledComponents.default.div.withConfig({
displayName: "Tag__CloseContainer",
componentId: "sc-kfwiit-0"
})(["", ";"], ({
theme,
actionable,
type,
selected
}) => (0, _styledComponents.css)(["display:flex;margin-", ":", ";opacity:", ";color:", ";cursor:", ";transition:all ", " ease-in-out;&:active{color:", ";}"], _rtl.left, theme.orbit.spaceXSmall, selected ? "1" : "0.5", (0, _resolveColor.default)({
selected: "paletteWhite",
removable: type === _consts.TYPES.NEUTRAL ? "paletteInkNormal" : "paletteBlueDarker",
normal: "paletteInkLink"
}), actionable && `pointer`, theme.orbit.durationFast, _resolveCircleColor.default));
CloseContainer.defaultProps = {
theme: _defaultTheme.default
};
const StyledTag = _styledComponents.default.div.withConfig({
displayName: "Tag__StyledTag",
componentId: "sc-kfwiit-1"
})(["", ""], ({
theme,
actionable,
type
}) => (0, _styledComponents.css)(["font-family:", ";color:", ";background:", ";display:inline-flex;box-sizing:border-box;justify-content:center;align-items:center;line-height:", ";font-size:", ";font-weight:", ";border-radius:", ";padding:", ";transition:color ", " ease-in-out,box-shadow ", " ease-in-out,background ", " ease-in-out;", " ", ";"], theme.orbit.fontFamily, (0, _resolveColor.default)({
selected: "paletteWhite",
removable: type === _consts.TYPES.NEUTRAL ? "paletteInkDark" : "paletteBlueDarker",
normal: type === _consts.TYPES.NEUTRAL ? "paletteInkDark" : "paletteBlueDarker"
}), getBackgroundColor(_consts.STATES.DEFAULT), getLineHeight, getFontSize, theme.orbit.fontWeightMedium, theme.orbit.borderRadiusLarge, theme.orbit.spaceXSmall, theme.orbit.durationFast, theme.orbit.durationFast, theme.orbit.durationFast, _mediaQuery.default.tablet((0, _styledComponents.css)(["border-radius:", ";"], theme.orbit.borderRadiusNormal)), actionable && (0, _styledComponents.css)(["cursor:pointer;&:hover,&:focus-visible{background:", ";box-shadow:none;}&:focus{background:", ";}&:active{", "{opacity:1;}background:", ";}&:focus:not(:focus-visible):not(:active){background:", ";}"], getBackgroundColor(_consts.STATES.HOVER), getBackgroundColor(_consts.STATES.HOVER), CloseContainer, getBackgroundColor(_consts.STATES.ACTIVE), getBackgroundColor(_consts.STATES.HOVER))));
exports.StyledTag = StyledTag;
StyledTag.defaultProps = {
theme: _defaultTheme.default
};
const StyledClose = _styledComponents.default.div.withConfig({
displayName: "Tag__StyledClose",
componentId: "sc-kfwiit-2"
})(["display:flex;border-radius:100%;&:focus{", "{opacity:1;}}"], CloseContainer);
StyledClose.defaultProps = {
theme: _defaultTheme.default
};
const StyledIconContainer = _styledComponents.default.div.withConfig({
displayName: "Tag__StyledIconContainer",
componentId: "sc-kfwiit-3"
})(["", ""], ({
theme
}) => (0, _styledComponents.css)(["display:flex;flex-direction:row;align-items:center;justify-content:center;padding-right:", ";svg{width:", ";height:", ";}"], theme.orbit.spaceXSmall, theme.orbit.widthIconSmall, theme.orbit.heightIconSmall));
StyledIconContainer.defaultProps = {
theme: _defaultTheme.default
};
const buttonClickEmulation = callback => ev => {
if (ev && ev.keyCode === _keyMaps.default.SPACE) {
ev.preventDefault();
if (callback) callback();
} else if (ev && ev.keyCode === _keyMaps.default.ENTER) {
if (callback) callback();
}
};
const Tag = /*#__PURE__*/React.forwardRef(({
selected,
children,
iconLeft,
size = _consts.SIZES.NORMAL,
onClick,
onRemove,
id,
dataTest,
type = _consts.TYPES.NEUTRAL,
dateTag
}, ref) => {
return /*#__PURE__*/React.createElement(StyledTag, {
className: "orbit-tag",
actionable: !!(onClick || onRemove),
"data-test": dataTest,
id: id,
dateTag: dateTag,
size: size,
ref: ref,
type: type,
onClick: onClick,
removable: !!onRemove,
selected: selected,
tabIndex: (onClick || onRemove) && 0,
role: "button",
onKeyDown: buttonClickEmulation(onClick)
}, iconLeft && /*#__PURE__*/React.createElement(StyledIconContainer, null, iconLeft), children, onRemove && /*#__PURE__*/React.createElement(CloseContainer, {
selected: selected,
removable: !!onRemove,
type: type,
onClick: ev => {
ev.stopPropagation();
if (onRemove) onRemove();
}
}, /*#__PURE__*/React.createElement(StyledClose, {
tabIndex: 0,
"aria-label": "close",
role: "button",
onKeyDown: ev => {
ev.stopPropagation();
buttonClickEmulation(onRemove);
}
}, /*#__PURE__*/React.createElement(_CloseCircle.default, {
size: "small"
}))));
});
var _default = Tag;
exports.default = _default;