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

Conversation

siriwatknp
Copy link
Member

@siriwatknp siriwatknp commented Nov 25, 2024

closes #44501

before: https://codesandbox.io/p/sandbox/avatar-imgprops-demo-7jdnp5
after: https://codesandbox.io/p/sandbox/material-ui-cra-ts-forked-l9rtth

The problem is slotProps.img is not spread to the useLoaded hook inside Avatar, so the crossOrigin does not take into account.

Test added.

@siriwatknp siriwatknp added the bug 🐛 Something doesn't work label Nov 25, 2024
@mui-bot
Copy link

mui-bot commented Nov 25, 2024

Netlify deploy preview

https://deploy-preview-44536--material-ui.netlify.app/

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against d72596d

@DiegoAndai
Copy link
Member

It's not possible to test without mocking the useLoaded hook.

Can we mock it to add a test?

@siriwatknp
Copy link
Member Author

It's not possible to test without mocking the useLoaded hook.

Can we mock it to add a test?

I decided to use the global.Image instead of mocking the hook. It's cleaner.

// 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.

@siriwatknp siriwatknp merged commit b89845a into mui:master Nov 28, 2024
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Avatar component has imgProps marked as deprecated but the suggested alternative does not work
3 participants