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] Fix slotProps.transition types #45214

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

Conversation

siriwatknp
Copy link
Member

@siriwatknp siriwatknp commented Feb 5, 2025

closes #45210
closes #40650

Root cause

The existing slotProps below for transition is not correct:

transition: SlotProps<
  React.ElementType<TransitionProps>,
  DialogTransitionSlotPropsOverrides,
  DialogOwnerState
>;

It's because TransitionProps extends React.HTMLAttributes<HTMLElement> that causes the final types to be a union of TransitionProps and HTML props. That's why transition['onExited'] does not exist, see this TypeScript playground

This PR fixes the issue by moving TransitionProps to the second parameter (the TOverrides).

All slotProps.transition should follow:

   transition: SlotComponentProps<
      React.ElementType,
      TransitionProps & DialogTransitionSlotPropsOverrides,
      DialogOwnerState
    >;

The SlotComponentProps must be used instead of SlotProps because transition slot does not support component and sx prop.


@siriwatknp siriwatknp changed the title Fix/transition slot props [material-ui] Fix slotProps.transition types Feb 5, 2025
@siriwatknp siriwatknp added typescript package: material-ui Specific to @mui/material labels Feb 5, 2025
@mui-bot
Copy link

mui-bot commented Feb 5, 2025

Netlify deploy preview

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

Bundle size report

No bundle size changes (Toolpad)
No bundle size changes

Generated by 🚫 dangerJS against ce677e0

Copy link
Member

@aarongarciah aarongarciah left a comment

Choose a reason for hiding this comment

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

Looks good. Left a question. I'll let @DiegoAndai do another pass.

Copy link
Member

@DiegoAndai DiegoAndai left a comment

Choose a reason for hiding this comment

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

This also closes #40650 👍🏼. @siriwatknp I assigned you to it and added it to the description. Please review it.

This change makes the children optional, but from what I remember, some components will throw if no children are provided. May I ask you to make sure that none of these components will crash if no children are provided? Otherwise, we have to either:

  • Make the children required
  • Modify the components so they can handle this case

@siriwatknp
Copy link
Member Author

siriwatknp commented Feb 6, 2025

For #40650, I added a comment here.

This change makes the children optional

Do you mean the slotProps.transition? This is expected because the usage of <TransitionSlot {…transitionSlotProps}> always have children in the implementation.

I am not sure where this PR changes the behavior, can you point me to the code?

Comment on lines -21 to -23
transition: React.JSXElementConstructor<
TransitionProps & { children?: React.ReactElement<unknown, any> }
>;
Copy link
Member Author

Choose a reason for hiding this comment

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

any slot should be React.ElementType, otherwise user cannot provide their own props interface.

See this TS playground

@DiegoAndai
Copy link
Member

For #40650, I added a #40650 (comment).

I agree with your comment 👌🏼

This is expected because the usage of <TransitionSlot {…transitionSlotProps}> always have children in the implementation.

You're right

One final question: In the test files, we're using

const transitionProps =
    typeof slotProps?.transition === 'function'
        ? slotProps.transition(ownerState)
        : slotProps?.transition;

Should we use the mergeSlotProps util instead? Or maybe add an additional test that uses it?

@siriwatknp
Copy link
Member Author

siriwatknp commented Feb 7, 2025

Should we use the mergeSlotProps util instead? Or maybe add an additional test that uses it?

I don't think mergeSlotProps should be touched in this PR as it's not directly related. This PR tests that the component's slotProps.transition has the expected types.

@DiegoAndai
Copy link
Member

@siriwatknp I didn't mean modifying mergeSlotProps.

What I propose is to use mergeSlotProps in the .spec tests (for example, this one) instead of manually checking slotProps?.transition === 'function'. I think this is better because that's what we have in our docs, so that's the way I expect users will be extending the slotProps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package: material-ui Specific to @mui/material typescript
Projects
None yet
4 participants