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] Prevent ownerState propagation for transition slots #44401

Conversation

ZeeshanTamboli
Copy link
Member

@ZeeshanTamboli ZeeshanTamboli commented Nov 13, 2024

Found this while investigating an issue—check the console error about ownerState.

Before: Sandbox
After: Sandbox

You can also see it in the local Accordion docs at http://localhost:3000/material-ui/react-accordion/.

The workaround in #40418 was removed in favor of #41187, but #41187 doesn’t handle custom transition slots like Fade and Zoom.

This PR introduces a new shouldAppendOwnerState option in useSlot, preventing ownerState from being appended unnecessarily, instead of appending and then removing it.

@ZeeshanTamboli ZeeshanTamboli added the package: material-ui Specific to @mui/material label Nov 13, 2024
@mui-bot
Copy link

mui-bot commented Nov 13, 2024

Netlify deploy preview

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

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against ea60c29

@ZeeshanTamboli ZeeshanTamboli changed the title [material-ui] Remove ownerState propagation in accordion transition slot [material-ui] Remove ownerState propagation for transition slots Nov 13, 2024
@ZeeshanTamboli ZeeshanTamboli marked this pull request as ready for review November 13, 2024 12:07
@ZeeshanTamboli ZeeshanTamboli added bug 🐛 Something doesn't work regression A bug, but worse labels Nov 13, 2024
@ZeeshanTamboli ZeeshanTamboli changed the title [material-ui] Remove ownerState propagation for transition slots [material-ui] Prevent ownerState propagation for transition slots Nov 13, 2024
Co-authored-by: Olivier Tassinari <[email protected]>
Signed-off-by: Zeeshan Tamboli <[email protected]>
@DiegoAndai
Copy link
Member

Hey @ZeeshanTamboli, thanks for working on this.

We do want the ownerState to be available for the transition slots. That way, users can consume it when building their custom transitions. The responsibility of filtering ownerState would be on the developer.

The fix here should be to filter out the ownerState inside the Fade and Zoom components. Because those are our transition components, the responsibility of not forwarding ownerState incorrectly should be handled on each one. Does this make sense?

@ZeeshanTamboli
Copy link
Member Author

ZeeshanTamboli commented Nov 19, 2024

Hey @ZeeshanTamboli, thanks for working on this.

We do want the ownerState to be available for the transition slots. That way, users can consume it when building their custom transitions. The responsibility of filtering ownerState would be on the developer.

The fix here should be to filter out the ownerState inside the Fade and Zoom components. Because those are our transition components, the responsibility of not forwarding ownerState incorrectly should be handled on each one. Does this make sense?

Oh, I thought the ownerState passed to the slotProps callback through resolveComponentProps in useSlot was sufficient. For example:

slotProps={{ transition: ({ expanded }) => (expanded ? 300 : null) }}

For non-transition slots, I assumed ownerState was appended for use in the styled API for styling.

Is that not the case?

The responsibility of filtering ownerState would be on the developer.

Do we document this somewhere?

@DiegoAndai
Copy link
Member

DiegoAndai commented Nov 19, 2024

Oh, I thought the ownerState passed to the slotProps callback through resolveComponentProps in useSlot was sufficient.

For non-transition slots, I assumed ownerState was appended for use in the styled API for styling.
Is that not the case?

Both of these are useful, but IMO don't cover all use cases. Making the parent's ownerState available as a prop provides more flexibility. There's also an argument for not providing the ownerState but only certain properties of it. This is another thing we need to decide regarding the slot pattern cc: @siriwatknp

Do we document this somewhere?

No. Documentation about slots could and should be much better. We're working on improving that, but first, we want to implement the missing slots throughout the components.

@ZeeshanTamboli
Copy link
Member Author

@DiegoAndai What's your take on the recent pushed change to destructure ownerState?

@ZeeshanTamboli
Copy link
Member Author

@DiegoAndai What's your take on the recent pushed change to destructure ownerState?

@DiegoAndai ^

@ZeeshanTamboli
Copy link
Member Author

@DiegoAndai Just a ping to review this further in case you missed the notification.

@ZeeshanTamboli
Copy link
Member Author

ZeeshanTamboli commented Dec 14, 2024

@DiegoAndai

  • Can we apply it to the Collapse transition as well?

The Collapse transition uses a styled JSX component instead of React.cloneElement, so ownerState is not forwarded to the DOM since it is already omitted in the styled API. Additionally, the incoming ownerState is overridden by Collapse's ownerState here.

  • May I ask you to add tests for this?

Added in Accordion test file because this is the component whose transition child is directly an HTML element and ownerState was getting propagated.

@DiegoAndai
Copy link
Member

DiegoAndai commented Dec 16, 2024

Additionally, the incoming ownerState is overridden by Collapse's ownerState here.

Yes, that was the previous solution to this same problem, but I would prefer that it had this PR's implementation, I think it's easier to understand and less fragile. Also, having all transition components be as consistently implemented as possible would be good.

@ZeeshanTamboli
Copy link
Member Author

Yes, that was the previous solution to this same problem, but I would prefer that it had this PR's implementation, I think it's easier to understand and less fragile. Also, having all transition components be as consistently implemented as possible would be good.

@DiegoAndai Made the change.

@DiegoAndai
Copy link
Member

Thanks @ZeeshanTamboli, looks good.

I have one final request.

May I ask you to add a regression test for the issue in #40653. We should've added it in #41187.

The regression test can be extracted for this example:

The Switch input and text can be removed for the regression test, they're there only for demonstration purposes.

Thanks in advance!

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.

Thanks @ZeeshanTamboli!

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.

Just saw the broken test 😅

packages/mui-material/src/Collapse/Collapse.test.js Outdated Show resolved Hide resolved
@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Dec 18, 2024
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Dec 18, 2024
@DiegoAndai DiegoAndai force-pushed the remove-ownerState-propogation-accordion-transition-slot branch from 82c4ed8 to bc48d22 Compare December 18, 2024 15:14
@DiegoAndai DiegoAndai force-pushed the remove-ownerState-propogation-accordion-transition-slot branch from bc48d22 to 0fc6f3d Compare December 18, 2024 15:15
@ZeeshanTamboli
Copy link
Member Author

@DiegoAndai All passing now

@ZeeshanTamboli ZeeshanTamboli merged commit 3f851ce into mui:master Dec 19, 2024
19 checks passed
@ZeeshanTamboli ZeeshanTamboli deleted the remove-ownerState-propogation-accordion-transition-slot branch December 19, 2024 06:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work package: material-ui Specific to @mui/material regression A bug, but worse
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants