When using the Material-UI Button component, I want the "focus" styling to match the "focusVisible" styling. This means I want the same ripple effect to be visible whether the button is focused programmatically or with the mouse, as it would be when focused with the tab key.
I discovered a workaround by triggering a
dispatchEvent(new window.Event("keydown"))
on the element before focusing it, which forces the keyboard to be the last input type used. This successfully achieves the desired button appearance until a mouse event like onMouseLeave is fired, causing the visible focus to disappear.
I managed to alter the focus styling of the component using the following code:
import React from "react"
import { withStyles } from "@material-ui/core/styles"
import Button from "@material-ui/core/Button"
const styles = {
root: {
'&:focus': {
border: "3px solid #000000"
}
}
}
const CustomButtonRaw = React.forwardRef((props, ref) => {
const { classes, ...rest } = props
return <Button classes={{root: classes.root}} {...rest} ref={ref}/>
}
const CustomButton = withStyles(styles, { name: "CustomButton" })(CustomButtonRaw)
export default CustomButton
Although I was able to apply some styles to the button in the "focus" state (such as adding a border), I am struggling to get the styles to actually apply. I attempted adding the className 'Mui-visibleFocus' to the Button, but it appeared to have no effect. Is there a way to access the styles that would be applied if the Button was in the visibleFocus state?