-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Badge.js
286 lines (275 loc) · 8.52 KB
/
Badge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import React, { useRef } from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import withStyles from '../styles/withStyles';
import capitalize from '../utils/capitalize';
const RADIUS_STANDARD = 10;
const RADIUS_DOT = 4;
export const styles = theme => ({
/* 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 `anchorOrigin={{ 'top', 'right' }} overlap="rectangle"`. */
anchorOriginTopRightRectangle: {
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="rectangle"`. */
anchorOriginBottomRightRectangle: {
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="rectangle"`. */
anchorOriginTopLeftRectangle: {
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="rectangle"`. */
anchorOriginBottomLeftRectangle: {
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="circle"`. */
anchorOriginTopRightCircle: {
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="circle"`. */
anchorOriginBottomRightCircle: {
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="circle"`. */
anchorOriginTopLeftCircle: {
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="circle"`. */
anchorOriginBottomLeftCircle: {
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,
}),
},
});
const Badge = React.forwardRef(function Badge(props, ref) {
const {
anchorOrigin = {
vertical: 'top',
horizontal: 'right',
},
badgeContent,
children,
classes,
className,
color: colorProp = 'default',
component: ComponentProp = 'span',
invisible: invisibleProp,
max = 99,
overlap = 'rectangle',
showZero = false,
variant: variantProp = 'standard',
...other
} = props;
let invisible = invisibleProp;
if (
invisibleProp == null &&
((badgeContent === 0 && !showZero) || (badgeContent == null && variantProp !== 'dot'))
) {
invisible = true;
}
let nextDisplayValue = '';
if (variantProp !== 'dot') {
nextDisplayValue = badgeContent > max ? `${max}+` : badgeContent;
}
// Retain the content of badge while invisible, to keep same appearance until disappearing.
// These should not be an attribute with transitions, or it will cause an undesirable animation at next appearing.
const nextNotTransitionedAttrs = {
displayValue: nextDisplayValue,
color: colorProp,
variant: variantProp,
};
const lastNotTransitionedPropsRef = useRef(nextNotTransitionedAttrs);
const { displayValue, color, variant } = invisible
? lastNotTransitionedPropsRef.current
: nextNotTransitionedAttrs;
return (
<ComponentProp className={clsx(classes.root, className)} ref={ref} {...other}>
{children}
<span
className={clsx(
classes.badge,
classes[`${anchorOrigin.horizontal}${capitalize(anchorOrigin.vertical)}}`],
classes[
`anchorOrigin${capitalize(anchorOrigin.vertical)}${capitalize(
anchorOrigin.horizontal,
)}${capitalize(overlap)}`
],
{
[classes[`color${capitalize(color)}`]]: color !== 'default',
[classes.invisible]: invisible,
[classes.dot]: variant === 'dot',
},
)}
>
{displayValue}
</span>
</ComponentProp>
);
});
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.shape({
horizontal: PropTypes.oneOf(['left', 'right']).isRequired,
vertical: PropTypes.oneOf(['bottom', 'top']).isRequired,
}),
/**
* The content rendered within the badge.
*/
badgeContent: PropTypes.node,
/**
* The badge will be added relative to this node.
*/
children: PropTypes.node,
/**
* Override or extend the styles applied to the component.
* See [CSS API](#css) below for more details.
*/
classes: PropTypes.object,
/**
* @ignore
*/
className: PropTypes.string,
/**
* The color of the component. It supports those theme colors that make sense for this component.
*/
color: PropTypes.oneOf(['default', 'error', 'primary', 'secondary']),
/**
* The component used for the root node.
* Either a string to use a DOM element or a component.
*/
component: PropTypes.elementType,
/**
* If `true`, the badge will be invisible.
*/
invisible: PropTypes.bool,
/**
* Max count to show.
*/
max: PropTypes.number,
/**
* Wrapped shape the badge should overlap.
*/
overlap: PropTypes.oneOf(['circle', 'rectangle']),
/**
* Controls whether the badge is hidden when `badgeContent` is zero.
*/
showZero: PropTypes.bool,
/**
* The variant to use.
*/
variant: PropTypes.oneOf(['dot', 'standard']),
};
export default withStyles(styles, { name: 'MuiBadge' })(Badge);