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.

168 lines (159 loc) 6.42 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, right } from "../utils/rtl"; import CloseCircle from "../icons/CloseCircle"; import { SIZES, STATES } from "./consts"; import KEY_CODE_MAP from "../common/keyMaps"; 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 (_ref2) { var _states; var selected = _ref2.selected, theme = _ref2.theme; var states = (_states = {}, _defineProperty(_states, STATES.DEFAULT, selected ? theme.orbit.paletteBlueLightHover : theme.orbit.paletteCloudDark), _defineProperty(_states, STATES.HOVER, selected ? theme.orbit.paletteBlueLightActive : theme.orbit.paletteCloudNormalHover), _defineProperty(_states, STATES.ACTIVE, selected ? theme.orbit.paletteBlueLightActive : theme.orbit.paletteCloudNormalHover), _states); return states[state]; }; }; export var StyledTag = styled.div.withConfig({ displayName: "Tag__StyledTag", componentId: "e565ij-0" })(["font-family:", ";color:", ";background:", ";display:inline-flex;box-sizing:border-box;justify-content:center;align-items:center;font-size:", ";font-weight:", ";border-radius:", ";box-shadow:", ";padding:", ";transition:color ", " ease-in-out,box-shadow ", " ease-in-out,background ", " ease-in-out;&:focus{outline:0;}", ";"], function (_ref3) { var theme = _ref3.theme; return theme.orbit.fontFamily; }, function (_ref4) { var theme = _ref4.theme, selected = _ref4.selected; return selected ? theme.orbit.paletteBlueDarker : theme.orbit.colorTextTag; }, getBackgroundColor(STATES.DEFAULT), getFontSize, function (_ref5) { var theme = _ref5.theme; return theme.orbit.fontWeightMedium; }, function (_ref6) { var theme = _ref6.theme; return theme.orbit.borderRadiusNormal; }, function (_ref7) { var theme = _ref7.theme, selected = _ref7.selected; return !selected && "inset 0 0 0 1px ".concat(theme.orbit.borderColorTag); }, function (_ref8) { var theme = _ref8.theme; return rtlSpacing(theme.orbit.paddingTag); }, function (_ref9) { var theme = _ref9.theme; return theme.orbit.durationFast; }, function (_ref10) { var theme = _ref10.theme; return theme.orbit.durationFast; }, function (_ref11) { var theme = _ref11.theme; return theme.orbit.durationFast; }, function (_ref12) { var actionable = _ref12.actionable; return actionable && css(["cursor:pointer;&:hover{background:", ";box-shadow:none;}&:active{background:", ";box-shadow:none;}&:focus{background:", ";box-shadow:none;outline:0;}"], getBackgroundColor(STATES.HOVER), getBackgroundColor(STATES.ACTIVE), getBackgroundColor(STATES.HOVER)); }); StyledTag.defaultProps = { theme: defaultTheme }; var IconContainer = styled.div.withConfig({ displayName: "Tag__IconContainer", componentId: "e565ij-1" })(["display:flex;margin-", ":8px;svg{height:", ";width:", ";}"], left, function (_ref13) { var theme = _ref13.theme; return theme.orbit.widthIconSmall; }, function (_ref14) { var theme = _ref14.theme; return theme.orbit.heightIconSmall; }); IconContainer.defaultProps = { theme: defaultTheme }; var CloseContainer = styled.div.withConfig({ displayName: "Tag__CloseContainer", componentId: "e565ij-2" })(["display:flex;margin-", ":8px;color:", ";cursor:pointer;transition:color ", " ease-in-out;&:hover{color:", ";}&:active{color:", ";}"], right, function (_ref15) { var theme = _ref15.theme, selected = _ref15.selected; return selected ? theme.orbit.paletteBlueDarker : theme.orbit.paletteInkLight; }, function (_ref16) { var theme = _ref16.theme; return theme.orbit.durationFast; }, function (_ref17) { var theme = _ref17.theme, selected = _ref17.selected; return selected ? theme.orbit.paletteBlueDarker : theme.orbit.paletteInkLight; }, function (_ref18) { var theme = _ref18.theme, selected = _ref18.selected; return selected ? theme.orbit.paletteBlueDarker : theme.orbit.paletteInkLight; }); CloseContainer.defaultProps = { theme: defaultTheme }; var StyledClose = styled.div.withConfig({ displayName: "Tag__StyledClose", componentId: "e565ij-3" })(["display:flex;border-radius:100%;&:focus{outline:none;box-shadow:0 0 0 2px ", ";}"], function (_ref19) { var theme = _ref19.theme, selected = _ref19.selected; return selected ? theme.orbit.paletteBlueDarker : theme.orbit.paletteBlueDarker; }); StyledClose.defaultProps = { theme: defaultTheme }; var buttonClickEmulation = function buttonClickEmulation(ev, callback) { if (ev && ev.keyCode === KEY_CODE_MAP.SPACE) { ev.preventDefault(); if (callback) callback(); } else if (ev.keyCode === KEY_CODE_MAP.ENTER) { if (callback) callback(); } }; var Tag = function Tag(props) { var icon = props.icon, 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, onClick: onClick, removable: !!onRemove, selected: selected, icon: !!icon, tabIndex: "0", role: "button", onKeyDown: function onKeyDown(ev) { return buttonClickEmulation(ev, onClick); } }, !!onRemove && /*#__PURE__*/React.createElement(CloseContainer, { selected: selected, onClick: function onClick(ev) { ev.stopPropagation(); if (onRemove) { onRemove(); } } }, /*#__PURE__*/React.createElement(StyledClose, { tabIndex: "0", role: "button", onKeyDown: function onKeyDown(ev) { ev.stopPropagation(); buttonClickEmulation(ev, onRemove); } }, /*#__PURE__*/React.createElement(CloseCircle, { size: "small" }))), children, icon && /*#__PURE__*/React.createElement(IconContainer, null, icon)); }; export default Tag;