-
-
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] Allow callbacks when defining styleOverrides inside the theme #27415
Comments
After spending a bit more time thinking about this, 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 I will leave the issue open still to see if there will be any traction. |
Allowing callbacks when defining styleOverrides supports the use case if developers want to deviate or augment from the material-ui theme standard and extend on top of it. For example, suppose if we had a let theme = createTheme({
typography: {
primaryFontFamily: '"Times New Roman", serif',
secondaryFontFamily: 'cursive'
},
palette: {
primary: {
// Purple and green play nicely together.
main: purple[500]
},
secondary: {
// This is green.A700 as hex.
main: "#11cb5f"
}
}
});
theme = createTheme(theme, {components: {
MuiButton: {
styleOverrides: {
root: {
fontFamily: theme.typography.secondaryFontFamily
}
}
}
}}); While this does make the Button <ThemeProvider theme={createTheme(theme, {typography: {secondaryFontFamily: "'sans-serif'"}})}>
</ThemeProvider> It does not change the To avoid making a completely custom const theme = createTheme(
{
typography: {
primaryFontFamily: '"Times New Roman", serif',
secondaryFontFamily: 'cursive'
},
palette: {
primary: {
// Purple and green play nicely together.
main: purple[500]
},
secondary: {
// This is green.A700 as hex.
main: "#11cb5f"
}
},
components: {
MuiButton: {
styleOverrides: {
root:({theme}) => ({
fontFamily: theme.typography.secondaryFontFamily
})
}
}
}); Here's a sandbox that provides why this use-case is useful: https://codesandbox.io/s/createtheme-supports-a-callback-usecase-tuibt?file=/demo.tsx Is it possible to do the same codesandbox with |
@hrgui yes this is already possible, see https://codesandbox.io/s/createtheme-supports-a-callback-usecase-forked-h9zkc?file=/demo.tsx |
@mnajdova Looks like that case (using a callback in the |
@jakelauer thanks for pointing this out. The second arg of |
@mnajdova but are the use cases equivalent? One seem to be about customizing all the instances on different slots, while the other to adapt the root slot style to a specific combination of props. It makes me think of this #29581 (comment) |
@oliviertassinari I don't understand this really.
This makes me think of #28107, currently is possible using nested selectors. One API should be enough long term, I cannot imagine scenario that is possible with the I would lean towards closing this issue to avoid confusion to be honest. |
As long as there is a callback option, I believe one API is sufficient. My use case was in attempting to use the current theme's color specifications while modifying the styles, i.e. "set button background to semi-transparent for the current theme's color" is a use-case that requires callbacks because it can't be achieved without access to the theme's color. |
Just a question regarding #29610 - I tried this in our app with casting everything to Given this, is there a specific reason why I suppose this is what |
@Brodzko thanks for testing this out. I intentionally typed only the theme in the callback, so that people would fall back into using the
If we ignore these points, we may as well remove the key from the theme, as the same can be achieved using the |
@mnajdova I agree all these points, however I don't think going with Example:
Having to define each possible combination of But of course then we're back at why keep the |
This is one way of how I would go about it. You can spread this in the
I would wait with updating the types to accept all props, until we actually see a valid reason that it is needed. |
Shouldn't the typing match what's actually provided? If only the theme is allowed as as argument in the callback shouldn't the api itself only give you that and nothing else? My understanding from the corresponding PR is that styleOverrides is deprecated and replaced with variants even though styleOverrides is still being used in the docs? An example use case I don't see a typescript solutions for is when you want the a button to use the main and light shade of the color prop like here:
One wants this working even for custom palettes one adds. Am i missing something obvious? |
I guess you mean like this? Now variants are getting incompatible typing. and the code gets very ugly compared to passing the color prop as callback argument. |
Summary 💡
Currently the
styleOverrides
defined in the theme are applied on top of the styles defined for the component, so they would always win.This means that if developers add overrides for the
root
element those will always be applied on top of whatever styles were defined in the base component (even styles specific for some props combination).For example, the code below would override the background even when the color: 'primary' prop is set:
Would be great if developers can define styles based on combination of props.
Examples 🌈
With the example below, the developers would be able to provide styls overrides that match the condition
props.color !== 'defualt
.The discussion started in #27369.
The text was updated successfully, but these errors were encountered: