11

I am trying to style using theme overrides as laid out in the documentation here:

I have the following code sandbox:

import * as React from 'react';
import { ThemeProvider, createTheme } from '@mui/material/styles';
import Select from '@mui/material/Select'
import MenuItem from '@mui/material/MenuItem'

const theme = createTheme({
  components: {
    MuiSelect: {
      styleOverrides: {
        root: {
          background: '#000',
        },
      },
    },
  },
});

export default function GlobalThemeOverride() {
  return (
    <ThemeProvider theme={theme}>
          <Select
            labelId="demo-simple-select-label"
            id="demo-simple-select"
            variant="standard"
          >
            <MenuItem value={10}>Ten</MenuItem>
            <MenuItem value={20}>Twenty</MenuItem>
            <MenuItem value={30}>Thirty</MenuItem>
          </Select>
    </ThemeProvider>
  );
}

The Select box should have a background of #fff however no background is set at all. :(

NearHuscarl
  • 66,950
  • 18
  • 261
  • 230
Jamie Hutber
  • 26,790
  • 46
  • 179
  • 291

1 Answers1

8

This looks like a bug to me (or at least missing a feature that is reasonable for developers to expect to be there). The issue is that Select doesn't define any styles of its own at the root level, so it doesn't leverage the code (which would be a call to MUI's styled such as here for the select class) that would take care of looking at the theme and applying the corresponding style overrides. I recommend logging an issue.

There are a couple possible workarounds.

Workaround 1 - Target the select CSS class

This approach may work, but it depends on what all you are trying to do since this targets a child of the root element.

const theme = createTheme({
  components: {
    MuiSelect: {
      styleOverrides: {
        select: {
          backgroundColor: "#000",
          color: "#fff"
        }
      }
    }
  }
});

Edit GlobalThemeOverride Material Demo (forked)

Workaround 2 - Target the MuiInput-root CSS class

The approach below targets the same element as MuiSelect-root by leveraging MuiInput-root (the rendering of the root element of the Select is delegated to the Input component when the variant is "standard") and then qualifying it via "&.MuiSelect-root" so that it only affects Select rather than all inputs.

const theme = createTheme({
  components: {
    MuiInput: {
      styleOverrides: {
        root: {
          "&.MuiSelect-root": {
            backgroundColor: "#000",
            color: "#fff"
          }
        }
      }
    }
  }
});

Edit GlobalThemeOverride Material Demo (forked)

Ryan Cogswell
  • 75,046
  • 9
  • 218
  • 198
  • putting the styles directly in `root` works for [me](https://i.stack.imgur.com/DmhVS.png) in approach #2. – NearHuscarl Oct 14 '21 at 22:23
  • 3
    @NearHuscarl Yes, but it will affect all standard `Input` elements, not just `Select` elements. – Ryan Cogswell Oct 14 '21 at 22:24
  • @NearHuscarl Example: https://codesandbox.io/s/globalthemeoverride-material-demo-forked-qbtuq?file=/demo.tsx – Ryan Cogswell Oct 14 '21 at 22:26
  • Oh yeah, you're right. I'm too tired to look at it now, but maybe it is also related to [this](https://stackoverflow.com/a/69280433/9449426). – NearHuscarl Oct 14 '21 at 22:32
  • Ahhh interesting, so you can still refer to things as `MuiXxx`. This single fact has confused me allot. Do you use `"& .xxx"` or do you use `select` as suggested in the documentation. But I see, the key is actually to use the cascading idea and go levels above and come back down.... Very good to know, I appreciate it. – Jamie Hutber Oct 15 '21 at 07:23
  • @JamieHutber The `Muixx` name is a convention, some components like `Box` doesn't have it. See [this](https://stackoverflow.com/a/69455458/9449426) answer if you want to apply the styles from `createTheme` in your custom component. See this typescript [file](https://github.com/mui-org/material-ui/blob/e0cdcd130db60d252c4382570844ea7278649a08/packages/mui-material/src/styles/components.d.ts) for a list of all supported components. – NearHuscarl Oct 15 '21 at 15:24
  • Interestingly, I tried with `root` and it failed. I needed the `select` part again! :O – Jamie Hutber Jul 13 '22 at 11:47