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.

186 lines (161 loc) 7.01 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default; Object.defineProperty(exports, "__esModule", { value: 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")); const getFontSize = ({ theme, size }) => { const tokens = { [_consts.SIZES.SMALL]: theme.orbit.fontSizeTextSmall, [_consts.SIZES.NORMAL]: theme.orbit.fontSizeTextNormal }; return tokens[size]; }; const getBackgroundColor = state => ({ type, dateTag }) => { const states = { [_consts.TYPES.COLORED]: { [_consts.STATES.DEFAULT]: (0, _resolveColor.default)({ selected: dateTag ? "paletteInkLighterHover" : "paletteBlueNormal", removable: "paletteBlueLight", normal: "paletteBlueLight" }), [_consts.STATES.HOVER]: (0, _resolveColor.default)({ selected: dateTag ? "paletteInkLighterActive" : "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 ? "paletteInkLighterHover" : "paletteBlueNormal", removable: "paletteCloudDark", normal: "paletteCloudDark" }), [_consts.STATES.HOVER]: (0, _resolveColor.default)({ selected: dateTag ? "paletteInkLighterActive" : "paletteBlueNormalHover", removable: "paletteCloudNormalHover", normal: "paletteCloudNormalHover" }), [_consts.STATES.ACTIVE]: (0, _resolveColor.default)({ selected: dateTag ? "paletteInkLightHover" : "paletteBlueNormalActive", removable: "paletteCloudNormalActive", normal: "paletteCloudNormalActive" }) } }; return states[type][state]; }; const CloseContainer = _styledComponents.default.div.withConfig({ displayName: "Tag__CloseContainer", componentId: "sc-ohf26k-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)); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 CloseContainer.defaultProps = { theme: _defaultTheme.default }; const StyledTag = _styledComponents.default.div.withConfig({ displayName: "Tag__StyledTag", componentId: "sc-ohf26k-1" })(["", ""], ({ theme, actionable, type }) => (0, _styledComponents.css)(["font-family:", ";color:", ";background:", ";display:inline-flex;box-sizing:border-box;justify-content:center;align-items:center;font-size:", ";font-weight:", ";border-radius:", ";padding:", ";transition:color ", " ease-in-out,box-shadow ", " ease-in-out,background ", " ease-in-out;&:focus{outline:0;box-shadow:none;}", ";"], theme.orbit.fontFamily, (0, _resolveColor.default)({ selected: "paletteWhite", removable: type === _consts.TYPES.NEUTRAL ? "paletteInkNormal" : "paletteBlueDarker", normal: type === _consts.TYPES.NEUTRAL ? "paletteInkNormal" : "paletteBlueDarker" }), getBackgroundColor(_consts.STATES.DEFAULT), getFontSize, theme.orbit.fontWeightMedium, theme.orbit.borderRadiusNormal, theme.orbit.spaceXSmall, theme.orbit.durationFast, theme.orbit.durationFast, theme.orbit.durationFast, actionable && (0, _styledComponents.css)(["cursor:pointer;&:hover{background:", ";box-shadow:none;}&:focus{background:", ";}&:focus:not(:focus-visible):not(:active){background:", ";}&:active,&:focus-visible{", "{opacity:1;}background:", ";}"], getBackgroundColor(_consts.STATES.HOVER), getBackgroundColor(_consts.STATES.DEFAULT), getBackgroundColor(_consts.STATES.DEFAULT), CloseContainer, getBackgroundColor(_consts.STATES.ACTIVE)))); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 exports.StyledTag = StyledTag; StyledTag.defaultProps = { theme: _defaultTheme.default }; const StyledClose = _styledComponents.default.div.withConfig({ displayName: "Tag__StyledClose", componentId: "sc-ohf26k-2" })(["display:flex;border-radius:100%;&:focus{", "{opacity:1;}outline:none;box-shadow:0 0 0 2px;}"], CloseContainer); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 StyledClose.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, size = _consts.SIZES.NORMAL, onClick, onRemove, dataTest, type = _consts.TYPES.NEUTRAL, dateTag }, ref) => { return /*#__PURE__*/React.createElement(StyledTag, { actionable: onClick || onRemove, "data-test": dataTest, dateTag: dateTag, size: size, ref: ref, type: type, onClick: onClick, removable: !!onRemove, selected: selected, tabIndex: (onClick || onRemove) && "0", role: "button", onKeyDown: buttonClickEmulation(onClick) }, 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", selected: selected, "aria-label": "close", role: "button", onKeyDown: ev => { ev.stopPropagation(); buttonClickEmulation(onRemove); } }, /*#__PURE__*/React.createElement(_CloseCircle.default, { size: "small" })))); }); var _default = Tag; exports.default = _default;