UNPKG

10.2 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
4
5var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
6
7Object.defineProperty(exports, "__esModule", {
8 value: true
9});
10exports.default = exports.styles = void 0;
11
12var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
13
14var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
15
16var React = _interopRequireWildcard(require("react"));
17
18var _propTypes = _interopRequireDefault(require("prop-types"));
19
20var _clsx = _interopRequireDefault(require("clsx"));
21
22var _withStyles = _interopRequireDefault(require("../styles/withStyles"));
23
24var _capitalize = _interopRequireDefault(require("../utils/capitalize"));
25
26var RADIUS_STANDARD = 10;
27var RADIUS_DOT = 4;
28
29var styles = function styles(theme) {
30 return {
31 /* Styles applied to the root element. */
32 root: {
33 position: 'relative',
34 display: 'inline-flex',
35 // For correct alignment with the text.
36 verticalAlign: 'middle',
37 flexShrink: 0
38 },
39
40 /* Styles applied to the badge `span` element. */
41 badge: {
42 display: 'flex',
43 flexDirection: 'row',
44 flexWrap: 'wrap',
45 justifyContent: 'center',
46 alignContent: 'center',
47 alignItems: 'center',
48 position: 'absolute',
49 boxSizing: 'border-box',
50 fontFamily: theme.typography.fontFamily,
51 fontWeight: theme.typography.fontWeightMedium,
52 fontSize: theme.typography.pxToRem(12),
53 minWidth: RADIUS_STANDARD * 2,
54 lineHeight: 1,
55 padding: '0 6px',
56 height: RADIUS_STANDARD * 2,
57 borderRadius: RADIUS_STANDARD,
58 zIndex: 1,
59 // Render the badge on top of potential ripples.
60 transition: theme.transitions.create('transform', {
61 easing: theme.transitions.easing.easeInOut,
62 duration: theme.transitions.duration.enteringScreen
63 })
64 },
65
66 /* Styles applied to the root element if `color="primary"`. */
67 colorPrimary: {
68 backgroundColor: theme.palette.primary.main,
69 color: theme.palette.primary.contrastText
70 },
71
72 /* Styles applied to the root element if `color="secondary"`. */
73 colorSecondary: {
74 backgroundColor: theme.palette.secondary.main,
75 color: theme.palette.secondary.contrastText
76 },
77
78 /* Styles applied to the root element if `color="error"`. */
79 colorError: {
80 backgroundColor: theme.palette.error.main,
81 color: theme.palette.error.contrastText
82 },
83
84 /* Styles applied to the root element if `variant="dot"`. */
85 dot: {
86 borderRadius: RADIUS_DOT,
87 height: RADIUS_DOT * 2,
88 minWidth: RADIUS_DOT * 2,
89 padding: 0
90 },
91
92 /* Styles applied to the root element if `anchorOrigin={{ 'top', 'right' }} overlap="rectangle"`. */
93 anchorOriginTopRightRectangle: {
94 top: 0,
95 right: 0,
96 transform: 'scale(1) translate(50%, -50%)',
97 transformOrigin: '100% 0%',
98 '&$invisible': {
99 transform: 'scale(0) translate(50%, -50%)'
100 }
101 },
102
103 /* Styles applied to the root element if `anchorOrigin={{ 'bottom', 'right' }} overlap="rectangle"`. */
104 anchorOriginBottomRightRectangle: {
105 bottom: 0,
106 right: 0,
107 transform: 'scale(1) translate(50%, 50%)',
108 transformOrigin: '100% 100%',
109 '&$invisible': {
110 transform: 'scale(0) translate(50%, 50%)'
111 }
112 },
113
114 /* Styles applied to the root element if `anchorOrigin={{ 'top', 'left' }} overlap="rectangle"`. */
115 anchorOriginTopLeftRectangle: {
116 top: 0,
117 left: 0,
118 transform: 'scale(1) translate(-50%, -50%)',
119 transformOrigin: '0% 0%',
120 '&$invisible': {
121 transform: 'scale(0) translate(-50%, -50%)'
122 }
123 },
124
125 /* Styles applied to the root element if `anchorOrigin={{ 'bottom', 'left' }} overlap="rectangle"`. */
126 anchorOriginBottomLeftRectangle: {
127 bottom: 0,
128 left: 0,
129 transform: 'scale(1) translate(-50%, 50%)',
130 transformOrigin: '0% 100%',
131 '&$invisible': {
132 transform: 'scale(0) translate(-50%, 50%)'
133 }
134 },
135
136 /* Styles applied to the root element if `anchorOrigin={{ 'top', 'right' }} overlap="circle"`. */
137 anchorOriginTopRightCircle: {
138 top: '14%',
139 right: '14%',
140 transform: 'scale(1) translate(50%, -50%)',
141 transformOrigin: '100% 0%',
142 '&$invisible': {
143 transform: 'scale(0) translate(50%, -50%)'
144 }
145 },
146
147 /* Styles applied to the root element if `anchorOrigin={{ 'bottom', 'right' }} overlap="circle"`. */
148 anchorOriginBottomRightCircle: {
149 bottom: '14%',
150 right: '14%',
151 transform: 'scale(1) translate(50%, 50%)',
152 transformOrigin: '100% 100%',
153 '&$invisible': {
154 transform: 'scale(0) translate(50%, 50%)'
155 }
156 },
157
158 /* Styles applied to the root element if `anchorOrigin={{ 'top', 'left' }} overlap="circle"`. */
159 anchorOriginTopLeftCircle: {
160 top: '14%',
161 left: '14%',
162 transform: 'scale(1) translate(-50%, -50%)',
163 transformOrigin: '0% 0%',
164 '&$invisible': {
165 transform: 'scale(0) translate(-50%, -50%)'
166 }
167 },
168
169 /* Styles applied to the root element if `anchorOrigin={{ 'bottom', 'left' }} overlap="circle"`. */
170 anchorOriginBottomLeftCircle: {
171 bottom: '14%',
172 left: '14%',
173 transform: 'scale(1) translate(-50%, 50%)',
174 transformOrigin: '0% 100%',
175 '&$invisible': {
176 transform: 'scale(0) translate(-50%, 50%)'
177 }
178 },
179
180 /* Pseudo-class to the badge `span` element if `invisible={true}`. */
181 invisible: {
182 transition: theme.transitions.create('transform', {
183 easing: theme.transitions.easing.easeInOut,
184 duration: theme.transitions.duration.leavingScreen
185 })
186 }
187 };
188};
189
190exports.styles = styles;
191var Badge = /*#__PURE__*/React.forwardRef(function Badge(props, ref) {
192 var _props$anchorOrigin = props.anchorOrigin,
193 anchorOrigin = _props$anchorOrigin === void 0 ? {
194 vertical: 'top',
195 horizontal: 'right'
196 } : _props$anchorOrigin,
197 badgeContent = props.badgeContent,
198 children = props.children,
199 classes = props.classes,
200 className = props.className,
201 _props$color = props.color,
202 color = _props$color === void 0 ? 'default' : _props$color,
203 _props$component = props.component,
204 ComponentProp = _props$component === void 0 ? 'span' : _props$component,
205 invisibleProp = props.invisible,
206 _props$max = props.max,
207 max = _props$max === void 0 ? 99 : _props$max,
208 _props$overlap = props.overlap,
209 overlap = _props$overlap === void 0 ? 'rectangle' : _props$overlap,
210 _props$showZero = props.showZero,
211 showZero = _props$showZero === void 0 ? false : _props$showZero,
212 _props$variant = props.variant,
213 variant = _props$variant === void 0 ? 'standard' : _props$variant,
214 other = (0, _objectWithoutProperties2.default)(props, ["anchorOrigin", "badgeContent", "children", "classes", "className", "color", "component", "invisible", "max", "overlap", "showZero", "variant"]);
215 var invisible = invisibleProp;
216
217 if (invisibleProp == null && (badgeContent === 0 && !showZero || badgeContent == null && variant !== 'dot')) {
218 invisible = true;
219 }
220
221 var displayValue = '';
222
223 if (variant !== 'dot') {
224 displayValue = badgeContent > max ? "".concat(max, "+") : badgeContent;
225 }
226
227 return /*#__PURE__*/React.createElement(ComponentProp, (0, _extends2.default)({
228 className: (0, _clsx.default)(classes.root, className),
229 ref: ref
230 }, other), children, /*#__PURE__*/React.createElement("span", {
231 className: (0, _clsx.default)(classes.badge, classes["".concat(anchorOrigin.horizontal).concat((0, _capitalize.default)(anchorOrigin.vertical), "}")], classes["anchorOrigin".concat((0, _capitalize.default)(anchorOrigin.vertical)).concat((0, _capitalize.default)(anchorOrigin.horizontal)).concat((0, _capitalize.default)(overlap))], color !== 'default' && classes["color".concat((0, _capitalize.default)(color))], invisible && classes.invisible, variant === 'dot' && classes.dot)
232 }, displayValue));
233});
234process.env.NODE_ENV !== "production" ? Badge.propTypes = {
235 // ----------------------------- Warning --------------------------------
236 // | These PropTypes are generated from the TypeScript type definitions |
237 // | To update them edit the d.ts file and run "yarn proptypes" |
238 // ----------------------------------------------------------------------
239
240 /**
241 * The anchor of the badge.
242 */
243 anchorOrigin: _propTypes.default.shape({
244 horizontal: _propTypes.default.oneOf(['left', 'right']).isRequired,
245 vertical: _propTypes.default.oneOf(['bottom', 'top']).isRequired
246 }),
247
248 /**
249 * The content rendered within the badge.
250 */
251 badgeContent: _propTypes.default.node,
252
253 /**
254 * The badge will be added relative to this node.
255 */
256 children: _propTypes.default.node,
257
258 /**
259 * Override or extend the styles applied to the component.
260 * See [CSS API](#css) below for more details.
261 */
262 classes: _propTypes.default.object,
263
264 /**
265 * @ignore
266 */
267 className: _propTypes.default.string,
268
269 /**
270 * The color of the component. It supports those theme colors that make sense for this component.
271 */
272 color: _propTypes.default.oneOf(['default', 'error', 'primary', 'secondary']),
273
274 /**
275 * The component used for the root node.
276 * Either a string to use a HTML element or a component.
277 */
278 component: _propTypes.default
279 /* @typescript-to-proptypes-ignore */
280 .elementType,
281
282 /**
283 * If `true`, the badge will be invisible.
284 */
285 invisible: _propTypes.default.bool,
286
287 /**
288 * Max count to show.
289 */
290 max: _propTypes.default.number,
291
292 /**
293 * Wrapped shape the badge should overlap.
294 */
295 overlap: _propTypes.default.oneOf(['circle', 'rectangle']),
296
297 /**
298 * Controls whether the badge is hidden when `badgeContent` is zero.
299 */
300 showZero: _propTypes.default.bool,
301
302 /**
303 * The variant to use.
304 */
305 variant: _propTypes.default.oneOf(['dot', 'standard'])
306} : void 0;
307
308var _default = (0, _withStyles.default)(styles, {
309 name: 'MuiBadge'
310})(Badge);
311
312exports.default = _default;
\No newline at end of file