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.

Custom theme: styleOverrides on Ra* components are not applied

See original GitHub issue

What you were expecting:

It being possible to override styles of Ra* components in the theme as in v3.

What happened instead:

Styles specified in the theme are not applied. Styles on Mui* components are, however, applied as expected.

Steps to reproduce:

  1. Create a custom theme and pass it to <Admin>
  2. Add styleOverrides on react-admin components (see below)
  3. Open in browser

Related code:

https://codesandbox.io/s/determined-wilson-k6ifbu?file=/src/index.tsx:722-1068

const theme = _.merge({}, defaultTheme, {
  components: {
    MuiIconButton: {
      styleOverrides: {
        root: {
          // works as expected
          color: "red"
        }
      }
    },
    RaList: {
      styleOverrides: {
        content: {
          // does not work
          backgroundColor: "red"
        }
      }
    }
  }
});

Other information:

Environment

  • React-admin version: 4.0.1
  • Last version that did not exhibit the issue (if applicable): 3.19.7
  • React version: 17.0.2
  • Browser: Chrome 100.0.4896.127
  • Stack trace (in case of a JS error):

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
fzaninottocommented, May 20, 2022

To me it looks like the example in the “Global Theme Overrides” section in documentation is wrong:

Well spotted! Would you like to open a PR to fix it?

1reaction
sweco-sedalhcommented, May 2, 2022

I haven’t fully understood how MUI wants it now in v5, but from what I understand the overridesResolver (for example https://github.com/marmelab/react-admin/blob/master/packages/ra-ui-materialui/src/list/ListView.tsx#L171) would need to be adjusted, see for a somewhat related example: https://mui.com/system/styled/#custom-components.

I believe something along these lines should work:

  overridesResolver: (props, styles) => {
    return [
      styles.root,
      {
        [`& .${ListClasses.main}`]: styles.main,
        [`& .${ListClasses.content}`]: styles.content,
        [`& .${ListClasses.actions}`]: styles.actions,
        [`& .${ListClasses.noResults}`]: styles.noResults
      }
    ];
  },
Read more comments on GitHub >

github_iconTop Results From Across the Web

Mui v5 styleOverrides not working with themes - Stack Overflow
The issue is that Select doesn't define any styles of its own at the root ... at the theme and applying the corresponding...
Read more >
React-admin - Theming
Leveraging this technique, you can create a custom theme that overrides the style of a component for the entire application. For instance, to...
Read more >
Themed components - Joy UI - MUI
Learn how to apply custom styles to components at the theme level. ... Use styleOverrides to apply styles to each component slots. Every...
Read more >
Overriding styles with the sx prop | Primer React
The sx prop allows ad-hoc styling that is still theme-aware. Declare the styles you want to apply in CamelCase object notation, ...
Read more >
When to Use the MUI SX Prop, Styled API, or Theme Override
Do you want to build a full MUI app from beginning to end, learn every aspect of the sx prop, styled API, and...
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