I am trying to animate a button when hovered using styled
from MUI 5 - but it is not working. I tried to find inspiration from:
- How to apply custom animation effect @keyframes in MaterialUI using makestyles()
- https://github.com/mui-org/material-ui/issues/24851
.. with no luck.
Try and have a look and tell what I cannot see:
import Button from "@mui/material/Button";
import { styled } from "@mui/material/styles";
import { keyframes } from "@emotion/react";
const getAnimation = () => keyframes`
0 % { transform: translate(1px, 1px) rotate(0deg) },
10% { transform: translate(-1px, -2px) rotate(-1deg); },
20% { transform: translate(-3px, 0px) rotate(1deg); },
30% { transform: translate(3px, 2px) rotate(0deg); },
40% { transform: translate(1px, -1px) rotate(1deg); },
50% { transform: translate(-1px, 2px) rotate(-1deg); },
60% { transform: translate(-3px, 1px) rotate(0deg); },
70% { transform: translate(3px, 1px) rotate(-1deg); },
80% { transform: translate(-1px, -1px) rotate(1deg); },
90% { transform: translate(1px, 2px) rotate(0deg); },
100% { transform: translate(1px, -2px) rotate(-1deg); }
`;
const StyledButton = styled((props) => {
const { ...other } = props;
return <Button {...other} />;
})(({ theme }) => ({
":hover": {
animation: `${getAnimation} shake infinite`
},
backgroundColor: "#2699FB",
color: "#FFFFFF"
}));
const App = () => {
return (
<StyledButton variant="contained">
My button
</StyledButton>
)
}
export default App