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.

143 lines (133 loc) 5.84 kB
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import * as React from "react"; import styled, { css } from "styled-components"; import defaultTheme from "../defaultTheme"; import { rtlSpacing, left } from "../utils/rtl"; import CloseCircle from "../icons/CloseCircle"; import { SIZES, STATES } from "./consts"; import KEY_CODE_MAP from "../common/keyMaps"; import resolveColor from "./helpers/resolveColor"; var getFontSize = function getFontSize(_ref) { var _tokens; var theme = _ref.theme, size = _ref.size; var tokens = (_tokens = {}, _defineProperty(_tokens, SIZES.SMALL, theme.orbit.fontSizeTextSmall), _defineProperty(_tokens, SIZES.NORMAL, theme.orbit.fontSizeTextNormal), _tokens); return tokens[size]; }; var getBackgroundColor = function getBackgroundColor(state) { return function () { var _states; var states = (_states = {}, _defineProperty(_states, STATES.DEFAULT, resolveColor({ selected: "paletteBlueNormal", removable: "paletteBlueLight", normal: "paletteCloudDark" })), _defineProperty(_states, STATES.HOVER, resolveColor({ selected: "paletteBlueNormalHover", removable: "paletteBlueLightHover", normal: "paletteCloudNormalHover" })), _defineProperty(_states, STATES.ACTIVE, resolveColor({ selected: "paletteBlueNormalActive", removable: "paletteBlueLightActive", normal: "paletteCloudNormalHover" })), _states); return states[state]; }; }; var CloseContainer = styled.div.withConfig({ displayName: "Tag__CloseContainer", componentId: "e565ij-0" })(["display:flex;margin-", ":8px;opacity:0.5;color:", ";cursor:", ";transition:all ", " ease-in-out;&:active{color:", ";}"], left, resolveColor({ selected: "paletteWhite", removable: "paletteBlueDarker", normal: "paletteInkLink" }), function (_ref2) { var actionable = _ref2.actionable; return actionable && "pointer"; }, function (_ref3) { var theme = _ref3.theme; return theme.orbit.durationFast; }, function (_ref4) { var theme = _ref4.theme, selected = _ref4.selected; return selected ? theme.orbit.paletteWhite : theme.orbit.paletteBlueDarker; }); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 CloseContainer.defaultProps = { theme: defaultTheme }; export var StyledTag = styled.div.withConfig({ displayName: "Tag__StyledTag", componentId: "e565ij-1" })(["", ""], function (_ref5) { var theme = _ref5.theme, actionable = _ref5.actionable; return 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;}", ";"], theme.orbit.fontFamily, resolveColor({ selected: "paletteWhite", removable: "paletteBlueDarker", normal: "colorTextTag" }), getBackgroundColor(STATES.DEFAULT), getFontSize, theme.orbit.fontWeightMedium, theme.orbit.borderRadiusNormal, rtlSpacing(theme.orbit.paddingTag), theme.orbit.durationFast, theme.orbit.durationFast, theme.orbit.durationFast, actionable && css(["cursor:pointer;&:hover{background:", ";box-shadow:none;}&:active{", "{opacity:1;}background:", ";box-shadow:none;}&:focus{", "{opacity:1;}background:", ";box-shadow:none;outline:0;}"], getBackgroundColor(STATES.HOVER), CloseContainer, getBackgroundColor(STATES.ACTIVE), CloseContainer, getBackgroundColor(STATES.HOVER))); }); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 StyledTag.defaultProps = { theme: defaultTheme }; var StyledClose = styled.div.withConfig({ displayName: "Tag__StyledClose", componentId: "e565ij-2" })(["display:flex;border-radius:100%;&:focus{", "{opacity:1;}outline:none;box-shadow:0 0 0 2px ", ";}"], CloseContainer, function (_ref6) { var theme = _ref6.theme, selected = _ref6.selected, removable = _ref6.removable; return selected && !removable ? theme.orbit.paletteWhite : theme.orbit.paletteBlueDarker; }); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 StyledClose.defaultProps = { theme: defaultTheme }; var buttonClickEmulation = function buttonClickEmulation(callback) { return function (ev) { if (ev && ev.keyCode === KEY_CODE_MAP.SPACE) { ev.preventDefault(); if (callback) callback(); } else if (ev && ev.keyCode === KEY_CODE_MAP.ENTER) { if (callback) callback(); } }; }; var Tag = /*#__PURE__*/React.forwardRef(function (props, ref) { var selected = props.selected, children = props.children, _props$size = props.size, size = _props$size === void 0 ? SIZES.NORMAL : _props$size, onClick = props.onClick, onRemove = props.onRemove, dataTest = props.dataTest; return /*#__PURE__*/React.createElement(StyledTag, { actionable: onClick || onRemove, "data-test": dataTest, size: size, ref: ref, 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, onClick: function onClick(ev) { ev.stopPropagation(); if (onRemove) onRemove(); } }, /*#__PURE__*/React.createElement(StyledClose, { tabIndex: "0", selected: selected, "aria-label": "close", role: "button", onKeyDown: function onKeyDown(ev) { ev.stopPropagation(); buttonClickEmulation(onRemove); } }, /*#__PURE__*/React.createElement(CloseCircle, { size: "small" })))); }); export default Tag;