-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
/
Copy pathAvatarGroup.js
263 lines (241 loc) · 7.25 KB
/
AvatarGroup.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
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import { isFragment } from 'react-is';
import clsx from 'clsx';
import chainPropTypes from '@mui/utils/chainPropTypes';
import composeClasses from '@mui/utils/composeClasses';
import { styled } from '../zero-styled';
import memoTheme from '../utils/memoTheme';
import { useDefaultProps } from '../DefaultPropsProvider';
import Avatar, { avatarClasses } from '../Avatar';
import avatarGroupClasses, { getAvatarGroupUtilityClass } from './avatarGroupClasses';
import useSlot from '../utils/useSlot';
const SPACINGS = {
small: -16,
medium: -8,
};
const useUtilityClasses = (ownerState) => {
const { classes } = ownerState;
const slots = {
root: ['root'],
avatar: ['avatar'],
};
return composeClasses(slots, getAvatarGroupUtilityClass, classes);
};
const AvatarGroupRoot = styled('div', {
name: 'MuiAvatarGroup',
slot: 'Root',
overridesResolver: (props, styles) => {
return [{ [`& .${avatarGroupClasses.avatar}`]: styles.avatar }, styles.root];
},
})(
memoTheme(({ theme }) => ({
display: 'flex',
flexDirection: 'row-reverse',
[`& .${avatarClasses.root}`]: {
border: `2px solid ${(theme.vars || theme).palette.background.default}`,
boxSizing: 'content-box',
marginLeft: 'var(--AvatarGroup-spacing, -8px)',
'&:last-child': {
marginLeft: 0,
},
},
})),
);
const AvatarGroup = React.forwardRef(function AvatarGroup(inProps, ref) {
const props = useDefaultProps({
props: inProps,
name: 'MuiAvatarGroup',
});
const {
children: childrenProp,
className,
component = 'div',
componentsProps,
max = 5,
renderSurplus,
slotProps = {},
slots = {},
spacing = 'medium',
total,
variant = 'circular',
...other
} = props;
let clampedMax = max < 2 ? 2 : max;
const ownerState = {
...props,
max,
spacing,
component,
variant,
};
const classes = useUtilityClasses(ownerState);
const children = React.Children.toArray(childrenProp).filter((child) => {
if (process.env.NODE_ENV !== 'production') {
if (isFragment(child)) {
console.error(
[
"MUI: The AvatarGroup component doesn't accept a Fragment as a child.",
'Consider providing an array instead.',
].join('\n'),
);
}
}
return React.isValidElement(child);
});
const totalAvatars = total || children.length;
if (totalAvatars === clampedMax) {
clampedMax += 1;
}
clampedMax = Math.min(totalAvatars + 1, clampedMax);
const maxAvatars = Math.min(children.length, clampedMax - 1);
const extraAvatars = Math.max(totalAvatars - clampedMax, totalAvatars - maxAvatars, 0);
const extraAvatarsElement = renderSurplus ? renderSurplus(extraAvatars) : `+${extraAvatars}`;
const marginValue =
ownerState.spacing && SPACINGS[ownerState.spacing] !== undefined
? SPACINGS[ownerState.spacing]
: -ownerState.spacing || -8;
const externalForwardedProps = {
slots,
slotProps: {
surplus: slotProps.additionalAvatar ?? componentsProps?.additionalAvatar,
...componentsProps,
...slotProps,
},
};
const [SurplusSlot, surplusProps] = useSlot('surplus', {
elementType: Avatar,
externalForwardedProps,
className: classes.avatar,
ownerState,
additionalProps: {
variant,
},
});
return (
<AvatarGroupRoot
as={component}
ownerState={ownerState}
className={clsx(classes.root, className)}
ref={ref}
{...other}
style={{
'--AvatarGroup-spacing': marginValue ? `${marginValue}px` : undefined,
...other.style,
}}
>
{extraAvatars ? <SurplusSlot {...surplusProps}>{extraAvatarsElement}</SurplusSlot> : null}
{children
.slice(0, maxAvatars)
.reverse()
.map((child) => {
return React.cloneElement(child, {
className: clsx(child.props.className, classes.avatar),
variant: child.props.variant || variant,
});
})}
</AvatarGroupRoot>
);
});
AvatarGroup.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
// │ To update them, edit the d.ts file and run `pnpm proptypes`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* The avatars to stack.
*/
children: PropTypes.node,
/**
* Override or extend the styles applied to the component.
*/
classes: PropTypes.object,
/**
* @ignore
*/
className: PropTypes.string,
/**
* The component used for the root node.
* Either a string to use a HTML element or a component.
*/
component: PropTypes.elementType,
/**
* The extra props for the slot components.
* You can override the existing props or add new ones.
*
* This prop is an alias for the `slotProps` prop.
*
* @deprecated use the `slotProps` prop instead. This prop will be removed in v7. See [Migrating from deprecated APIs](https://mui.com/material-ui/migration/migrating-from-deprecated-apis/) for more details.
*/
componentsProps: PropTypes.shape({
additionalAvatar: PropTypes.object,
}),
/**
* Max avatars to show before +x.
* @default 5
*/
max: chainPropTypes(PropTypes.number, (props) => {
if (props.max < 2) {
return new Error(
[
'MUI: The prop `max` should be equal to 2 or above.',
'A value below is clamped to 2.',
].join('\n'),
);
}
return null;
}),
/**
* custom renderer of extraAvatars
* @param {number} surplus number of extra avatars
* @returns {React.ReactNode} custom element to display
*/
renderSurplus: PropTypes.func,
/**
* The props used for each slot inside.
* @default {}
*/
slotProps: PropTypes.shape({
additionalAvatar: PropTypes.object,
surplus: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
}),
/**
* The components used for each slot inside.
* @default {}
*/
slots: PropTypes.shape({
surplus: PropTypes.elementType,
}),
/**
* Spacing between avatars.
* @default 'medium'
*/
spacing: PropTypes.oneOfType([PropTypes.oneOf(['medium', 'small']), PropTypes.number]),
/**
* @ignore
*/
style: PropTypes.object,
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])),
PropTypes.func,
PropTypes.object,
]),
/**
* The total number of avatars. Used for calculating the number of extra avatars.
* @default children.length
*/
total: PropTypes.number,
/**
* The variant to use.
* @default 'circular'
*/
variant: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['circular', 'rounded', 'square']),
PropTypes.string,
]),
};
export default AvatarGroup;