I have a Material UI table in React 18 and each cell has a different background color. However this will break the hover style on the row and I am not able to find a solution.
Just to be clear you can check here what I want to achieve: https://explain.depesz.com/s/dFJ2
If you hover on the table’s row you can see that the color just gets darker.
How can I achieve this using React and Material UI? Is there a simple way?
From my Cell component I can set a different color for when it is in hover state, but this will work for single Cell hover not for the entire Row
export const MyCustomCell = (props) => {
const theme = useTheme();
return (
<TableCell
style={{
backgroundColor: getCellBackground(props)
}}
sx={{
"&.MuiTableCell-root:hover": {
backgroundColor: getCellBackgroundOnHover(props)
}
}}
>
{betterTiming(prop)}
</TableCell>
)
}
<TableRow
role="checkbox"
sx={{
'&:last-child td, &:last-child th': {border: 0},
// "&.MuiTableRow-root:hover": {
// backgroundColor: "pink !important"
// }
}}
tabIndex={-1}
>
</TableRow>
EDIT: The color is computed dynamically from props
2
Answers
Unfortunately I could not find any solution by searching on google and I ended up by just using hover state in React
And then
getCellBackgroundColor
will just calculate the color according to my specific business logic.Then Modify MyCustomCell component to use CSS variables for background colors:
In your CSS, define the CSS variables and styles for both the cell and row hover:
In your TableRow component, add the hoverable-row class: