@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.
160 lines (152 loc) • 5.99 kB
JavaScript
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";
const getFontSize = ({
theme,
size
}) => {
const tokens = {
[SIZES.SMALL]: theme.orbit.fontSizeTextSmall,
[SIZES.NORMAL]: theme.orbit.fontSizeTextNormal
};
return tokens[size];
};
const getBackgroundColor = state => ({
type,
dateTag
}) => {
const states = {
[TYPES.COLORED]: {
[STATES.DEFAULT]: resolveColor({
selected: dateTag ? "paletteInkLighterHover" : "paletteBlueNormal",
removable: "paletteBlueLight",
normal: "paletteBlueLight"
}),
[STATES.HOVER]: resolveColor({
selected: dateTag ? "paletteInkLighterActive" : "paletteBlueNormalHover",
removable: "paletteBlueLightHover",
normal: "paletteBlueLightHover"
}),
[STATES.ACTIVE]: resolveColor({
selected: dateTag ? "paletteInkLightHover" : "paletteBlueNormalActive",
removable: "paletteBlueLightActive",
normal: "paletteBlueLightActive"
})
},
[TYPES.NEUTRAL]: {
[STATES.DEFAULT]: resolveColor({
selected: dateTag ? "paletteInkLighterHover" : "paletteBlueNormal",
removable: "paletteCloudDark",
normal: "paletteCloudDark"
}),
[STATES.HOVER]: resolveColor({
selected: dateTag ? "paletteInkLighterActive" : "paletteBlueNormalHover",
removable: "paletteCloudNormalHover",
normal: "paletteCloudNormalHover"
}),
[STATES.ACTIVE]: resolveColor({
selected: dateTag ? "paletteInkLightHover" : "paletteBlueNormalActive",
removable: "paletteCloudNormalActive",
normal: "paletteCloudNormalActive"
})
}
};
return states[type][state];
};
const CloseContainer = styled.div.withConfig({
displayName: "Tag__CloseContainer",
componentId: "sc-ohf26k-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)); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198
CloseContainer.defaultProps = {
theme: defaultTheme
};
export const StyledTag = styled.div.withConfig({
displayName: "Tag__StyledTag",
componentId: "sc-ohf26k-1"
})(["", ""], ({
theme,
actionable,
type
}) => 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, resolveColor({
selected: "paletteWhite",
removable: type === TYPES.NEUTRAL ? "paletteInkNormal" : "paletteBlueDarker",
normal: type === TYPES.NEUTRAL ? "paletteInkNormal" : "paletteBlueDarker"
}), getBackgroundColor(STATES.DEFAULT), getFontSize, theme.orbit.fontWeightMedium, theme.orbit.borderRadiusNormal, theme.orbit.spaceXSmall, theme.orbit.durationFast, theme.orbit.durationFast, theme.orbit.durationFast, actionable && css(["cursor:pointer;&:hover{background:", ";box-shadow:none;}&:focus{background:", ";}&:focus:not(:focus-visible):not(:active){background:", ";}&:active,&:focus-visible{", "{opacity:1;}background:", ";}"], getBackgroundColor(STATES.HOVER), getBackgroundColor(STATES.DEFAULT), getBackgroundColor(STATES.DEFAULT), CloseContainer, getBackgroundColor(STATES.ACTIVE)))); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198
StyledTag.defaultProps = {
theme: defaultTheme
};
const StyledClose = styled.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
};
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,
size = SIZES.NORMAL,
onClick,
onRemove,
dataTest,
type = 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, {
size: "small"
}))));
});
export default Tag;