@material-ui/core
Version:
React components that implement Google's Material Design.
203 lines (168 loc) • 6.15 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.styles = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _clsx = _interopRequireDefault(require("clsx"));
var _withStyles = _interopRequireDefault(require("../styles/withStyles"));
var _helpers = require("../utils/helpers");
var RADIUS = 10;
var styles = function styles(theme) {
return {
/* Styles applied to the root element. */
root: {
position: 'relative',
display: 'inline-flex',
// For correct alignment with the text.
verticalAlign: 'middle'
},
/* Styles applied to the badge `span` element. */
badge: {
display: 'flex',
flexDirection: 'row',
flexWrap: 'wrap',
justifyContent: 'center',
alignContent: 'center',
alignItems: 'center',
position: 'absolute',
top: 0,
right: 0,
boxSizing: 'border-box',
fontFamily: theme.typography.fontFamily,
fontWeight: theme.typography.fontWeightMedium,
fontSize: theme.typography.pxToRem(12),
minWidth: RADIUS * 2,
padding: '0 4px',
height: RADIUS * 2,
borderRadius: RADIUS,
backgroundColor: theme.palette.color,
color: theme.palette.textColor,
zIndex: 1,
// Render the badge on top of potential ripples.
transform: 'scale(1) translate(50%, -50%)',
transformOrigin: '100% 0%',
transition: theme.transitions.create('transform', {
easing: theme.transitions.easing.easeInOut,
duration: theme.transitions.duration.enteringScreen
})
},
/* Styles applied to the root element if `color="primary"`. */
colorPrimary: {
backgroundColor: theme.palette.primary.main,
color: theme.palette.primary.contrastText
},
/* Styles applied to the root element if `color="secondary"`. */
colorSecondary: {
backgroundColor: theme.palette.secondary.main,
color: theme.palette.secondary.contrastText
},
/* Styles applied to the root element if `color="error"`. */
colorError: {
backgroundColor: theme.palette.error.main,
color: theme.palette.error.contrastText
},
/* Styles applied to the badge `span` element if `invisible={true}`. */
invisible: {
transition: theme.transitions.create('transform', {
easing: theme.transitions.easing.easeInOut,
duration: theme.transitions.duration.leavingScreen
}),
transform: 'scale(0) translate(50%, -50%)',
transformOrigin: '100% 0%'
},
/* Styles applied to the root element if `variant="dot"`. */
dot: {
height: 6,
minWidth: 6,
padding: 0
}
};
};
exports.styles = styles;
var Badge = _react.default.forwardRef(function Badge(props, ref) {
var badgeContent = props.badgeContent,
children = props.children,
classes = props.classes,
className = props.className,
_props$color = props.color,
color = _props$color === void 0 ? 'default' : _props$color,
_props$component = props.component,
ComponentProp = _props$component === void 0 ? 'span' : _props$component,
invisibleProp = props.invisible,
_props$max = props.max,
max = _props$max === void 0 ? 99 : _props$max,
_props$showZero = props.showZero,
showZero = _props$showZero === void 0 ? false : _props$showZero,
_props$variant = props.variant,
variant = _props$variant === void 0 ? 'standard' : _props$variant,
other = (0, _objectWithoutProperties2.default)(props, ["badgeContent", "children", "classes", "className", "color", "component", "invisible", "max", "showZero", "variant"]);
var invisible = invisibleProp;
if (invisibleProp == null && (badgeContent === 0 && !showZero || badgeContent == null && variant !== 'dot')) {
invisible = true;
}
var displayValue = '';
if (variant !== 'dot') {
displayValue = badgeContent > max ? "".concat(max, "+") : badgeContent;
}
return _react.default.createElement(ComponentProp, (0, _extends2.default)({
className: (0, _clsx.default)(classes.root, className),
ref: ref
}, other), children, _react.default.createElement("span", {
className: (0, _clsx.default)(classes.badge, color !== 'default' && classes["color".concat((0, _helpers.capitalize)(color))], invisible && classes.invisible, {
dot: classes.dot
}[variant])
}, displayValue));
});
process.env.NODE_ENV !== "production" ? Badge.propTypes = {
/**
* The content rendered within the badge.
*/
badgeContent: _propTypes.default.node,
/**
* The badge will be added relative to this node.
*/
children: _propTypes.default.node.isRequired,
/**
* Override or extend the styles applied to the component.
* See [CSS API](#css) below for more details.
*/
classes: _propTypes.default.object.isRequired,
/**
* @ignore
*/
className: _propTypes.default.string,
/**
* The color of the component. It supports those theme colors that make sense for this component.
*/
color: _propTypes.default.oneOf(['default', 'primary', 'secondary', 'error']),
/**
* The component used for the root node.
* Either a string to use a DOM element or a component.
*/
component: _propTypes.default.elementType,
/**
* If `true`, the badge will be invisible.
*/
invisible: _propTypes.default.bool,
/**
* Max count to show.
*/
max: _propTypes.default.number,
/**
* Controls whether the badge is hidden when `badgeContent` is zero.
*/
showZero: _propTypes.default.bool,
/**
* The variant to use.
*/
variant: _propTypes.default.oneOf(['standard', 'dot'])
} : void 0;
var _default = (0, _withStyles.default)(styles, {
name: 'MuiBadge'
})(Badge);
exports.default = _default;