-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
[system] Support callback value in styleOverrides
slot
#30524
Conversation
>; | ||
|
||
export type ComponentsOverrides = { | ||
export type ComponentsOverrides<Theme = unknown> = { |
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.
add default type so it does not break the people who import ComponentsOverrides
in whatever cases they are using.
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.
Looks good generally. When we should deprecate the keys that we don't plan to support in v6?
The deprecation will touch a lot of files (basically, all components) and the Codemod should be in place, so I propose we do the deprecation after we have migrated to the new URLs. |
…style-overrides-callback
…style-overrides-callback
d87fa09
to
de46f74
Compare
@michaldudak tag you as another reviewer. As discussed with @mnajdova, we plan to mention |
@@ -2,6 +2,29 @@ | |||
|
|||
<p class="description">The theme's components key allows you to customize a component without wrapping it in another component. You can change the styles, the default props, and more.</p> | |||
|
|||
## Default props |
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.
moved Default props
up
74a5c01
to
90701bb
Compare
@mnajdova I found some bug when doing this in Chip: {
MuiChip: {
styleOverrides: {
icon: ({ ownerState }) => ({ ... }), // this does not work
}
}
} Some keys does not work because they are nested classNames: // in Chip.js
const ChipRoot = styled('div', {
name: 'MuiChip',
slot: 'Root',
overridesResolver: (props, styles) => {
return [
{ [`& .${chipClasses.icon}`]: styles.icon }, // < function does not work here.
// ...other styles
]
} To fix this, I added statement to check if it is a function: // in Chip.js
const ChipRoot = styled('div', {
name: 'MuiChip',
slot: 'Root',
overridesResolver: (props, styles) => {
return [
{ [`& .${chipClasses.icon}`]: typeof styles.icon === 'function' ? styles.icon(props) : styles.icon },
// ...other styles
]
} From what I check, there are not a lot of components that has nested className styles, so I think this is enough for the styleOverrides callback to work for non-slot keys. Also, they will be removed in the next major version anyway. If you are okay with this approach, I will merge the PR. |
@siriwatknp how about we resolve the keys of the index 42ae0c4cf4..a5c04ff9b3 100644
--- a/packages/mui-system/src/createStyled.js
+++ b/packages/mui-system/src/createStyled.js
@@ -133,9 +133,12 @@ export default function createStyled(input = {}) {
expressionsWithDefaultTheme.push((props) => {
const theme = isEmpty(props.theme) ? defaultTheme : props.theme;
const styleOverrides = getStyleOverrides(componentName, theme);
-
+ const resolvedStyleOverrides = Object.keys(styleOverrides).reduce((acc, key) => {
+ acc[key] = typeof styleOverrides[key] === 'function' ? styleOverrides[key](props) : styleOverrides[key];
+ return acc;
+ }, {})
if (styleOverrides) {
- return overridesResolver(props, styleOverrides);
+ return overridesResolver(props, resolvedStyleOverrides);
}
return null; |
This reverts commit b85903e.
Yeah, this is better. |
@siriwatknp did you check that this would work with string templates? |
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.
@michaldudak tag you as another reviewer. As discussed with @mnajdova, we plan to mention ownerState on the docs so that developers can use it to conditionally apply styles. Are you good with this?
Sure, let them know about it. It's an external API after all.
Good job overall with the PR!
docs/src/pages/customization/theme-components/theme-components.md
Outdated
Show resolved
Hide resolved
Co-authored-by: Michał Dudak <michal.dudak@gmail.com>
Yes, a test added in |
|
||
## Adding new component variants | ||
|
||
> ⚠️ This API has been **deprecated** and will likely be removed in the next major release. If you want to apply styles based on props, take a look at [Overrides based on props](#overrides-based-on-props) instead. |
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.
I landed here after seeing the notion of deprecation in #30668.
We might be jumping one step head too quickly on this one. I will add a new comment on why we added the "variant" API in the first place, it's true purpose doesn't seem covered in #30412 or in #28107 (from my perspective on why we added it in the first place, it wasn't because of JSS, when we added it we were already adding emotion from what I recall, we knew this PR was going to be possible).
Edit: done #30412 (comment)
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.
Okay, I will remove the deprecation section for now and update the #30412.
This appears to have been merged with the deprecation warning of the But the comments above lead me to believe that it wasn't supposed to be deprecated, or at least there was some contention on that point? At the very least, the documentation says nothing about the deprecation of this prop: https://mui.com/customization/theme-components/#adding-new-component-variants This is leading to a very confusing experience. Should I or should I not use |
@genepaul I think that the main learning here is that when MUI adds deprecations or breaking changes, it's best to do it in isolated PRs so that the matters are isolated and communicated very clearly to the community. I would propose this as a best practice.
Regarding the topic specifically, I don't know 😁 @siriwatknp should be able to provide lights on this. What I had understood is that this PR adds great new functionality. However, this new API duplicates to some extent with the |
@oliviertassinari I get that, but this PR added a deprecation notice for variants, when whether or not that should be deprecated appears to be in dispute. Or am I misunderstanding? |
Thanks for noticing this and your feedback is very welcome. To give you some context, this PR is the result of #30412. At first, I thought that using callback already covers the To summarize:
|
@siriwatknp - thank you for the clarification. That was my main confusion on all of this - I love the efforts to consolidate the many ways to do component theming, I was just surprised when I went to do something the documentation told me I could do, and the code told me it was deprecated. Just wanted to make sure it was clear. I appreciate all the work on this library! |
This is the first PR as described in #30412
close #27415
📦 Code sandbox
🔗 Deploy preview
Summary
Review suggestion
types
packages/mui-material/src/styles/overrides.d.ts