javascriptcssreactjsmaterial-ui

Change ExpansionPanel arrow color on expanded parent ExpansionPanel in material-ui


Trying to figure out how to get material-ui themes to work how I want it to, and having some trouble.

what I want is for my expansion panels to have a different arrow color when expanded to make them more visible. The catch is that I need this behavior only on parent expansion panels, and not expansion panels within expansion panels.

Currently I have written my material-ui theme so that only parent expansion panels have their color change when expanded, like so: enter image description here

My issue is that the black arrow is hard to see against the dark-blue panel summary. I would like to be able to change that color to white, but only when in a parent panel. So basically, whenever an ExpansionPanelSummary is blue, the arrow should be white.

I can't seem to find a CSS control to do what I want. Here is my theme for ExpansionPanelSummary that controls the color (I think my CSS for the arrow should be in here somewhere but I'm not sure):

      MuiExpansionPanelSummary: {
        root: {
          minHeight: "0px",
          minWidth: "0px",
          "&$expanded": {
            //Elevation 1
            boxShadow:
              "0px 1px 3px 0px rgba(0, 0, 0, 0.2), 0px 1px 1px 0px rgba(0, 0, 0, 0.14), 0px 2px 1px -1px rgba(0, 0, 0, 0.12)",
            minHeight: "0px",
            backgroundColor: "#3f51b5",
            color: "white"
          },
          ".MuiExpansionPanelDetails-root &$expanded": {
            backgroundColor: "transparent",
            color: "black"
          }
        },
        content: {
          minWidth: "0px",
          margin: "8px 0px",
          "&$expanded": {
            margin: "8px 0px"
          }
        }
      },

Any help or pointers would be appreciated.


Solution

  • Below is one way of achieving this which uses the MuiExpansionPanelSummary-expandIcon class to target the icon and overrides this back to the default for the nested expansion panel.

    import { createMuiTheme } from "@material-ui/core";
    const defaultTheme = createMuiTheme();
    
    const myTheme = createMuiTheme({
      overrides: {
        MuiExpansionPanelSummary: {
          root: {
            minHeight: "0px",
            minWidth: "0px",
            "&$expanded": {
              //Elevation 1
              boxShadow:
                "0px 1px 3px 0px rgba(0, 0, 0, 0.2), 0px 1px 1px 0px rgba(0, 0, 0, 0.14), 0px 2px 1px -1px rgba(0, 0, 0, 0.12)",
              minHeight: "0px",
              backgroundColor: "#3f51b5",
              color: "white",
              "& .MuiExpansionPanelSummary-expandIcon": {
                color: "white"
              }
            },
            ".MuiExpansionPanelDetails-root &$expanded": {
              backgroundColor: "transparent",
              color: "black",
              "& .MuiExpansionPanelSummary-expandIcon": {
                color: defaultTheme.palette.action.active
              }
            }
          }
        }
      }
    });
    
    export default myTheme;
    

    Edit custom ExpansionPanelSummary theme