Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't override a style of a deeply nested component (Material-UI Jss Styling)

Material-UI's ExpansionPanelSummary component allows to render an icon inside it via the expandIcon prop, and to change its style by the expandIcon css class.
As you can see in the implementation of the component, this class has nested class which adds transform:

'&$expanded': {
     transform: 'translateY(-50%) rotate(180deg)',
 },

There is no access to this nested class via component's API, and my need is to override it.
I have tried to do this with jss-nested plugin as described here and override with classes prop:

expandIcon: {
    "&$expanded": {
        transform: "translateY(-50%) rotate(90deg)"
    }
}

<ExpansionPanelSummary
     expandIcon={<ExpandMoreIcon />}
     classes={{
         expandIcon: classes.expandIcon
     }}
 >

But it not works, and I got this warning in the console:

Warning: [JSS] Could not find the referenced rule expanded in MyExpansionPanel.  

You can see live codesandbox example here.
Am i missing something?

like image 459
Oron Avatar asked Jan 05 '19 21:01

Oron


People also ask

How do you override styles in material UI?

If you want to override a component's styles using custom classes, you can use the className prop, available on each component.

How do you override a style in react?

To override a style, you have two options. Either you can pass a style object, or you can pass a function that will obtain props regarding the current internal state of the component, thus enabling you to modify styles dynamically depending on the component state, such as isError or isSelected .

How to override material UI styles with external CSS?

The options will be available in all child elements of the StylesProvider. We can use the injectFirst boolean prop to add styles that override existing Material UI styles. This way, styles we referenced from external CSS files will override Material UI’s.

How to override material UI styles using stylesprovider in react?

The StylesProvider component lets us change how styles are applied to child components. The options will be available in all child elements of the StylesProvider. We can use the injectFirst boolean prop to add styles that override existing Material UI styles. This way, styles we referenced from external CSS files will override Material UI’s.

How do I add style overrides to a component?

The sx prop is the best option for adding style overrides to a single instance of a component in most cases. It can be used with all Material UI components. To customize a specific part of a component, you can use the class name provided by Material UI inside the sx prop.

How to override styles for the h1 elements in CSS?

Here is an example of how you can override styles for the h1 elements: If you are already using the CssBaseline component for setting baseline styles, you can also add these global styles as overrides for this component. Here is how you can achieve the same by using this approach.


2 Answers

The &$ syntax references a rule in the same style sheet, you need to create an expanded rule and pass it into the classes object e.g.

const styles = theme => ({
  expandIcon: {
    "&$expanded": {
      transform: "translateY(-50%) rotate(90deg)"
    }
  },
  expanded: {},
});

<ExpansionPanelSummary
  expandIcon={<ExpandMoreIcon />}
  classes={{
    expandIcon: classes.expandIcon,
    expanded: classes.expanded,
  }}
>

Codesandbox example: https://codesandbox.io/s/x256q3xz44

like image 164
Josh Wooding Avatar answered Sep 28 '22 06:09

Josh Wooding


In order to override CSS style of the icon on expansion, you need to add classes to both the ExpansionPanelSummery and the ExpandMoreIcon (this is what you are missing).

const styles = theme => ({
  expanded: {
    "& $icon": {
      transform: "translateY(-50%) rotate(90deg)"
    }
  },
  icon: {}
});
<ExpansionPanelSummary
    expandIcon={<ExpandMoreIcon classes={{ root: classes.icon }} />}
    classes={{ expanded: classes.expanded }}
>
like image 31
Claire Lin Avatar answered Sep 28 '22 05:09

Claire Lin