I am making a custom input component with MUI InputBase, and I want to have a "Clear" button endAdornment that only appears when you hover over the input:
<InputBase
inputComponent={getCustomInputComponent()}
onClick={onClick}
...
endAdornment={
<IconButton
size='small'
onClick={handleClear}>
<IconClear fontSize='small'/>
</IconButton>
}
/>
Similar to how their new "Autocomplete" component works: https://material-ui.com/components/autocomplete/
I've looked at the source code of Autocomplete but I can't get it working in my component, any suggestions?
Hover effect on IconButton in Material-UI Ask Question Asked4 years, 1 month ago Modified2 months ago Viewed25k times 11 1 IconButton Hover This is an iconButton from Material-UI that I am using. As you can see there is a slight grey border around the icon when you hover on it.
The Material-UI IconButton creates a clickable icon with all the props and behavior of a MUI Button. In this post we will learn how to add hover functionality such as opening a tooltip and changing color. We will also add an onClick handler and examine how IconButtons are rendered in the DOM.
Take a look at the abbreviated code below: Simply wrap the IconButton in a Tooltip and give the Tooltip a title (which is the text it will display). MUI then adds the desired hover functionality to the IconButton. In my example, I have a tooltip but I also have default hover styling disabled.
When you hover over an IconButton, you usually want to do one of two things: We will look at examples of both of these below. As info, hover styling can be applied using a handler like onMouseOver, but it requires less code and runs faster to add hover style using CSS.
Below is an example that is roughly equivalent to what is being done in Autocomplete. The gist of the approach is to make the icon hidden by default, then flip the visibility to visible
on hover of the input (&:hover $clearIndicatorDirty
) and when the input is focused (& .Mui-focused
), but only if there is currently text in the input (clearIndicatorDirty
is only applied when value.length > 0
).
import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import TextField from "@material-ui/core/TextField";
import IconButton from "@material-ui/core/IconButton";
import ClearIcon from "@material-ui/icons/Clear";
import clsx from "clsx";
const useStyles = makeStyles(theme => ({
root: {
"&:hover $clearIndicatorDirty, & .Mui-focused $clearIndicatorDirty": {
visibility: "visible"
}
},
clearIndicatorDirty: {},
clearIndicator: {
visibility: "hidden"
}
}));
export default function CustomizedInputBase() {
const classes = useStyles();
const [value, setValue] = React.useState("");
return (
<TextField
variant="outlined"
className={classes.root}
value={value}
onChange={e => setValue(e.target.value)}
InputProps={{
endAdornment: (
<IconButton
className={clsx(classes.clearIndicator, {
[classes.clearIndicatorDirty]: value.length > 0
})}
size="small"
onClick={() => {
setValue("");
}}
>
<ClearIcon fontSize="small" />
</IconButton>
)
}}
/>
);
}
Related documentation:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With