File tree Expand file tree Collapse file tree 2 files changed +26
-6
lines changed
packages/mui-material/src/Avatar Expand file tree Collapse file tree 2 files changed +26
-6
lines changed Original file line number Diff line number Diff line change @@ -163,17 +163,23 @@ const Avatar = React.forwardRef(function Avatar(inProps, ref) {
163
163
164
164
let children = null ;
165
165
166
- // Use a hook instead of onError on the img element to support server-side rendering.
167
- const loaded = useLoaded ( { ...imgProps , src, srcSet } ) ;
168
- const hasImg = src || srcSet ;
169
- const hasImgNotFailing = hasImg && loaded !== 'error' ;
170
-
171
166
const ownerState = {
172
167
...props ,
173
- colorDefault : ! hasImgNotFailing ,
174
168
component,
175
169
variant,
176
170
} ;
171
+
172
+ // Use a hook instead of onError on the img element to support server-side rendering.
173
+ const loaded = useLoaded ( {
174
+ ...imgProps ,
175
+ ...( typeof slotProps . img === 'function' ? slotProps . img ( ownerState ) : slotProps . img ) ,
176
+ src,
177
+ srcSet,
178
+ } ) ;
179
+ const hasImg = src || srcSet ;
180
+ const hasImgNotFailing = hasImg && loaded !== 'error' ;
181
+
182
+ ownerState . colorDefault = ! hasImgNotFailing ;
177
183
// This issue explains why this is required: https://github.com/mui/material-ui/issues/42184
178
184
delete ownerState . ownerState ;
179
185
Original file line number Diff line number Diff line change @@ -62,6 +62,20 @@ describe('<Avatar />', () => {
62
62
fireEvent . error ( img ) ;
63
63
expect ( onError . callCount ) . to . equal ( 1 ) ;
64
64
} ) ;
65
+
66
+ it ( 'should pass slots.img to `useLoaded` hook' , ( ) => {
67
+ const originalImage = global . Image ;
68
+ const image = { } ;
69
+ global . Image = function Image ( ) {
70
+ return image ;
71
+ } ;
72
+
73
+ render ( < Avatar src = "/fake.png" slotProps = { { img : { crossOrigin : 'anonymous' } } } /> ) ;
74
+
75
+ expect ( image . crossOrigin ) . to . equal ( 'anonymous' ) ;
76
+
77
+ global . Image = originalImage ;
78
+ } ) ;
65
79
} ) ;
66
80
67
81
describe ( 'image avatar with unrendered children' , ( ) => {
You can’t perform that action at this time.
0 commit comments