UNPKG

@material-ui/core

Version:

React components that implement Google's Material Design.

358 lines (308 loc) 11.7 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); 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 = _interopRequireWildcard(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _clsx = _interopRequireDefault(require("clsx")); var _styles = require("@material-ui/styles"); var _withStyles = _interopRequireDefault(require("../styles/withStyles")); var _capitalize = _interopRequireDefault(require("../utils/capitalize")); var RADIUS_STANDARD = 10; var RADIUS_DOT = 4; 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', flexShrink: 0 }, /* Styles applied to the badge `span` element. */ badge: { display: 'flex', flexDirection: 'row', flexWrap: 'wrap', justifyContent: 'center', alignContent: 'center', alignItems: 'center', position: 'absolute', boxSizing: 'border-box', fontFamily: theme.typography.fontFamily, fontWeight: theme.typography.fontWeightMedium, fontSize: theme.typography.pxToRem(12), minWidth: RADIUS_STANDARD * 2, lineHeight: 1, padding: '0 6px', height: RADIUS_STANDARD * 2, borderRadius: RADIUS_STANDARD, zIndex: 1, // Render the badge on top of potential ripples. 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 root element if `variant="dot"`. */ dot: { borderRadius: RADIUS_DOT, height: RADIUS_DOT * 2, minWidth: RADIUS_DOT * 2, padding: 0 }, /* Styles applied to the root element if `variant="standard"`. */ standard: {}, /* Styles applied to the root element if `anchorOrigin={{ 'top', 'right' }} overlap="rectangular"`. */ anchorOriginTopRightRectangular: { top: 0, right: 0, transform: 'scale(1) translate(50%, -50%)', transformOrigin: '100% 0%', '&$invisible': { transform: 'scale(0) translate(50%, -50%)' } }, /* Styles applied to the root element if `anchorOrigin={{ 'bottom', 'right' }} overlap="rectangular"`. */ anchorOriginBottomRightRectangular: { bottom: 0, right: 0, transform: 'scale(1) translate(50%, 50%)', transformOrigin: '100% 100%', '&$invisible': { transform: 'scale(0) translate(50%, 50%)' } }, /* Styles applied to the root element if `anchorOrigin={{ 'top', 'left' }} overlap="rectangular"`. */ anchorOriginTopLeftRectangular: { top: 0, left: 0, transform: 'scale(1) translate(-50%, -50%)', transformOrigin: '0% 0%', '&$invisible': { transform: 'scale(0) translate(-50%, -50%)' } }, /* Styles applied to the root element if `anchorOrigin={{ 'bottom', 'left' }} overlap="rectangular"`. */ anchorOriginBottomLeftRectangular: { bottom: 0, left: 0, transform: 'scale(1) translate(-50%, 50%)', transformOrigin: '0% 100%', '&$invisible': { transform: 'scale(0) translate(-50%, 50%)' } }, /* Styles applied to the root element if `anchorOrigin={{ 'top', 'right' }} overlap="circular"`. */ anchorOriginTopRightCircular: { top: '14%', right: '14%', transform: 'scale(1) translate(50%, -50%)', transformOrigin: '100% 0%', '&$invisible': { transform: 'scale(0) translate(50%, -50%)' } }, /* Styles applied to the root element if `anchorOrigin={{ 'bottom', 'right' }} overlap="circular"`. */ anchorOriginBottomRightCircular: { bottom: '14%', right: '14%', transform: 'scale(1) translate(50%, 50%)', transformOrigin: '100% 100%', '&$invisible': { transform: 'scale(0) translate(50%, 50%)' } }, /* Styles applied to the root element if `anchorOrigin={{ 'top', 'left' }} overlap="circular"`. */ anchorOriginTopLeftCircular: { top: '14%', left: '14%', transform: 'scale(1) translate(-50%, -50%)', transformOrigin: '0% 0%', '&$invisible': { transform: 'scale(0) translate(-50%, -50%)' } }, /* Styles applied to the root element if `anchorOrigin={{ 'bottom', 'left' }} overlap="circular"`. */ anchorOriginBottomLeftCircular: { bottom: '14%', left: '14%', transform: 'scale(1) translate(-50%, 50%)', transformOrigin: '0% 100%', '&$invisible': { transform: 'scale(0) translate(-50%, 50%)' } }, /* Pseudo-class to the badge `span` element if `invisible={true}`. */ invisible: { transition: theme.transitions.create('transform', { easing: theme.transitions.easing.easeInOut, duration: theme.transitions.duration.leavingScreen }) } }; }; exports.styles = styles; var usePreviousProps = function usePreviousProps(value) { var ref = React.useRef({}); React.useEffect(function () { ref.current = value; }); return ref.current; }; var Badge = /*#__PURE__*/React.forwardRef(function Badge(props, ref) { var _props$anchorOrigin = props.anchorOrigin, anchorOriginProp = _props$anchorOrigin === void 0 ? { vertical: 'top', horizontal: 'right' } : _props$anchorOrigin, badgeContentProp = props.badgeContent, children = props.children, classes = props.classes, className = props.className, _props$color = props.color, colorProp = _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, maxProp = _props$max === void 0 ? 99 : _props$max, _props$overlap = props.overlap, overlapProp = _props$overlap === void 0 ? 'rectangular' : _props$overlap, _props$showZero = props.showZero, showZero = _props$showZero === void 0 ? false : _props$showZero, _props$variant = props.variant, variantProp = _props$variant === void 0 ? 'standard' : _props$variant, other = (0, _objectWithoutProperties2.default)(props, ["anchorOrigin", "badgeContent", "children", "classes", "className", "color", "component", "invisible", "max", "overlap", "showZero", "variant"]); var prevProps = usePreviousProps({ anchorOrigin: anchorOriginProp, badgeContent: badgeContentProp, color: colorProp, max: maxProp, overlap: overlapProp, variant: variantProp }); var invisible = invisibleProp; if (invisibleProp == null && (badgeContentProp === 0 && !showZero || badgeContentProp == null && variantProp !== 'dot')) { invisible = true; } var _ref = invisible ? prevProps : props, _ref$anchorOrigin = _ref.anchorOrigin, anchorOrigin = _ref$anchorOrigin === void 0 ? anchorOriginProp : _ref$anchorOrigin, badgeContent = _ref.badgeContent, _ref$color = _ref.color, color = _ref$color === void 0 ? colorProp : _ref$color, _ref$max = _ref.max, max = _ref$max === void 0 ? maxProp : _ref$max, _ref$overlap = _ref.overlap, overlap = _ref$overlap === void 0 ? overlapProp : _ref$overlap, _ref$variant = _ref.variant, variant = _ref$variant === void 0 ? variantProp : _ref$variant; var themeVariantsClasses = (0, _styles.useThemeVariants)((0, _extends2.default)({}, props, { anchorOrigin: anchorOrigin, badgeContent: badgeContent, color: color, component: ComponentProp, invisible: invisible, max: max, overlap: overlap, showZero: showZero, variant: variant }), 'MuiBadge'); var displayValue = ''; if (variant !== 'dot') { displayValue = badgeContent > max ? "".concat(max, "+") : badgeContent; } return /*#__PURE__*/React.createElement(ComponentProp, (0, _extends2.default)({ className: (0, _clsx.default)(classes.root, className), ref: ref }, other), children, /*#__PURE__*/React.createElement("span", { className: (0, _clsx.default)(classes.badge, classes[variant], classes["anchorOrigin".concat((0, _capitalize.default)(anchorOrigin.vertical)).concat((0, _capitalize.default)(anchorOrigin.horizontal)).concat((0, _capitalize.default)(overlap))], themeVariantsClasses, color !== 'default' && classes["color".concat((0, _capitalize.default)(color))], invisible && classes.invisible) }, displayValue)); }); process.env.NODE_ENV !== "production" ? Badge.propTypes = { // ----------------------------- Warning -------------------------------- // | These PropTypes are generated from the TypeScript type definitions | // | To update them edit the d.ts file and run "yarn proptypes" | // ---------------------------------------------------------------------- /** * The anchor of the badge. */ anchorOrigin: _propTypes.default.shape({ horizontal: _propTypes.default.oneOf(['left', 'right']).isRequired, vertical: _propTypes.default.oneOf(['bottom', 'top']).isRequired }), /** * The content rendered within the badge. */ badgeContent: _propTypes.default.node, /** * The badge will be added relative to this node. */ children: _propTypes.default.node, /** * Override or extend the styles applied to the component. * See [CSS API](#css) below for more details. */ classes: _propTypes.default.object, /** * @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', 'error', 'primary', 'secondary']), /** * The component used for the root node. * Either a string to use a HTML 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, /** * Wrapped shape the badge should overlap. */ overlap: _propTypes.default.oneOf(['circular', 'rectangular']), /** * Controls whether the badge is hidden when `badgeContent` is zero. */ showZero: _propTypes.default.bool, /** * The variant to use. */ variant: _propTypes.default /* @typescript-to-proptypes-ignore */ .oneOfType([_propTypes.default.oneOf(['dot', 'standard']), _propTypes.default.string]) } : void 0; var _default = (0, _withStyles.default)(styles, { name: 'MuiBadge' })(Badge); exports.default = _default;