`how can i put a hover effect without using sx pros on Material UI?
I want to give a Hover effect on this code ( I wrote a hover here on the code) but i have no idea how to do it without using the sx props.
import React from 'react'
import { Box } from '@mui/material'
import { MuiSelect } from './MuiSelect'
export const MuiLayout = () => {
return (
<>
<Box
sx={{
backgroundColor: 'primary.main',
color: 'white',
height: '100px',
width: '50%',
padding: '16px',
'&:hover': {
backgroundColor: 'primary.light',
},
}}>
<MuiSelect />
</Box>
<Box
display= 'flex'
height= '100px'
width= '768px'
bgcolor= 'success.light'
// HoVER HERE
p={2}
>
</Box>
</>
)
}
2
Answers
The
onHover
event handler does not exist in React. Therefore, React has provided the following event handlers for detecting the hover state for an element:onMouseEnter
andonMouseLeave
.this is an example using your code :
NOTE : you might no be able to access the default values of the mui palette
(primary.light , primary.main ….). instead use hexadecimal color check t mui docs for these default values
Can you explain why you don’t want to use sx prop? It is there for that kind of usage.
You can go without
sx
with the following approaches:You can use
styled
from@mui/material/styles
and in your component use
<StyledBox {...props}>{children}</StyledBox>
Or you can use theme component overrides. Add class to your component or create special prop variant for it and override the style in the theme
https://mui.com/material-ui/customization/theme-components/