Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material-UI: Make IconButton only visible on hover?

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?

like image 902
Rick Barkhouse Avatar asked Dec 04 '19 21:12

Rick Barkhouse


People also ask

Is there a hover effect on iconbutton in material-UI?

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.

What is iconbutton in material UI?

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.

How to add hover to an iconbutton using MUI?

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.

How do I hover over an iconbutton?

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.


1 Answers

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>
        )
      }}
    />
  );
}

Edit Material demo

Related documentation:

  • https://cssinjs.org/jss-plugin-nested?v=v10.0.0#use-rulename-to-reference-a-local-rule-within-the-same-style-sheet
like image 77
Ryan Cogswell Avatar answered Oct 05 '22 22:10

Ryan Cogswell