-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
Conversation
Netlify deploy preview@material-ui/core: parsed: +0.07% , gzip: +0.07% Bundle size reportDetails of bundle changes (Toolpad) |
Co-authored-by: Sycamore <[email protected]> Signed-off-by: Siriwat K <[email protected]>
There was a problem hiding this 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.
Yes, this PR will be used by some Material UI components too, e.g. a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Why is this utility implemented so that the first param overwrites the second: Previously I've been writing code like this to override the some of the default values while keeping the rest untouched: slotProps={{
...props.slotProps,
popper: {
...props.slotProps?.popper,
disablePortal: true,
placement: 'top',
}),
}} Assuming props.slotProps === {
popper: {
disablePortal: false,
placement: 'bottom',
}
} I ended up with popper: {
disablePortal: true,
placement: 'top',
} Now looking at mergeSlotProps(props.slotProps?.popper, {
className: 'custom-tooltip-popper',
disablePortal: true,
placement: 'top',
}) ...my expectation would be the same kind of behavior, that the default props I get from popper: {
disablePortal: false,
placement: 'bottom',
} The docs do call out the actual behavior, that the first param overwrites the second, but to me this seems counterintuitive and easy to trip up on. This utility also reminds me of |
Thanks for the feedback. My intention is to let TypeScript pickup the type from the first argument so that you can leverage the autocompletion/type correction when providing the second argument. mergeSlotProps(props.slotProps?.popper, {
// you will get type completion based on the types of `slotProps?.popper`.
}) |
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 theclassName
that's concatenated to preserve the same behavior ofslotProps
, the rest will override the default ones. The usage is added to the docs.