-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
base: master
Are you sure you want to change the base?
Conversation
Netlify deploy preview@material-ui/core: parsed: +0.07% , gzip: +0.06% Bundle size reportDetails of bundle changes (Toolpad) |
### Forwarding slot props | ||
|
||
Use the `mergeSlotProps` utility function to merge custom props with the slot props. | ||
If the arguments are functions, they are resolved before merging. The result from the first argument will override the second. |
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.
This is a rare example where it's ok to use the future tense "will" because it's an "if... then" scenario. I would also connect these two sentences into one if I'm understanding it correctly.
If the arguments are functions, they are resolved before merging. The result from the first argument will override the second. | |
If the arguments are functions then they'll be resolved before merging, and the result from the first argument will override the second. |
``` | ||
|
||
:::info | ||
`className` values are concatenated instead of overriding each other. For example, using the above snippet, the `className` of the popper slot passed to `CustomTooltip` is appended. |
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.
The grammar and the explanation are both tricky here—let me know if I understood the original text correctly.
`className` values are concatenated instead of overriding each other. For example, using the above snippet, the `className` of the popper slot passed to `CustomTooltip` is appended. | |
`className` values are concatenated rather than overriding one another. | |
In the snippet above, the `custom-tooltip-popper` class is applied to the Tooltip's popper slot. | |
If you added another `className` via the `slotProps` prop on the Custom Tooltip—as shown below—then both would be present on the rendered popper slot: |
<CustomTooltip slotProps={{ popper: { className: 'foo' } }} /> | ||
``` | ||
|
||
The popper slot will have the following className: `"… custom-tooltip-popper foo"`. |
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.
The popper slot will have the following className: `"… custom-tooltip-popper foo"`. | |
The popper slot in the original example would now have both classes applied to it, in addition to any others that may be present: `"[…] custom-tooltip-popper foo"`. |
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.