1
1
import * as React from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
+ import clsx from 'clsx' ;
3
4
import { unstable_composeClasses as composeClasses } from '@mui/base' ;
4
- import { useSlotProps } from '@mui/base/utils' ;
5
5
import { OverridableComponent } from '@mui/types' ;
6
6
import { unstable_capitalize as capitalize } from '@mui/utils' ;
7
7
import { useThemeProps } from '../styles' ;
8
+ import useSlot from '../utils/useSlot' ;
8
9
import styled from '../styles/styled' ;
9
10
import Person from '../internal/svg-icons/Person' ;
10
11
import { getAvatarUtilityClass } from './avatarClasses' ;
@@ -149,6 +150,7 @@ const Avatar = React.forwardRef(function Avatar(inProps, ref) {
149
150
color : colorProp = 'neutral' ,
150
151
component = 'div' ,
151
152
componentsProps = { } ,
153
+ className,
152
154
size : sizeProp = 'md' ,
153
155
variant : variantProp = 'soft' ,
154
156
imgProps,
@@ -186,50 +188,47 @@ const Avatar = React.forwardRef(function Avatar(inProps, ref) {
186
188
const hasImgNotFailing = hasImg && loaded !== 'error' ;
187
189
188
190
const classes = useUtilityClasses ( ownerState ) ;
191
+ const externalForwardedProps = { ...other , component, componentsProps } ;
189
192
190
- const imageProps = useSlotProps ( {
191
- elementType : AvatarImg ,
192
- externalSlotProps : componentsProps . img ,
193
+ const [ SlotRoot , rootProps ] = useSlot ( 'root' , {
194
+ ref,
195
+ className : clsx ( classes . root , className ) ,
196
+ elementType : AvatarRoot ,
197
+ externalForwardedProps,
193
198
ownerState,
199
+ } ) ;
200
+
201
+ const [ SlotImg , imageProps ] = useSlot ( 'img' , {
194
202
additionalProps : {
195
203
alt,
196
204
src,
197
205
srcSet,
198
206
...imgProps ,
199
207
} ,
200
208
className : classes . img ,
209
+ elementType : AvatarImg ,
210
+ externalForwardedProps,
211
+ ownerState,
201
212
} ) ;
202
213
203
- const fallbackProps = useSlotProps ( {
214
+ const [ SlotFallback , fallbackProps ] = useSlot ( 'fallback' , {
215
+ className : classes . fallback ,
204
216
elementType : AvatarFallback ,
205
- externalSlotProps : componentsProps . fallback ,
217
+ externalForwardedProps ,
206
218
ownerState,
207
- className : classes . fallback ,
208
219
} ) ;
209
220
210
221
if ( hasImgNotFailing ) {
211
- children = < AvatarImg { ...imageProps } /> ;
222
+ children = < SlotImg { ...imageProps } /> ;
212
223
} else if ( childrenProp != null ) {
213
224
children = childrenProp ;
214
225
} else if ( hasImg && alt ) {
215
226
children = alt [ 0 ] ;
216
227
} else {
217
- children = < AvatarFallback { ...fallbackProps } /> ;
228
+ children = < SlotFallback { ...fallbackProps } /> ;
218
229
}
219
230
220
- const rootProps = useSlotProps ( {
221
- elementType : AvatarRoot ,
222
- externalSlotProps : componentsProps . root ,
223
- ownerState,
224
- externalForwardedProps : other ,
225
- additionalProps : {
226
- ref,
227
- as : component ,
228
- } ,
229
- className : classes . root ,
230
- } ) ;
231
-
232
- return < AvatarRoot { ...rootProps } > { children } </ AvatarRoot > ;
231
+ return < SlotRoot { ...rootProps } > { children } </ SlotRoot > ;
233
232
} ) as OverridableComponent < AvatarTypeMap > ;
234
233
235
234
Avatar . propTypes /* remove-proptypes */ = {
@@ -247,6 +246,10 @@ Avatar.propTypes /* remove-proptypes */ = {
247
246
* This can be an element, or just a string.
248
247
*/
249
248
children : PropTypes . node ,
249
+ /**
250
+ * @ignore
251
+ */
252
+ className : PropTypes . string ,
250
253
/**
251
254
* The color of the component. It supports those theme colors that make sense for this component.
252
255
* @default 'neutral'
@@ -260,6 +263,14 @@ Avatar.propTypes /* remove-proptypes */ = {
260
263
* Either a string to use a HTML element or a component.
261
264
*/
262
265
component : PropTypes . elementType ,
266
+ /**
267
+ * Replace the default slots.
268
+ */
269
+ components : PropTypes . shape ( {
270
+ fallback : PropTypes . elementType ,
271
+ img : PropTypes . elementType ,
272
+ root : PropTypes . elementType ,
273
+ } ) ,
263
274
/**
264
275
* The props used for each slot inside the component.
265
276
* @default {}
0 commit comments