UNPKG

14.8 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 _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
13
14var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
15
16var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
17
18var React = _interopRequireWildcard(require("react"));
19
20var _propTypes = _interopRequireDefault(require("prop-types"));
21
22var _clsx = _interopRequireDefault(require("clsx"));
23
24var _utils = require("@material-ui/utils");
25
26var _withStyles = _interopRequireDefault(require("../styles/withStyles"));
27
28var _capitalize = _interopRequireDefault(require("../utils/capitalize"));
29
30var RADIUS_STANDARD = 10;
31var RADIUS_DOT = 4;
32
33var styles = function styles(theme) {
34 return {
35 /* Styles applied to the root element. */
36 root: {
37 position: 'relative',
38 display: 'inline-flex',
39 // For correct alignment with the text.
40 verticalAlign: 'middle',
41 flexShrink: 0
42 },
43
44 /* Styles applied to the badge `span` element. */
45 badge: {
46 display: 'flex',
47 flexDirection: 'row',
48 flexWrap: 'wrap',
49 justifyContent: 'center',
50 alignContent: 'center',
51 alignItems: 'center',
52 position: 'absolute',
53 boxSizing: 'border-box',
54 fontFamily: theme.typography.fontFamily,
55 fontWeight: theme.typography.fontWeightMedium,
56 fontSize: theme.typography.pxToRem(12),
57 minWidth: RADIUS_STANDARD * 2,
58 lineHeight: 1,
59 padding: '0 6px',
60 height: RADIUS_STANDARD * 2,
61 borderRadius: RADIUS_STANDARD,
62 zIndex: 1,
63 // Render the badge on top of potential ripples.
64 transition: theme.transitions.create('transform', {
65 easing: theme.transitions.easing.easeInOut,
66 duration: theme.transitions.duration.enteringScreen
67 })
68 },
69
70 /* Styles applied to the root element if `color="primary"`. */
71 colorPrimary: {
72 backgroundColor: theme.palette.primary.main,
73 color: theme.palette.primary.contrastText
74 },
75
76 /* Styles applied to the root element if `color="secondary"`. */
77 colorSecondary: {
78 backgroundColor: theme.palette.secondary.main,
79 color: theme.palette.secondary.contrastText
80 },
81
82 /* Styles applied to the root element if `color="error"`. */
83 colorError: {
84 backgroundColor: theme.palette.error.main,
85 color: theme.palette.error.contrastText
86 },
87
88 /* Styles applied to the root element if `variant="dot"`. */
89 dot: {
90 borderRadius: RADIUS_DOT,
91 height: RADIUS_DOT * 2,
92 minWidth: RADIUS_DOT * 2,
93 padding: 0
94 },
95
96 /* Styles applied to the root element if `anchorOrigin={{ 'top', 'right' }} overlap="rectangle"`. */
97 anchorOriginTopRightRectangle: {
98 top: 0,
99 right: 0,
100 transform: 'scale(1) translate(50%, -50%)',
101 transformOrigin: '100% 0%',
102 '&$invisible': {
103 transform: 'scale(0) translate(50%, -50%)'
104 }
105 },
106
107 /* Styles applied to the root element if `anchorOrigin={{ 'top', 'right' }} overlap="rectangular"`. */
108 anchorOriginTopRightRectangular: {
109 top: 0,
110 right: 0,
111 transform: 'scale(1) translate(50%, -50%)',
112 transformOrigin: '100% 0%',
113 '&$invisible': {
114 transform: 'scale(0) translate(50%, -50%)'
115 }
116 },
117
118 /* Styles applied to the root element if `anchorOrigin={{ 'bottom', 'right' }} overlap="rectangle"`. */
119 anchorOriginBottomRightRectangle: {
120 bottom: 0,
121 right: 0,
122 transform: 'scale(1) translate(50%, 50%)',
123 transformOrigin: '100% 100%',
124 '&$invisible': {
125 transform: 'scale(0) translate(50%, 50%)'
126 }
127 },
128
129 /* Styles applied to the root element if `anchorOrigin={{ 'bottom', 'right' }} overlap="rectangular"`. */
130 anchorOriginBottomRightRectangular: {
131 bottom: 0,
132 right: 0,
133 transform: 'scale(1) translate(50%, 50%)',
134 transformOrigin: '100% 100%',
135 '&$invisible': {
136 transform: 'scale(0) translate(50%, 50%)'
137 }
138 },
139
140 /* Styles applied to the root element if `anchorOrigin={{ 'top', 'left' }} overlap="rectangle"`. */
141 anchorOriginTopLeftRectangle: {
142 top: 0,
143 left: 0,
144 transform: 'scale(1) translate(-50%, -50%)',
145 transformOrigin: '0% 0%',
146 '&$invisible': {
147 transform: 'scale(0) translate(-50%, -50%)'
148 }
149 },
150
151 /* Styles applied to the root element if `anchorOrigin={{ 'top', 'left' }} overlap="rectangular"`. */
152 anchorOriginTopLeftRectangular: {
153 top: 0,
154 left: 0,
155 transform: 'scale(1) translate(-50%, -50%)',
156 transformOrigin: '0% 0%',
157 '&$invisible': {
158 transform: 'scale(0) translate(-50%, -50%)'
159 }
160 },
161
162 /* Styles applied to the root element if `anchorOrigin={{ 'bottom', 'left' }} overlap="rectangle"`. */
163 anchorOriginBottomLeftRectangle: {
164 bottom: 0,
165 left: 0,
166 transform: 'scale(1) translate(-50%, 50%)',
167 transformOrigin: '0% 100%',
168 '&$invisible': {
169 transform: 'scale(0) translate(-50%, 50%)'
170 }
171 },
172
173 /* Styles applied to the root element if `anchorOrigin={{ 'bottom', 'left' }} overlap="rectangular"`. */
174 anchorOriginBottomLeftRectangular: {
175 bottom: 0,
176 left: 0,
177 transform: 'scale(1) translate(-50%, 50%)',
178 transformOrigin: '0% 100%',
179 '&$invisible': {
180 transform: 'scale(0) translate(-50%, 50%)'
181 }
182 },
183
184 /* Styles applied to the root element if `anchorOrigin={{ 'top', 'right' }} overlap="circle"`. */
185 anchorOriginTopRightCircle: {
186 top: '14%',
187 right: '14%',
188 transform: 'scale(1) translate(50%, -50%)',
189 transformOrigin: '100% 0%',
190 '&$invisible': {
191 transform: 'scale(0) translate(50%, -50%)'
192 }
193 },
194
195 /* Styles applied to the root element if `anchorOrigin={{ 'top', 'right' }} overlap="circular"`. */
196 anchorOriginTopRightCircular: {
197 top: '14%',
198 right: '14%',
199 transform: 'scale(1) translate(50%, -50%)',
200 transformOrigin: '100% 0%',
201 '&$invisible': {
202 transform: 'scale(0) translate(50%, -50%)'
203 }
204 },
205
206 /* Styles applied to the root element if `anchorOrigin={{ 'bottom', 'right' }} overlap="circle"`. */
207 anchorOriginBottomRightCircle: {
208 bottom: '14%',
209 right: '14%',
210 transform: 'scale(1) translate(50%, 50%)',
211 transformOrigin: '100% 100%',
212 '&$invisible': {
213 transform: 'scale(0) translate(50%, 50%)'
214 }
215 },
216
217 /* Styles applied to the root element if `anchorOrigin={{ 'bottom', 'right' }} overlap="circular"`. */
218 anchorOriginBottomRightCircular: {
219 bottom: '14%',
220 right: '14%',
221 transform: 'scale(1) translate(50%, 50%)',
222 transformOrigin: '100% 100%',
223 '&$invisible': {
224 transform: 'scale(0) translate(50%, 50%)'
225 }
226 },
227
228 /* Styles applied to the root element if `anchorOrigin={{ 'top', 'left' }} overlap="circle"`. */
229 anchorOriginTopLeftCircle: {
230 top: '14%',
231 left: '14%',
232 transform: 'scale(1) translate(-50%, -50%)',
233 transformOrigin: '0% 0%',
234 '&$invisible': {
235 transform: 'scale(0) translate(-50%, -50%)'
236 }
237 },
238
239 /* Styles applied to the root element if `anchorOrigin={{ 'top', 'left' }} overlap="circular"`. */
240 anchorOriginTopLeftCircular: {
241 top: '14%',
242 left: '14%',
243 transform: 'scale(1) translate(-50%, -50%)',
244 transformOrigin: '0% 0%',
245 '&$invisible': {
246 transform: 'scale(0) translate(-50%, -50%)'
247 }
248 },
249
250 /* Styles applied to the root element if `anchorOrigin={{ 'bottom', 'left' }} overlap="circle"`. */
251 anchorOriginBottomLeftCircle: {
252 bottom: '14%',
253 left: '14%',
254 transform: 'scale(1) translate(-50%, 50%)',
255 transformOrigin: '0% 100%',
256 '&$invisible': {
257 transform: 'scale(0) translate(-50%, 50%)'
258 }
259 },
260
261 /* Styles applied to the root element if `anchorOrigin={{ 'bottom', 'left' }} overlap="circular"`. */
262 anchorOriginBottomLeftCircular: {
263 bottom: '14%',
264 left: '14%',
265 transform: 'scale(1) translate(-50%, 50%)',
266 transformOrigin: '0% 100%',
267 '&$invisible': {
268 transform: 'scale(0) translate(-50%, 50%)'
269 }
270 },
271
272 /* Pseudo-class to the badge `span` element if `invisible={true}`. */
273 invisible: {
274 transition: theme.transitions.create('transform', {
275 easing: theme.transitions.easing.easeInOut,
276 duration: theme.transitions.duration.leavingScreen
277 })
278 }
279 };
280};
281
282exports.styles = styles;
283var Badge = /*#__PURE__*/React.forwardRef(function Badge(props, ref) {
284 var _props$anchorOrigin = props.anchorOrigin,
285 anchorOrigin = _props$anchorOrigin === void 0 ? {
286 vertical: 'top',
287 horizontal: 'right'
288 } : _props$anchorOrigin,
289 badgeContent = props.badgeContent,
290 children = props.children,
291 classes = props.classes,
292 className = props.className,
293 _props$color = props.color,
294 color = _props$color === void 0 ? 'default' : _props$color,
295 _props$component = props.component,
296 ComponentProp = _props$component === void 0 ? 'span' : _props$component,
297 invisibleProp = props.invisible,
298 _props$max = props.max,
299 max = _props$max === void 0 ? 99 : _props$max,
300 _props$overlap = props.overlap,
301 overlap = _props$overlap === void 0 ? 'rectangle' : _props$overlap,
302 _props$showZero = props.showZero,
303 showZero = _props$showZero === void 0 ? false : _props$showZero,
304 _props$variant = props.variant,
305 variant = _props$variant === void 0 ? 'standard' : _props$variant,
306 other = (0, _objectWithoutProperties2.default)(props, ["anchorOrigin", "badgeContent", "children", "classes", "className", "color", "component", "invisible", "max", "overlap", "showZero", "variant"]);
307 var invisible = invisibleProp;
308
309 if (invisibleProp == null && (badgeContent === 0 && !showZero || badgeContent == null && variant !== 'dot')) {
310 invisible = true;
311 }
312
313 var displayValue = '';
314
315 if (variant !== 'dot') {
316 displayValue = badgeContent > max ? "".concat(max, "+") : badgeContent;
317 }
318
319 return /*#__PURE__*/React.createElement(ComponentProp, (0, _extends2.default)({
320 className: (0, _clsx.default)(classes.root, className),
321 ref: ref
322 }, other), children, /*#__PURE__*/React.createElement("span", {
323 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)
324 }, displayValue));
325});
326process.env.NODE_ENV !== "production" ? Badge.propTypes = {
327 // ----------------------------- Warning --------------------------------
328 // | These PropTypes are generated from the TypeScript type definitions |
329 // | To update them edit the d.ts file and run "yarn proptypes" |
330 // ----------------------------------------------------------------------
331
332 /**
333 * The anchor of the badge.
334 */
335 anchorOrigin: _propTypes.default.shape({
336 horizontal: _propTypes.default.oneOf(['left', 'right']).isRequired,
337 vertical: _propTypes.default.oneOf(['bottom', 'top']).isRequired
338 }),
339
340 /**
341 * The content rendered within the badge.
342 */
343 badgeContent: _propTypes.default.node,
344
345 /**
346 * The badge will be added relative to this node.
347 */
348 children: _propTypes.default.node,
349
350 /**
351 * Override or extend the styles applied to the component.
352 * See [CSS API](#css) below for more details.
353 */
354 classes: (0, _utils.chainPropTypes)(_propTypes.default.object, function (props) {
355 var classes = props.classes;
356
357 if (classes == null) {
358 return null;
359 }
360
361 [['anchorOriginTopRightRectangle', 'anchorOriginTopRightRectangular'], ['anchorOriginBottomRightRectangle', 'anchorOriginBottomRightRectangular'], ['anchorOriginTopLeftRectangle', 'anchorOriginTopLeftRectangular'], ['anchorOriginBottomLeftRectangle', 'anchorOriginBottomLeftRectangular'], ['anchorOriginTopRightCircle', 'anchorOriginTopRightCircular'], ['anchorOriginBottomRightCircle', 'anchorOriginBottomRightCircular'], ['anchorOriginTopLeftCircle', 'anchorOriginTopLeftCircular']].forEach(function (_ref) {
362 var _ref2 = (0, _slicedToArray2.default)(_ref, 2),
363 deprecatedClassKey = _ref2[0],
364 newClassKey = _ref2[1];
365
366 if (classes[deprecatedClassKey] != null && // 2 classnames? one from withStyles the other must be custom
367 classes[deprecatedClassKey].split(' ').length > 1) {
368 throw new Error("Material-UI: The `".concat(deprecatedClassKey, "` class was deprecated. Use `").concat(newClassKey, "` instead."));
369 }
370 });
371 return null;
372 }),
373
374 /**
375 * @ignore
376 */
377 className: _propTypes.default.string,
378
379 /**
380 * The color of the component. It supports those theme colors that make sense for this component.
381 */
382 color: _propTypes.default.oneOf(['default', 'error', 'primary', 'secondary']),
383
384 /**
385 * The component used for the root node.
386 * Either a string to use a HTML element or a component.
387 */
388 component: _propTypes.default
389 /* @typescript-to-proptypes-ignore */
390 .elementType,
391
392 /**
393 * If `true`, the badge will be invisible.
394 */
395 invisible: _propTypes.default.bool,
396
397 /**
398 * Max count to show.
399 */
400 max: _propTypes.default.number,
401
402 /**
403 * Wrapped shape the badge should overlap.
404 */
405 overlap: (0, _utils.chainPropTypes)(_propTypes.default.oneOf(['circle', 'rectangle', 'circular', 'rectangular']), function (props) {
406 var overlap = props.overlap;
407
408 if (overlap === 'rectangle') {
409 throw new Error('Material-UI: `overlap="rectangle"` was deprecated. Use `overlap="rectangular"` instead.');
410 }
411
412 if (overlap === 'circle') {
413 throw new Error('Material-UI: `overlap="circle"` was deprecated. Use `overlap="circular"` instead.');
414 }
415
416 return null;
417 }),
418
419 /**
420 * Controls whether the badge is hidden when `badgeContent` is zero.
421 */
422 showZero: _propTypes.default.bool,
423
424 /**
425 * The variant to use.
426 */
427 variant: _propTypes.default.oneOf(['dot', 'standard'])
428} : void 0;
429
430var _default = (0, _withStyles.default)(styles, {
431 name: 'MuiBadge'
432})(Badge);
433
434exports.default = _default;
\No newline at end of file