UNPKG

9.03 kBJavaScriptView Raw
1"use strict";
2'use client';
3
4var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
5var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
6Object.defineProperty(exports, "__esModule", {
7 value: true
8});
9exports.default = void 0;
10var React = _interopRequireWildcard(require("react"));
11var _propTypes = _interopRequireDefault(require("prop-types"));
12var _clsx = _interopRequireDefault(require("clsx"));
13var _composeClasses = _interopRequireDefault(require("@mui/utils/composeClasses"));
14var _zeroStyled = require("../zero-styled");
15var _memoTheme = _interopRequireDefault(require("../utils/memoTheme"));
16var _DefaultPropsProvider = require("../DefaultPropsProvider");
17var _Person = _interopRequireDefault(require("../internal/svg-icons/Person"));
18var _avatarClasses = require("./avatarClasses");
19var _useSlot = _interopRequireDefault(require("../utils/useSlot"));
20var _jsxRuntime = require("react/jsx-runtime");
21const useUtilityClasses = ownerState => {
22 const {
23 classes,
24 variant,
25 colorDefault
26 } = ownerState;
27 const slots = {
28 root: ['root', variant, colorDefault && 'colorDefault'],
29 img: ['img'],
30 fallback: ['fallback']
31 };
32 return (0, _composeClasses.default)(slots, _avatarClasses.getAvatarUtilityClass, classes);
33};
34const AvatarRoot = (0, _zeroStyled.styled)('div', {
35 name: 'MuiAvatar',
36 slot: 'Root',
37 overridesResolver: (props, styles) => {
38 const {
39 ownerState
40 } = props;
41 return [styles.root, styles[ownerState.variant], ownerState.colorDefault && styles.colorDefault];
42 }
43})((0, _memoTheme.default)(({
44 theme
45}) => ({
46 position: 'relative',
47 display: 'flex',
48 alignItems: 'center',
49 justifyContent: 'center',
50 flexShrink: 0,
51 width: 40,
52 height: 40,
53 fontFamily: theme.typography.fontFamily,
54 fontSize: theme.typography.pxToRem(20),
55 lineHeight: 1,
56 borderRadius: '50%',
57 overflow: 'hidden',
58 userSelect: 'none',
59 variants: [{
60 props: {
61 variant: 'rounded'
62 },
63 style: {
64 borderRadius: (theme.vars || theme).shape.borderRadius
65 }
66 }, {
67 props: {
68 variant: 'square'
69 },
70 style: {
71 borderRadius: 0
72 }
73 }, {
74 props: {
75 colorDefault: true
76 },
77 style: {
78 color: (theme.vars || theme).palette.background.default,
79 ...(theme.vars ? {
80 backgroundColor: theme.vars.palette.Avatar.defaultBg
81 } : {
82 backgroundColor: theme.palette.grey[400],
83 ...theme.applyStyles('dark', {
84 backgroundColor: theme.palette.grey[600]
85 })
86 })
87 }
88 }]
89})));
90const AvatarImg = (0, _zeroStyled.styled)('img', {
91 name: 'MuiAvatar',
92 slot: 'Img',
93 overridesResolver: (props, styles) => styles.img
94})({
95 width: '100%',
96 height: '100%',
97 textAlign: 'center',
98 // Handle non-square image.
99 objectFit: 'cover',
100 // Hide alt text.
101 color: 'transparent',
102 // Hide the image broken icon, only works on Chrome.
103 textIndent: 10000
104});
105const AvatarFallback = (0, _zeroStyled.styled)(_Person.default, {
106 name: 'MuiAvatar',
107 slot: 'Fallback',
108 overridesResolver: (props, styles) => styles.fallback
109})({
110 width: '75%',
111 height: '75%'
112});
113function useLoaded({
114 crossOrigin,
115 referrerPolicy,
116 src,
117 srcSet
118}) {
119 const [loaded, setLoaded] = React.useState(false);
120 React.useEffect(() => {
121 if (!src && !srcSet) {
122 return undefined;
123 }
124 setLoaded(false);
125 let active = true;
126 const image = new Image();
127 image.onload = () => {
128 if (!active) {
129 return;
130 }
131 setLoaded('loaded');
132 };
133 image.onerror = () => {
134 if (!active) {
135 return;
136 }
137 setLoaded('error');
138 };
139 image.crossOrigin = crossOrigin;
140 image.referrerPolicy = referrerPolicy;
141 image.src = src;
142 if (srcSet) {
143 image.srcset = srcSet;
144 }
145 return () => {
146 active = false;
147 };
148 }, [crossOrigin, referrerPolicy, src, srcSet]);
149 return loaded;
150}
151const Avatar = /*#__PURE__*/React.forwardRef(function Avatar(inProps, ref) {
152 const props = (0, _DefaultPropsProvider.useDefaultProps)({
153 props: inProps,
154 name: 'MuiAvatar'
155 });
156 const {
157 alt,
158 children: childrenProp,
159 className,
160 component = 'div',
161 slots = {},
162 slotProps = {},
163 imgProps,
164 sizes,
165 src,
166 srcSet,
167 variant = 'circular',
168 ...other
169 } = props;
170 let children = null;
171
172 // Use a hook instead of onError on the img element to support server-side rendering.
173 const loaded = useLoaded({
174 ...imgProps,
175 src,
176 srcSet
177 });
178 const hasImg = src || srcSet;
179 const hasImgNotFailing = hasImg && loaded !== 'error';
180 const ownerState = {
181 ...props,
182 colorDefault: !hasImgNotFailing,
183 component,
184 variant
185 };
186 // This issue explains why this is required: https://github.com/mui/material-ui/issues/42184
187 delete ownerState.ownerState;
188 const classes = useUtilityClasses(ownerState);
189 const [ImgSlot, imgSlotProps] = (0, _useSlot.default)('img', {
190 className: classes.img,
191 elementType: AvatarImg,
192 externalForwardedProps: {
193 slots,
194 slotProps: {
195 img: {
196 ...imgProps,
197 ...slotProps.img
198 }
199 }
200 },
201 additionalProps: {
202 alt,
203 src,
204 srcSet,
205 sizes
206 },
207 ownerState
208 });
209 if (hasImgNotFailing) {
210 children = /*#__PURE__*/(0, _jsxRuntime.jsx)(ImgSlot, {
211 ...imgSlotProps
212 });
213 // We only render valid children, non valid children are rendered with a fallback
214 // We consider that invalid children are all falsy values, except 0, which is valid.
215 } else if (!!childrenProp || childrenProp === 0) {
216 children = childrenProp;
217 } else if (hasImg && alt) {
218 children = alt[0];
219 } else {
220 children = /*#__PURE__*/(0, _jsxRuntime.jsx)(AvatarFallback, {
221 ownerState: ownerState,
222 className: classes.fallback
223 });
224 }
225 return /*#__PURE__*/(0, _jsxRuntime.jsx)(AvatarRoot, {
226 as: component,
227 className: (0, _clsx.default)(classes.root, className),
228 ref: ref,
229 ...other,
230 ownerState: ownerState,
231 children: children
232 });
233});
234process.env.NODE_ENV !== "production" ? Avatar.propTypes /* remove-proptypes */ = {
235 // ┌────────────────────────────── Warning ──────────────────────────────┐
236 // │ These PropTypes are generated from the TypeScript type definitions. │
237 // │ To update them, edit the d.ts file and run `pnpm proptypes`. │
238 // └─────────────────────────────────────────────────────────────────────┘
239 /**
240 * Used in combination with `src` or `srcSet` to
241 * provide an alt attribute for the rendered `img` element.
242 */
243 alt: _propTypes.default.string,
244 /**
245 * Used to render icon or text elements inside the Avatar if `src` is not set.
246 * This can be an element, or just a string.
247 */
248 children: _propTypes.default.node,
249 /**
250 * Override or extend the styles applied to the component.
251 */
252 classes: _propTypes.default.object,
253 /**
254 * @ignore
255 */
256 className: _propTypes.default.string,
257 /**
258 * The component used for the root node.
259 * Either a string to use a HTML element or a component.
260 */
261 component: _propTypes.default.elementType,
262 /**
263 * [Attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attributes) applied to the `img` element if the component is used to display an image.
264 * It can be used to listen for the loading error event.
265 */
266 imgProps: _propTypes.default.object,
267 /**
268 * The `sizes` attribute for the `img` element.
269 */
270 sizes: _propTypes.default.string,
271 /**
272 * The props used for each slot inside.
273 * @default {}
274 */
275 slotProps: _propTypes.default.shape({
276 img: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.object])
277 }),
278 /**
279 * The components used for each slot inside.
280 * @default {}
281 */
282 slots: _propTypes.default.shape({
283 img: _propTypes.default.elementType
284 }),
285 /**
286 * The `src` attribute for the `img` element.
287 */
288 src: _propTypes.default.string,
289 /**
290 * The `srcSet` attribute for the `img` element.
291 * Use this attribute for responsive image display.
292 */
293 srcSet: _propTypes.default.string,
294 /**
295 * The system prop that allows defining system overrides as well as additional CSS styles.
296 */
297 sx: _propTypes.default.oneOfType([_propTypes.default.arrayOf(_propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.object, _propTypes.default.bool])), _propTypes.default.func, _propTypes.default.object]),
298 /**
299 * The shape of the avatar.
300 * @default 'circular'
301 */
302 variant: _propTypes.default /* @typescript-to-proptypes-ignore */.oneOfType([_propTypes.default.oneOf(['circular', 'rounded', 'square']), _propTypes.default.string])
303} : void 0;
304var _default = exports.default = Avatar;
\No newline at end of file