@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.
179 lines (178 loc) • 6.6 kB
JavaScript
"use client";
import * as React from "react";
import styled, { css } from "styled-components";
import defaultTheme from "../defaultTheme";
import { left } from "../utils/rtl";
import CloseCircle from "../icons/CloseCircle";
import { SIZES, STATES, TYPES } from "./consts";
import KEY_CODE_MAP from "../common/keyMaps";
import resolveColor from "./helpers/resolveColor";
import resolveCircleColor from "./helpers/resolveCircleColor";
import mq from "../utils/mediaQuery";
const getFontSize = ({
theme,
size
}) => {
const tokens = {
[SIZES.SMALL]: theme.orbit.fontSizeTextSmall,
[SIZES.NORMAL]: theme.orbit.fontSizeTextNormal
};
if (!size) return null;
return tokens[size];
};
const getBackgroundColor = state => ({
type,
dateTag
}) => {
const states = {
[TYPES.COLORED]: {
[STATES.DEFAULT]: resolveColor({
selected: dateTag ? "paletteInkLightHover" : "paletteBlueNormal",
removable: "paletteBlueLight",
normal: "paletteBlueLight"
}),
[STATES.HOVER]: resolveColor({
selected: dateTag ? "paletteInkLightActive" : "paletteBlueNormalHover",
removable: "paletteBlueLightHover",
normal: "paletteBlueLightHover"
}),
[STATES.ACTIVE]: resolveColor({
selected: dateTag ? "paletteInkLightHover" : "paletteBlueNormalActive",
removable: "paletteBlueLightActive",
normal: "paletteBlueLightActive"
})
},
[TYPES.NEUTRAL]: {
[STATES.DEFAULT]: resolveColor({
selected: dateTag ? "paletteInkLightHover" : "paletteBlueNormal",
removable: "paletteCloudNormal",
normal: "paletteCloudNormal"
}),
[STATES.HOVER]: resolveColor({
selected: dateTag ? "paletteInkLightActive" : "paletteBlueNormalHover",
removable: "paletteCloudNormalHover",
normal: "paletteCloudNormalHover"
}),
[STATES.ACTIVE]: resolveColor({
selected: dateTag ? "paletteInkLightHover" : "paletteBlueNormalActive",
removable: "paletteCloudNormalActive",
normal: "paletteCloudNormalActive"
})
}
};
return states[type][state];
};
const getLineHeight = ({
theme,
size
}) => {
const tokens = {
[SIZES.SMALL]: theme.orbit.lineHeightTextSmall,
[SIZES.NORMAL]: theme.orbit.lineHeightTextNormal
};
if (!size) return null;
return tokens[size];
};
const CloseContainer = styled.div.withConfig({
displayName: "Tag__CloseContainer",
componentId: "sc-kfwiit-0"
})(["", ";"], ({
theme,
actionable,
type,
selected
}) => css(["display:flex;margin-", ":", ";opacity:", ";color:", ";cursor:", ";transition:all ", " ease-in-out;&:active{color:", ";}"], left, theme.orbit.spaceXSmall, selected ? "1" : "0.5", resolveColor({
selected: "paletteWhite",
removable: type === TYPES.NEUTRAL ? "paletteInkNormal" : "paletteBlueDarker",
normal: "paletteInkLink"
}), actionable && `pointer`, theme.orbit.durationFast, resolveCircleColor));
CloseContainer.defaultProps = {
theme: defaultTheme
};
export const StyledTag = styled.div.withConfig({
displayName: "Tag__StyledTag",
componentId: "sc-kfwiit-1"
})(["", ""], ({
theme,
actionable,
type
}) => 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, resolveColor({
selected: "paletteWhite",
removable: type === TYPES.NEUTRAL ? "paletteInkDark" : "paletteBlueDarker",
normal: type === TYPES.NEUTRAL ? "paletteInkDark" : "paletteBlueDarker"
}), getBackgroundColor(STATES.DEFAULT), getLineHeight, getFontSize, theme.orbit.fontWeightMedium, theme.orbit.borderRadiusLarge, theme.orbit.spaceXSmall, theme.orbit.durationFast, theme.orbit.durationFast, theme.orbit.durationFast, mq.tablet(css(["border-radius:", ";"], theme.orbit.borderRadiusNormal)), actionable && css(["cursor:pointer;&:hover,&:focus-visible{background:", ";box-shadow:none;}&:focus{background:", ";}&:active{", "{opacity:1;}background:", ";}&:focus:not(:focus-visible):not(:active){background:", ";}"], getBackgroundColor(STATES.HOVER), getBackgroundColor(STATES.HOVER), CloseContainer, getBackgroundColor(STATES.ACTIVE), getBackgroundColor(STATES.HOVER))));
StyledTag.defaultProps = {
theme: defaultTheme
};
const StyledClose = styled.div.withConfig({
displayName: "Tag__StyledClose",
componentId: "sc-kfwiit-2"
})(["display:flex;border-radius:100%;&:focus{", "{opacity:1;}}"], CloseContainer);
StyledClose.defaultProps = {
theme: defaultTheme
};
const StyledIconContainer = styled.div.withConfig({
displayName: "Tag__StyledIconContainer",
componentId: "sc-kfwiit-3"
})(["", ""], ({
theme
}) => 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
};
const buttonClickEmulation = callback => 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();
}
};
const Tag = /*#__PURE__*/React.forwardRef(({
selected,
children,
iconLeft,
size = SIZES.NORMAL,
onClick,
onRemove,
id,
dataTest,
type = 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, {
size: "small"
}))));
});
export default Tag;