import { ReactNode } from 'react'; import styled from '@emotion/styled'; import { css } from '@emotion/react'; import { mineShaft, mountainMeadow, nobel, pictonBlue, primary, secondary, treePoppy, valencia, white, } from 'styles/colors'; import { fontSize } from 'styles/fonts'; const COLORS_MAP = { default: mineShaft['800'], mineShaft: mineShaft.main, mountainMeadow: mountainMeadow.main, nobel: nobel.main, pictonBlue: pictonBlue.main, pictonBlueDark: pictonBlue['800'], primary: primary.main, secondary: secondary.main, treePoppy: treePoppy.main, valencia: valencia.main, }; const StyledBadge = styled.span>` display: inline-block; direction: ${({ theme: { rtl } }) => rtl && 'rtl'}; font-size: ${fontSize.small}; font-weight: bold; line-height: 1; text-align: center; border: 1px solid; border-color: ${({ variant = 'default' }) => COLORS_MAP[variant]}; ${({ size }) => size === 'small' ? css` padding: 0.2em 0.8em; ` : css` padding: 0.4em 0.8em; `}; ${({ solid, variant = 'default' }) => solid ? css` background-color: ${COLORS_MAP[variant]}; color: ${white}; ` : css` color: ${COLORS_MAP[variant]}; `}; `; const Text = styled.span` vertical-align: middle; ${({ theme: { rtl } }) => css` margin-${rtl ? 'right' : 'left'}: 8px;`} `; interface Props { /** Text in the Badge component */ // TODO: originally "node" from prop-types. Double check if should be ReactNode or string since its "Text in the Badge Component" children: ReactNode; /** Icon component shown next to the text */ icon?: ReactNode; /** Variant color of Badge component */ variant?: keyof typeof COLORS_MAP; /** Size of Badge component */ size?: 'small' | 'default'; /** `true` for showing as solid background color */ solid?: boolean; } /** * Badge component is label with a background color for displaying status or text. */ const Badge = ({ icon = null, variant = 'default', size = 'default', solid = false, children, ...rest }: Props) => { const badgeProps = { size, solid, variant, title: children as string | undefined, ...rest, }; return icon ? ( {icon} {children} ) : ( {children} ); }; export default Badge;