Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
150
rated 0 times [  154] [ 4]  / answers: 1 / hits: 12841  / 4 Years ago, tue, march 3, 2020, 12:00:00

I am using a component from an external library that does not allow me to change much of its style, but I would like to change the style of a button that is a material ui button, when inspecting the element it clearly shows the classes MuiButtonBase-root MuiIconButton-root MuiIconButton-colorInherit



how can I override the MuiIconButton-root style, for instance? this is my component:



class MyComponent extends Component {

render() {
const { classes } = this.props;
return (
<div className={classes.myComponent}>
<3rdPartyComponent />
</div>
);

}
}


export default withStyles(styles)(MyComponent)


I have tried declaring my styles function as follows, without success:



const styles = theme => ({
myComponent: {
&.MuiIconButton-root: {
padding: 0px
}
}
});


Can anybody help me? Thanks in advance.


More From » reactjs

 Answers
2

Let's say that the class name generated for myComponent is myComponent-jss123. The selector you used in your styles (&.MuiIconButton-root) would be equivalent to .myComponent-jss123.MuiIconButton-root which would match any element that has both of these classes applied to it. I believe your intent was to match icon buttons which are descendants of the div on which you are applying the myComponent class. In this case you need to use the descendant combinator represented by a space, so the appropriate styles would look like the following:



const styles = theme => ({
myComponent: {
& .MuiIconButton-root: {
padding: 0
}
}
});


Here's a full working example:



import React from react;
import IconButton from @material-ui/core/IconButton;
import DeleteIcon from @material-ui/icons/Delete;
import { makeStyles } from @material-ui/core/styles;

const useStyles = makeStyles({
myComponent: {
& .MuiIconButton-root: {
padding: 0
}
}
});
const ThirdPartyComponent = () => {
return (
<div>
I'm a third party component that contains an IconButton:
<IconButton color=primary>
<DeleteIcon />
</IconButton>
</div>
);
};
export default function App() {
const classes = useStyles();
return (
<div className={classes.myComponent}>
<ThirdPartyComponent />
</div>
);
}


Edit



Related documentation:




[#4582] Friday, February 28, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
latrelllloydb

Total Points: 449
Total Questions: 92
Total Answers: 100

Location: French Polynesia
Member since Tue, Jul 7, 2020
4 Years ago
latrelllloydb questions
Fri, Jul 10, 20, 00:00, 4 Years ago
Fri, May 29, 20, 00:00, 4 Years ago
Mon, Jun 17, 19, 00:00, 5 Years ago
;