4

I suppose that the state change of the button (enabled or disabled) is causing the issue. I have 5 action buttons (create, delete, edit, save and cancel). All buttons start disabled except the Create button. When I click the Create button, it becomes disabled and the Save and Cancel buttons become enabled. When it occours, the Save or Cancel tooltip pops up. Sometimes both of them pop up, sometimes only one of them pops up. At the first time, I thought that it was happening in response to focus events. Then I try to disable the tooltip response to focus events setting disableTriggerFocus={true}, but it doesn't work.

Here's the code for ActionButton:

import Tooltip from "@material-ui/core/Tooltip";

const ActionButton = ({ buttonIcon, onClick, disabled, tooltip }) => {
  return (
    <>
      <Tooltip
        title={disabled ? "" : tooltip}
        placement="top"
        arrow
        disableTriggerFocus={true}
      >
        <Button onClick={onClick} disabled={disabled}>
          <ButtonIcon tag={buttonIcon} />
        </Button>
      </Tooltip>
    </>
  );
};

Edit ActionButtonsPainel

Ryan Cogswell
  • 75,046
  • 9
  • 218
  • 198
Juliano Costa
  • 437
  • 6
  • 10

1 Answers1

5

The triggering of the tooltip for hovering is based on the mouseOver and mouseLeave events. mouseOver events get triggered for disabled buttons, but mouseLeave events do not. When you hover over a disabled button it triggers opening the tooltip, but when you leave the disabled button the mouseLeave event is not triggered so the tooltip stays open.

You have code (title={disabled ? "" : tooltip}) that suppresses the tooltip text when it is disabled, but the tooltip still thinks it is "open". Then when you enable the button, the text of the tooltip is restored and immediately displays. So which buttons this occurs on depends on which disabled buttons you happened to hover over while they were disabled.

You can fix this by explicitly controlling the open state of the Tooltip using the open, onOpen, and onClose properties. onOpen fires when Tooltip thinks it should open and onClose fires when Tooltip thinks it should close, but you can combine this information with additional information (e.g. the disabled state) to decide on the value of the open property.

Below is a working version of ActionButton. The useEffect call is to handle the case where the tooltip is open as you click on the button. If the button is disabled by the click, then onClose won't fire when leaving the button since the mouseLeave event won't be triggered for the disabled button, so the effect handles closing the tooltip in that case.

import Tooltip from "@material-ui/core/Tooltip";
import { useState, useEffect } from "react";

const ActionButton = ({ buttonIcon, onClick, disabled, tooltip }) => {
  const [open, setOpen] = useState(false);
  useEffect(() => {
    if (disabled && open) {
      setOpen(false);
    }
  }, [disabled, open]);
  return (
    <>
      <Tooltip
        title={tooltip}
        placement="top"
        arrow
        onOpen={() => {
          if (!disabled) {
            setOpen(true);
          }
        }}
        onClose={() => setOpen(false)}
        open={open}
      >
        <Button onClick={onClick} disabled={disabled}>
          <ButtonIcon tag={buttonIcon} />
        </Button>
      </Tooltip>
    </>
  );
};

Edit ActionButtons

Related answers:

Ryan Cogswell
  • 75,046
  • 9
  • 218
  • 198
  • Hi Ryan. Thanks for the fast answer. I try your code but it still not working :-( – Juliano Costa Apr 18 '21 at 00:29
  • It is working in the sandbox I provided in my answer. Please provide a new sandbox of how you’ve tried to incorporate my answer. – Ryan Cogswell Apr 18 '21 at 00:36
  • Sorry, I made another test and now it is working fine ;-) I don't know what happened at my first try. Here is the sandbox link: https://codesandbox.io/s/actionbuttonspainel-2s8wr?file=/src/index.js Thanks a lot dude – Juliano Costa Apr 18 '21 at 00:58
  • Is there any way to do this for a variable number of tooltips? I have a grid with a button on each row, and the desired tooltip for this button varies from row to row. Your solution works perfectly for 1 single tooltip, but the generic `open` state hook causes all the tooltips to open at once when I hover over any of them... – tprebenda Jul 07 '22 at 18:00