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?
If you want to override a component's styles using custom classes, you can use the className prop, available on each component.
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 .
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.
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.
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.
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.
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
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 }}
>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With