The background color of the element can be customized in TableRow
. To ensure proper specificity and avoid using "!important", you should utilize the "hover" class as demonstrated within the TableRow
.
The text color is controlled in TableCell
, so this is where you should make adjustments.
To implement a solution, your styles may look like this:
const styles = theme => ({
tableRow: {
"&$hover:hover": {
backgroundColor: "blue"
}
},
tableCell: {
"&$hover:hover &": {
color: "pink"
}
},
hover: {}
});
then in the render method:
<TableRow
hover
key={row.id}
classes={{ hover: classes.hover }}
className={classes.tableRow}
>
<TableCell
className={classes.tableCell}
component="th"
scope="row"
>
{row.name}
</TableCell>
Here's a functional example based on your sandbox link:
https://codesandbox.io/s/k36qlx0l1v?fontsize=14
Another similar example using "selected" instead of "hover":
https://codesandbox.io/s/llyqqwmr79
These examples utilize these styles:
const styles = theme => ({
tableRow: {
"&$selected, &$selected:hover": {
backgroundColor: "purple"
}
},
tableCell: {
"$selected &": {
color: "yellow"
}
},
selected: {}
});
and some state management:
const [selectedID, setSelectedID] = useState(null);
Adjusting the TableRow rendering to:
<TableRow
hover
key={row.id}
onClick={() => {
setSelectedID(row.id);
}}
selected={selectedID === row.id}
classes={{ selected: classes.selected }}
className={classes.tableRow}
>
In Material-UI v4, there will be updates that simplify customizing styles without overriding them.
v4 introduces global class names for the selected state ("Mui-selected") and for TableCell ("MuiTableCell-root"), making it easier to style with just one class applied to TableRow:
const styles = (theme) => ({
tableRow: {
"&.Mui-selected, &.Mui-selected:hover": {
backgroundColor: "purple",
"& > .MuiTableCell-root": {
color: "yellow"
}
}
}
});
https://codesandbox.io/s/table-hover-colors-forked-3vmds?fontsize=14&hidenavigation=1&theme=dark