UNPKG

8.07 kBJavaScriptView Raw
1'use client';
2
3import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
4import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
5import _extends from "@babel/runtime/helpers/esm/extends";
6import * as React from 'react';
7import PropTypes from 'prop-types';
8import { isFragment } from 'react-is';
9import clsx from 'clsx';
10import chainPropTypes from '@mui/utils/chainPropTypes';
11import composeClasses from '@mui/utils/composeClasses';
12import styled from '../styles/styled';
13import { useDefaultProps } from '../DefaultPropsProvider';
14import Avatar, { avatarClasses } from '../Avatar';
15import avatarGroupClasses, { getAvatarGroupUtilityClass } from './avatarGroupClasses';
16import { jsx as _jsx } from "react/jsx-runtime";
17import { jsxs as _jsxs } from "react/jsx-runtime";
18var SPACINGS = {
19 small: -16,
20 medium: null
21};
22var useUtilityClasses = function useUtilityClasses(ownerState) {
23 var classes = ownerState.classes;
24 var slots = {
25 root: ['root'],
26 avatar: ['avatar']
27 };
28 return composeClasses(slots, getAvatarGroupUtilityClass, classes);
29};
30var AvatarGroupRoot = styled('div', {
31 name: 'MuiAvatarGroup',
32 slot: 'Root',
33 overridesResolver: function overridesResolver(props, styles) {
34 return _extends(_defineProperty({}, "& .".concat(avatarGroupClasses.avatar), styles.avatar), styles.root);
35 }
36})(function (_ref) {
37 var theme = _ref.theme,
38 ownerState = _ref.ownerState;
39 var marginValue = ownerState.spacing && SPACINGS[ownerState.spacing] !== undefined ? SPACINGS[ownerState.spacing] : -ownerState.spacing;
40 return _defineProperty(_defineProperty(_defineProperty({}, "& .".concat(avatarClasses.root), {
41 border: "2px solid ".concat((theme.vars || theme).palette.background.default),
42 boxSizing: 'content-box',
43 marginLeft: marginValue != null ? marginValue : -8,
44 '&:last-child': {
45 marginLeft: 0
46 }
47 }), "display", 'flex'), "flexDirection", 'row-reverse');
48});
49var AvatarGroup = /*#__PURE__*/React.forwardRef(function AvatarGroup(inProps, ref) {
50 var _slotProps$additional;
51 var props = useDefaultProps({
52 props: inProps,
53 name: 'MuiAvatarGroup'
54 });
55 var childrenProp = props.children,
56 className = props.className,
57 _props$component = props.component,
58 component = _props$component === void 0 ? 'div' : _props$component,
59 _props$componentsProp = props.componentsProps,
60 componentsProps = _props$componentsProp === void 0 ? {} : _props$componentsProp,
61 _props$max = props.max,
62 max = _props$max === void 0 ? 5 : _props$max,
63 renderSurplus = props.renderSurplus,
64 _props$slotProps = props.slotProps,
65 slotProps = _props$slotProps === void 0 ? {} : _props$slotProps,
66 _props$spacing = props.spacing,
67 spacing = _props$spacing === void 0 ? 'medium' : _props$spacing,
68 total = props.total,
69 _props$variant = props.variant,
70 variant = _props$variant === void 0 ? 'circular' : _props$variant,
71 other = _objectWithoutProperties(props, ["children", "className", "component", "componentsProps", "max", "renderSurplus", "slotProps", "spacing", "total", "variant"]);
72 var clampedMax = max < 2 ? 2 : max;
73 var ownerState = _extends({}, props, {
74 max: max,
75 spacing: spacing,
76 component: component,
77 variant: variant
78 });
79 var classes = useUtilityClasses(ownerState);
80 var children = React.Children.toArray(childrenProp).filter(function (child) {
81 if (process.env.NODE_ENV !== 'production') {
82 if (isFragment(child)) {
83 console.error(["MUI: The AvatarGroup component doesn't accept a Fragment as a child.", 'Consider providing an array instead.'].join('\n'));
84 }
85 }
86 return /*#__PURE__*/React.isValidElement(child);
87 });
88 var totalAvatars = total || children.length;
89 if (totalAvatars === clampedMax) {
90 clampedMax += 1;
91 }
92 clampedMax = Math.min(totalAvatars + 1, clampedMax);
93 var maxAvatars = Math.min(children.length, clampedMax - 1);
94 var extraAvatars = Math.max(totalAvatars - clampedMax, totalAvatars - maxAvatars, 0);
95 var extraAvatarsElement = renderSurplus ? renderSurplus(extraAvatars) : "+".concat(extraAvatars);
96 var additionalAvatarSlotProps = (_slotProps$additional = slotProps.additionalAvatar) != null ? _slotProps$additional : componentsProps.additionalAvatar;
97 return /*#__PURE__*/_jsxs(AvatarGroupRoot, _extends({
98 as: component,
99 ownerState: ownerState,
100 className: clsx(classes.root, className),
101 ref: ref
102 }, other, {
103 children: [extraAvatars ? /*#__PURE__*/_jsx(Avatar, _extends({
104 variant: variant
105 }, additionalAvatarSlotProps, {
106 className: clsx(classes.avatar, additionalAvatarSlotProps == null ? void 0 : additionalAvatarSlotProps.className),
107 children: extraAvatarsElement
108 })) : null, children.slice(0, maxAvatars).reverse().map(function (child) {
109 return /*#__PURE__*/React.cloneElement(child, {
110 className: clsx(child.props.className, classes.avatar),
111 variant: child.props.variant || variant
112 });
113 })]
114 }));
115});
116process.env.NODE_ENV !== "production" ? AvatarGroup.propTypes /* remove-proptypes */ = {
117 // ┌────────────────────────────── Warning ──────────────────────────────┐
118 // │ These PropTypes are generated from the TypeScript type definitions. │
119 // │ To update them, edit the d.ts file and run `pnpm proptypes`. │
120 // └─────────────────────────────────────────────────────────────────────┘
121 /**
122 * The avatars to stack.
123 */
124 children: PropTypes.node,
125 /**
126 * Override or extend the styles applied to the component.
127 */
128 classes: PropTypes.object,
129 /**
130 * @ignore
131 */
132 className: PropTypes.string,
133 /**
134 * The component used for the root node.
135 * Either a string to use a HTML element or a component.
136 */
137 component: PropTypes.elementType,
138 /**
139 * The extra props for the slot components.
140 * You can override the existing props or add new ones.
141 *
142 * This prop is an alias for the `slotProps` prop.
143 * It's recommended to use the `slotProps` prop instead, as `componentsProps` will be deprecated in the future.
144 *
145 * @default {}
146 */
147 componentsProps: PropTypes.shape({
148 additionalAvatar: PropTypes.object
149 }),
150 /**
151 * Max avatars to show before +x.
152 * @default 5
153 */
154 max: chainPropTypes(PropTypes.number, function (props) {
155 if (props.max < 2) {
156 return new Error(['MUI: The prop `max` should be equal to 2 or above.', 'A value below is clamped to 2.'].join('\n'));
157 }
158 return null;
159 }),
160 /**
161 * custom renderer of extraAvatars
162 * @param {number} surplus number of extra avatars
163 * @returns {React.ReactNode} custom element to display
164 */
165 renderSurplus: PropTypes.func,
166 /**
167 * The extra props for the slot components.
168 * You can override the existing props or add new ones.
169 *
170 * This prop is an alias for the `componentsProps` prop, which will be deprecated in the future.
171 *
172 * @default {}
173 */
174 slotProps: PropTypes.shape({
175 additionalAvatar: PropTypes.object
176 }),
177 /**
178 * Spacing between avatars.
179 * @default 'medium'
180 */
181 spacing: PropTypes.oneOfType([PropTypes.oneOf(['medium', 'small']), PropTypes.number]),
182 /**
183 * The system prop that allows defining system overrides as well as additional CSS styles.
184 */
185 sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]),
186 /**
187 * The total number of avatars. Used for calculating the number of extra avatars.
188 * @default children.length
189 */
190 total: PropTypes.number,
191 /**
192 * The variant to use.
193 * @default 'circular'
194 */
195 variant: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([PropTypes.oneOf(['circular', 'rounded', 'square']), PropTypes.string])
196} : void 0;
197export default AvatarGroup;
\No newline at end of file