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

[Avatar] Fix img slot types and add missing slots #45483

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

siriwatknp
Copy link
Member

@siriwatknp siriwatknp commented Mar 5, 2025

closes #45443

This PR follows the option (2) solution explained in #45443 (comment)

Root cause

The types of slots.img is too strict, not able to provide a custom component, .e.g. Next.js Image.

Solution

Change the slot type to React.ElementType which accept any React component or string. Please see the added spec.

I also use this opportunity to add missing slots (root and fallback) in this PR.

@siriwatknp siriwatknp added component: avatar This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material labels Mar 5, 2025
{
"name": "img",
"description": "The component that renders the transition.\n[Follow this guide](https://mui.com/material-ui/transitions/#transitioncomponent-prop) to learn more about the requirements for this component.",
Copy link
Member Author

Choose a reason for hiding this comment

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

This is wrong.

@mui-bot
Copy link

mui-bot commented Mar 5, 2025

fallback: {
expectedClassName: classes.fallback,
},
},
Copy link
Member Author

Choose a reason for hiding this comment

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

Cannot test the img slot together with fallback here because there is a logic within the component that dynamically render either one of them.

Copy link
Member

@mnajdova mnajdova left a comment

Choose a reason for hiding this comment

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

Can we list what other options were tried, maybe even on the original issue to make sure we don't come back to this again. Some I can think of:

  • dynamically inferring types (pros and cons, e.g. TS check performance)
  • loosening the types for the slot props with some defaults
    etc.

height: 40,
blurDataURL: 'data:image/png;base64',
} satisfies ImageProps,
} as AvatarProps['slotProps']
Copy link

Choose a reason for hiding this comment

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

unnecessary assertion

Copy link
Member Author

Choose a reason for hiding this comment

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

In this case, it's still required because the slotProps.img has a fixed structure that's different from ImageProps.

If you exclude the line, TS throws an error.

@siriwatknp
Copy link
Member Author

siriwatknp commented Mar 10, 2025

Can we list what other options were tried, maybe even on the original issue to make sure we don't come back to this again. Some I can think of:

Added here: #45443 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: avatar This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support Next.js Image in Avatar slots API without TypeScript errors
4 participants