Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[material-ui][Avatar] Fix slotProps.img not spread to hook #44536

Merged
merged 3 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions packages/mui-material/src/Avatar/Avatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,17 +163,23 @@ const Avatar = React.forwardRef(function Avatar(inProps, ref) {

let children = null;

// Use a hook instead of onError on the img element to support server-side rendering.
const loaded = useLoaded({ ...imgProps, src, srcSet });
const hasImg = src || srcSet;
const hasImgNotFailing = hasImg && loaded !== 'error';

const ownerState = {
...props,
colorDefault: !hasImgNotFailing,
component,
variant,
};

// Use a hook instead of onError on the img element to support server-side rendering.
const loaded = useLoaded({
...imgProps,
...(typeof slotProps.img === 'function' ? slotProps.img(ownerState) : slotProps.img),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Off topic:

I think this common pattern (...(typeof slotProps.x === 'function' ? slotProps.x(ownerState) : slotProps.x)) could have a util in the future, it would also be useful for users: #44350 (comment)

What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be. It is already handled by the useSlot but I think this is an edge case.

src,
srcSet,
});
const hasImg = src || srcSet;
const hasImgNotFailing = hasImg && loaded !== 'error';

ownerState.colorDefault = !hasImgNotFailing;
// This issue explains why this is required: https://github.com/mui/material-ui/issues/42184
delete ownerState.ownerState;

Expand Down
14 changes: 14 additions & 0 deletions packages/mui-material/src/Avatar/Avatar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ describe('<Avatar />', () => {
fireEvent.error(img);
expect(onError.callCount).to.equal(1);
});

it('should pass slots.img to `useLoaded` hook', () => {
const originalImage = global.Image;
const image = {};
global.Image = function Image() {
return image;
};

render(<Avatar src="/fake.png" slotProps={{ img: { crossOrigin: 'anonymous' } }} />);

expect(image.crossOrigin).to.equal('anonymous');

global.Image = originalImage;
});
});

describe('image avatar with unrendered children', () => {
Expand Down