-
-
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
[system] Add posibility to use props in style overrides #28867
[system] Add posibility to use props in style overrides #28867
Conversation
I would like to this in documentation. Can someone help me with that? |
@mnajdova or @oliviertassinari can you give me some feedback and guidance? |
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 would document this as a new example under https://mui.com/customization/theme-components/#global-style-overrides
@mnajdova I have tried to add documentation for this feature, but I am not a great docs writer. |
@mnajdova |
What do you mean it doesn’t have access? |
For example: const Root = styled(ButtonBase, {
name: 'MuiButton',
})(({ theme }) => ({ }));
const Button = forwardRef(function Button(inProps, ref) {
const props = useThemeProps({ props: inProps, name: 'MuiButton' });
const ownerState = {
...props,
color,
component,
disabled,
disableElevation,
disableFocusRipple,
fullWidth,
size,
type,
variant,
};
return <Root ownerState={ownerState}>{children}</Root>
}); In this example Root created by styled doesn't have the same props as component itself. We want to access |
docs/src/pages/customization/theme-components/GlobalThemeOverrideProps.js
Outdated
Show resolved
Hide resolved
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 overall. I will test it more next week and merge it. Thanks a lot for the contribution 🙏
@mnajdova thank you. This would help us with our project a lot. And I am not sure why the prettier doesn't work on windows or my git configuration, can you maybe change gitattributes so it is config independent. |
@rajzik just an FYI I plan to test and merge this tomorrow :) |
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.
It looks good to me!
@mnajdova Is this different from |
@siriwatknp |
export type ComponentsOverrides = { | ||
[Name in keyof ComponentNameToClassKey]?: Partial< | ||
OverridesStyleRules<ComponentNameToClassKey[Name]> | ||
OverridesStyleRules<ComponentNameToClassKey[Name], Name> | ||
>; |
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 means that I can do:
MuiButton: {
styleOverrides: {
startIcon: props => {
// props has type `ButtonProps`
// but props contains only ownerState. see implementation in `/Button.js:315`
}
}
}
I think only root
can be a function.
Sorry, I don't mean variant prop. I mean the createTheme({
components: {
MuiButton: {
variants: [
{ props: { color: 'primary', customProp: true }, style: { ... } },
]
}
}
}) The above customization already existed. Is there any case that it could not achieve without using function? cc @mnajdova |
After spending a bit more time thinking about this, and considering @siriwatknp's review I think that we should consider the following: Currently we have two APIs for overrides in the
const theme = createTheme({
components: {
MuiButton: {
styleOverrides: {
root: { color: 'pink' }
}
}
}
})
const theme = createTheme({
components: {
MuiButton: {
variants: [
{ props: { color: 'primary', customProp: true }, style: { ... } },
]
}
}
}) Both of them were working with JSS and are working with emotion now. In my opinion, we should be dropping the const theme = createTheme({
components: {
MuiButton: {
variants: [
{
{ props: { color: 'primary', customProp: true },
style: {
textTransform: 'none',
border: `2px dashed grey${blue[500]}`,
+ '& .MuiButton-startIcon': {
+ textDecoration: 'underline',
+ },
},
},
],
},
},
}); I was exploring even new API with #28107, but it is not really necessary at this moment. The main motivation for this is that we should not have two APIs for doing the same thing. We could not drop the Sorry for the turnaround @rajzik but I am trying to think about what is the best option long term. Nevertheless, it was a great work. |
@mnajdova No problem, I will close my PR and issue should be closed too. |
This change introduces posibility to use props inside theme components style overrides.
Example
This change is tested and typed and should not break previous behavior
closes #27415
Documentation:
https://deploy-preview-28867--material-ui.netlify.app/customization/theme-components/#props-inside-style-overrides