@braineet/ui
Version:
Braineet design system
83 lines (79 loc) • 3.01 kB
JavaScript
var _excluded = ["as", "color", "isSolid", "size", "children", "ellipsis", "onClickIcon", "iconBefore", "iconAfter"];
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
import React from 'react';
import { useTheme } from 'styled-components';
import Box from '../box';
import Icon from '../icon';
import styles, { StyledBadge, StyledText } from './styles';
/**
The `Badge` component.
*/
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
var Badge = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
var as = _ref.as,
color = _ref.color,
isSolid = _ref.isSolid,
size = _ref.size,
children = _ref.children,
ellipsis = _ref.ellipsis,
onClickIcon = _ref.onClickIcon,
iconBefore = _ref.iconBefore,
iconAfter = _ref.iconAfter,
restProps = _objectWithoutPropertiesLoose(_ref, _excluded);
var theme = useTheme();
// Define if badge is solid or subtle
var appearance = isSolid ? 'solid' : 'subtle';
// Get text color and background color of badge
var _theme$helpers$getFil = theme.helpers.getFills({
appearance: appearance,
color: color
}),
textColor = _theme$helpers$getFil.color,
backgroundColor = _theme$helpers$getFil.backgroundColor;
// Define the badge size
var globalSize = ['small', 'big'].includes(size) ? size : 'big';
return /*#__PURE__*/_jsxs(StyledBadge, _extends({
forwardedAs: as,
ref: ref,
textColor: textColor,
backgroundColor: backgroundColor,
isSolid: isSolid,
size: globalSize,
reverse: iconAfter !== null
}, restProps, {
children: [(iconBefore || iconAfter) && /*#__PURE__*/_jsx(Box, {
onClick: onClickIcon,
cursor: onClickIcon ? 'pointer' : null,
flexShrink: "0",
width: styles.sizes[globalSize].icon,
height: styles.sizes[globalSize].icon,
children: /*#__PURE__*/_jsx(Icon, {
color: textColor,
name: iconBefore || iconAfter,
size: styles.sizes[globalSize].icon
})
}), /*#__PURE__*/_jsx(StyledText, {
fontFamily: "display",
color: textColor,
ellipsis: ellipsis,
margin: globalSize === 'small' ? "0 " + theme.spaces.sm : theme.spaces.xs + " " + theme.spaces.md,
fontWeight: 700,
size: styles.sizes[globalSize].text,
lineHeight: styles.sizes[globalSize].lineHeight,
children: children
})]
}));
});
Badge.defaultProps = {
children: '',
iconBefore: null,
iconAfter: null,
isSolid: false,
color: 'black',
size: 'big',
ellipsis: false,
onClickIcon: null
};
Badge.displayName = 'Badge';
export default Badge;