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

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

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.06%

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against d657ae9

@siriwatknp siriwatknp marked this pull request as ready for review December 20, 2024 02:54
### 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.
Copy link
Contributor

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.

Suggested change
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.
Copy link
Contributor

@samuelsycamore samuelsycamore Dec 20, 2024

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.

Suggested change
`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"`.
Copy link
Contributor

@samuelsycamore samuelsycamore Dec 20, 2024

Choose a reason for hiding this comment

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

Suggested change
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"`.

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.

3 participants