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] Add mergeSlotProps for extending components #44809

Merged
merged 11 commits into from
Jan 6, 2025

Conversation

siriwatknp
Copy link
Member

@siriwatknp siriwatknp commented Dec 19, 2024

From #44350 (comment).

Docs: https://deploy-preview-44809--material-ui.netlify.app/material-ui/guides/composition/#forwarding-slot-props

Without the function, a lot of boilerplate code is required. I believe that users will create this kind of utility in their codebase because it's quite a common use case to extend Material UI components, so I think we should provide this utility called mergeSlotProps.

The mergeSlotProps resolves function type and then merge the props. Only the className that's concatenated to preserve the same behavior of slotProps, the rest will override the default ones. The usage is added to the docs.

mergeSlotProps(
  (ownerState) => ({ className: 'outside' }),
  { className: 'inside' },
)

// className = 'inside outside'

@siriwatknp siriwatknp added the package: material-ui Specific to @mui/material label Dec 19, 2024
@siriwatknp siriwatknp marked this pull request as draft December 19, 2024 01:57
@mui-bot
Copy link

mui-bot commented Dec 19, 2024

Netlify deploy preview

@material-ui/core: parsed: +0.07% , gzip: +0.07%

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against 6f669a8

@siriwatknp siriwatknp marked this pull request as ready for review December 20, 2024 02:54
Co-authored-by: Sycamore <[email protected]>
Signed-off-by: Siriwat K <[email protected]>
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 for working on this @siriwatknp. I think it will be very useful now that we're standardizing the slots pattern.

packages/mui-material/src/utils/mergeSlotProps.ts Outdated Show resolved Hide resolved
packages/mui-material/src/utils/mergeSlotProps.test.ts Outdated Show resolved Hide resolved
@siriwatknp
Copy link
Member Author

siriwatknp commented Jan 3, 2025

Thanks for working on this @siriwatknp. I think it will be very useful now that we're standardizing the slots pattern.

Yes, this PR will be used by some Material UI components too, e.g. a SwipeableDrawer where it extends the Drawer component.

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.

LGTM

@siriwatknp siriwatknp merged commit ba35dfc into mui:master Jan 6, 2025
22 checks passed
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants