question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Theme overrides do not work for classes defined for custom component in `withStyles`

See original GitHub issue

I have a use case where I have defined classes for a custom component and inject those using the withStyles HOC. My classes depend heavily on the props passed to the component. However, I would also like to be able to override these classes using the global theme’s overrides object.

  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

Let me explain by showing the code:

const styles = (theme: Theme) =>
  createStyles({
    Appbar: ({ backgroundColor }: ComponentProps) => ({
        backgroundColor,
        // other styles
    }),
    // other classes...
});

This Appbar class is depending on the backgroundColor prop. That is why the class is a callback that returns the style object. Makes sense. I inject the classes like this:

export default withStyles(styles, {
  name: 'Component',
})(Component);

Now in global theme’s overrides, I would like to be able to override this class. I will do it something like this:

const theme = createTheme({
    overrides: {
         Component: {
             Appbar: {
                 backgroundColor: 'black',
             },
         } ,
      },
     // my large theme...
});

Unfortunately, this override will not work, and I will lose the styles that were defined in the component itself as well. If I remove the prop from the component class itself (making it independent of props) and a simple style object, the theme override works.

The strange thing is that the callback function that returns the style object is never called if I have overrides defined for that class. That is why I would also lose the class defined within the component itself.

Expected Behavior 🤔

The overrides classes should override the component level class, regardless if the component level class is a props dependent callback that returns the style object, or the style object itself.

Using material-ui: ^4.12.3

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
PabloLIONcommented, Oct 14, 2021

Thank you for the reply @oakhtar147,

  • Nice to see you fix the problems.
  • Truely, the @material-ui is still with version 4.x.x but I think they are moving on @mui with version 5.x.x.
  • Thanks for offering the help. I already resolved it: I imported in MUIv5 both withTheme() and withStyle() from @mui/material, but they are moved to @mui/styles. By the way, I like your personal website a lot!
0reactions
oakhtar147commented, Oct 14, 2021

Hi @Pablion,

  • I have fixed the checkboxes, thank you for pointing it out.
  • The latest stable version of @material-ui/core is 4.12.3. You can confirm this by either looking into their NPM page or run npm info @material-ui/core | grep latest in your project’s root dir.
  • No, we do not have the same bugs; however, I might be able to help if you could elaborate more, perhaps show the styles you have created.

Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Theme overrides do not work for classes defined for custom ...
I have a use case where I have defined classes for a custom component and inject those using the withStyles HOC. My classes...
Read more >
reactjs - Material UI theme `overrides` not working while using ...
The problem I am facing is that the styles in the overrides object will not apply/override the classes defined in the createStyles ....
Read more >
How to customize - Material UI - MUI
Overriding nested component styles. To customize a specific part of a component, you can use the class name provided by Material UI inside...
Read more >
Global Styling with Material-UI Theme Overrides and Props
Learn how to use global CSS overrides and default props in a theme to customize all instances of a Material-UI component in a...
Read more >
Advanced Usage - styled-components
A theme can also be passed down to a component using the theme prop. This is useful to circumvent a missing ThemeProvider or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found