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.

115 lines (111 loc) 3.91 kB
import * as React from "react"; import styled from "styled-components"; import defaultTheme from "../defaultTheme"; import { TYPE_OPTIONS, TOKENS } from "./consts"; import { rtlSpacing } from "../utils/rtl"; const getTypeToken = name => ({ theme, type }) => { const tokens = { [TOKENS.background]: { [TYPE_OPTIONS.NEUTRAL]: theme.orbit.backgroundBadgeNeutral, [TYPE_OPTIONS.INFO]: theme.orbit.backgroundBadgeInfo, [TYPE_OPTIONS.SUCCESS]: theme.orbit.backgroundBadgeSuccess, [TYPE_OPTIONS.WARNING]: theme.orbit.backgroundBadgeWarning, [TYPE_OPTIONS.CRITICAL]: theme.orbit.backgroundBadgeCritical, [TYPE_OPTIONS.DARK]: theme.orbit.backgroundBadgeDark, [TYPE_OPTIONS.WHITE]: theme.orbit.backgroundBadgeWhite, [TYPE_OPTIONS.INFO_INVERTED]: theme.orbit.paletteBlueNormal, [TYPE_OPTIONS.CRITICAL_INVERTED]: theme.orbit.paletteRedNormal, [TYPE_OPTIONS.SUCCESS_INVERTED]: theme.orbit.paletteGreenNormal, [TYPE_OPTIONS.WARNING_INVERTED]: theme.orbit.paletteOrangeNormal }, [TOKENS.color]: { [TYPE_OPTIONS.NEUTRAL]: theme.orbit.colorTextBadgeNeutral, [TYPE_OPTIONS.INFO]: theme.orbit.colorTextBadgeInfo, [TYPE_OPTIONS.SUCCESS]: theme.orbit.colorTextBadgeSuccess, [TYPE_OPTIONS.WARNING]: theme.orbit.colorTextBadgeWarning, [TYPE_OPTIONS.CRITICAL]: theme.orbit.colorTextBadgeCritical, [TYPE_OPTIONS.DARK]: theme.orbit.colorTextBadgeDark, [TYPE_OPTIONS.WHITE]: theme.orbit.colorTextBadgeWhite, [TYPE_OPTIONS.INFO_INVERTED]: theme.orbit.paletteWhite, [TYPE_OPTIONS.CRITICAL_INVERTED]: theme.orbit.paletteWhite, [TYPE_OPTIONS.SUCCESS_INVERTED]: theme.orbit.paletteWhite, [TYPE_OPTIONS.WARNING_INVERTED]: theme.orbit.paletteWhite } }; return tokens[name][type]; }; export const StyledBadge = styled(({ className, children, dataTest, ariaLabel }) => React.createElement("div", { className: className, "data-test": dataTest, "aria-label": ariaLabel }, children)).withConfig({ displayName: "Badge__StyledBadge", componentId: "sc-1y6i8f0-0" })(["font-family:", ";display:inline-flex;flex:0 0 auto;box-sizing:border-box;justify-content:center;align-items:center;min-height:", ";line-height:14px;font-size:", ";font-weight:", ";background-color:", ";color:", ";border-radius:", ";padding:", ";"], ({ theme }) => theme.orbit.fontFamily, ({ theme }) => theme.orbit.heightBadge, ({ theme }) => theme.orbit.fontSizeTextSmall, ({ theme }) => theme.orbit.fontWeightMedium, getTypeToken(TOKENS.background), getTypeToken(TOKENS.color), ({ theme }) => theme.orbit.borderRadiusBadge, ({ theme }) => theme.orbit.paddingBadge); StyledBadge.defaultProps = { theme: defaultTheme }; const IconContainer = styled(({ className, children }) => React.createElement("div", { className: className }, children)).withConfig({ displayName: "Badge__IconContainer", componentId: "sc-1y6i8f0-1" })(["display:flex;flex-shrink:0;margin:", ";svg{height:", ";width:", ";}"], ({ theme, hasContent }) => hasContent && rtlSpacing(theme.orbit.marginBadgeIcon), ({ theme }) => theme.orbit.widthIconSmall, ({ theme }) => theme.orbit.heightIconSmall); IconContainer.defaultProps = { theme: defaultTheme }; const StyledBadgeContent = styled.div.withConfig({ displayName: "Badge__StyledBadgeContent", componentId: "sc-1y6i8f0-2" })(["padding:5px 0;line-height:1;"]); StyledBadgeContent.defaultProps = { theme: defaultTheme }; const Badge = props => { const { type = TYPE_OPTIONS.NEUTRAL, icon, children, ariaLabel, dataTest } = props; return React.createElement(StyledBadge, { type: type, dataTest: dataTest, ariaLabel: ariaLabel }, icon && React.createElement(IconContainer, { type: type, hasContent: !!children }, icon), React.createElement(StyledBadgeContent, null, children)); }; export default Badge;