Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MaterialUI Custom Hover Style

Tags:

reactjs

styles

I'm a newbie here to React and I'm a little bit confused on how to override classes in Material UI. I took a look at the examples and tried to mimic it but it didn't seem to do what I want it to do.

Basically, on a table row hover, I want to make it set a background color different from what it is currently doing.

Here is my approach:

const styles = theme => ({   root: {     width: "100%",     marginTop: theme.spacing.unit * 3   },   table: {     minWidth: 1020   },   tableWrapper: {     overflowX: "auto"   },   hover: {     "&:hover": {       backgroundColor: 'rgb(7, 177, 77, 0.42)'     }   } });  return <TableRow hover classes={{hover: classes.hover}} role="checkbox" aria-checked={isSelected} tabIndex={-1} key={n.row_id} selected={isSelected}>      {this.insertRow(n, isSelected, counter, checkbox)} 

;

export default withStyles(styles)(EnhancedTable); 

Thanks for your help!

like image 876
Tim Avatar asked Oct 01 '18 17:10

Tim


People also ask

How do you style hover in MUI?

MUI Styled Components Code Here's a simple styled button: const StyledButton = styled(Button)` background-color: grey; color: #fff; padding: 6px 12px; &:hover { background-color: #a9a9a9; } &:focus { background-color: green; } `; The hover selector is simple to add.

How do you add a hover style in React?

Set the onMouseEnter and onMouseLeave props on the element. When the user hovers over or out of the element, update a state variable. Conditionally set inline styles on the element.

How do you use hover style?

The :hover selector is used to select elements when you mouse over them. Tip: The :hover selector can be used on all elements, not only on links. Tip: Use the :link selector to style links to unvisited pages, the :visited selector to style links to visited pages, and the :active selector to style the active link.

Is makeStyles deprecated?

Though deprecated, you can still use makeStyles() in MUI 5, but we expect it (to be removed in v6, but hence my confusion between past and present tense). If you're familiar with React and JSX, this should look straightforward.


2 Answers

If you are looking to make some custom hover animations then you can try this style
This block of code will change the colour of a card on hover.

For more info please check here Transitions in MUI

card : {     transition: theme.transitions.create(["background", "background-color"], {       duration: theme.transitions.duration.complex,     }),     "&:hover": {       backgroundColor: "#333",     }, }  
like image 39
theshubhagrwl Avatar answered Oct 11 '22 11:10

theshubhagrwl


You should define a key for TableRow as a className and then put your hover style right on that class name as an object.

const styles = theme => ({   ...   tr: {     background: "#f1f1f1",     '&:hover': {        background: "#f00",     },   },   ... });  return <TableRow className={props.classes.tr} ...> 

In another example it would be something like this:

const styles = {   tr: {     background: "#f1f1f1",     '&:hover': {       background: "#f00",     }   } };  function Table(props) {   return (     <Table>       <TableRow className={props.classes.tr}>         {"table row"}       </TableRow>     </Table>   ); }  export default withStyles(styles)(Table); 
like image 154
Spleen Avatar answered Oct 11 '22 10:10

Spleen