Skip to content

[FEAT REQ] Support combination of hover and openOnClick #1283

Description

@mdmower-csnw

Problem to solve

I use dual tooltip components (see "Alternatives considered" below) to achieve a goal of showing the tooltip on hover and letting a click activate the tooltip that keeps it open even when the mouse leaves the tooltip anchor. It's a bit crude because it really does result in two tooltips overlaying (exactly) for the on-click open. You can tell because the background opacity of the two stacked tooltips is less transparent.

Proposed solution

It would be great if a single tooltip component could be configured to show both on hover and on click, where the latter would suppress closing the tooltip when the mouse leaves the anchor.

Alternatives considered

Dual tooltip components applied to the same anchor:

<Tooltip anchorSelect={`#${CSS.escape(id)}`} place="bottom" className="z-top" openOnClick>
  {children}
</Tooltip>
<Tooltip anchorSelect={`#${CSS.escape(id)}`} place="bottom" className="z-top">
  {children}
</Tooltip>

Dynamically modifying close events, but I'm a little unsure of the safety of modifying close events while the tooltip is already open via hover:

export default function QuestionTooltip({id, className, children}: Props) {
  const [is_pinned, setIsPinned] = useState(false);
  return (
    <>
      <Icon.Help
        id={id}
        size={3}
        color="gray"
        className={className}
        aria-label="Tip"
        role="button"
        tabIndex={0}
        onClick={() => setIsPinned(true)}
      />
      <Tooltip
        anchorSelect={`#${CSS.escape(id)}`}
        place="bottom"
        className="z-top"
        openEvents={{mouseenter: true, focus: true, click: true}}
        closeEvents={is_pinned ? {} : {mouseleave: true, blur: true}}
        globalCloseEvents={{clickOutsideAnchor: true, escape: true}}
        afterHide={() => setIsPinned(false)}
      >
        {children}
      </Tooltip>
    </>
  );
}

Example usage

Additional context

Somewhat related request that didn't receive any comments: #647. Another tangentially related feature request that describes a past behavior where hover and on-click could both be applied: #946.

Checks

  • I searched existing issues and did not find an open match.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions