Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tooltip): do not open after the pointer has moved off of the reference element #11599

Draft
wants to merge 6 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @ts-strict-ignore
import { getShadowRootNode } from "../../utils/dom";
import { ReferenceElement } from "../../utils/floating-ui";
import { TOOLTIP_OPEN_DELAY_MS, TOOLTIP_CLOSE_DELAY_MS } from "./resources";
import { TOOLTIP_OPEN_DELAY_MS, TOOLTIP_QUICK_OPEN_DELAY_MS, TOOLTIP_CLOSE_DELAY_MS } from "./resources";
import { getEffectiveReferenceElement } from "./utils";
import type { Tooltip } from "./tooltip";

Expand All @@ -26,6 +26,8 @@ export default class TooltipManager {

private clickedTooltip: Tooltip["el"] = null;

private hoveredTooltip: Tooltip["el"] = null;

// --------------------------------------------------------------------------
//
// Public Methods
Expand Down Expand Up @@ -116,6 +118,12 @@ export default class TooltipManager {
return;
}

if (tooltip !== this.hoveredTooltip) {
this.clearHoverOpenTimeout();
}

this.hoveredTooltip = tooltip;

if (tooltip) {
this.openHoveredTooltip(tooltip);
} else if (activeTooltip?.open) {
Expand Down Expand Up @@ -266,15 +274,15 @@ export default class TooltipManager {
private openHoveredTooltip = (tooltip: Tooltip["el"]): void => {
this.hoverOpenTimeout = window.setTimeout(
() => {
if (this.hoverOpenTimeout === null) {
if (this.hoverOpenTimeout === null || tooltip !== this.hoveredTooltip) {
return;
}

this.clearHoverCloseTimeout();
this.closeTooltipIfNotActive(tooltip);
this.toggleTooltip(tooltip, true);
},
this.activeTooltip?.open ? 0 : TOOLTIP_OPEN_DELAY_MS,
this.activeTooltip?.open ? TOOLTIP_QUICK_OPEN_DELAY_MS : TOOLTIP_OPEN_DELAY_MS,
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const CSS = {
};

export const TOOLTIP_OPEN_DELAY_MS = 300;
export const TOOLTIP_CLOSE_DELAY_MS = 500;
export const TOOLTIP_QUICK_OPEN_DELAY_MS = TOOLTIP_OPEN_DELAY_MS / 3;
export const TOOLTIP_CLOSE_DELAY_MS = TOOLTIP_OPEN_DELAY_MS * 1.5;

export const ARIA_DESCRIBED_BY = "aria-describedby";
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { accessible, defaults, floatingUIOwner, hidden, openClose, renders, them
import { html } from "../../../support/formatting";
import { getElementXY, GlobalTestProps, skipAnimations } from "../../tests/utils";
import { FloatingCSS } from "../../utils/floating-ui";
import { TOOLTIP_OPEN_DELAY_MS, TOOLTIP_CLOSE_DELAY_MS, CSS } from "./resources";
import { TOOLTIP_OPEN_DELAY_MS, TOOLTIP_CLOSE_DELAY_MS, CSS, TOOLTIP_QUICK_OPEN_DELAY_MS } from "./resources";
import type { Tooltip } from "./tooltip";

interface PointerMoveOptions {
Expand Down Expand Up @@ -1236,7 +1236,7 @@ describe("calcite-tooltip", () => {
});
});

it("should open tooltip instantly if another tooltip is already visible", async () => {
it("should open tooltip faster if another tooltip is already visible", async () => {
const page = await newE2EPage();

await page.setContent(
Expand All @@ -1260,7 +1260,7 @@ describe("calcite-tooltip", () => {
expect(await tooltip2.getProperty("open")).toBe(false);

await dispatchPointerEvent(page, "#ref2");
await page.waitForTimeout(0);
await page.waitForTimeout(TOOLTIP_QUICK_OPEN_DELAY_MS);
expect(await tooltip1.getProperty("open")).toBe(false);
expect(await tooltip2.getProperty("open")).toBe(true);
});
Expand Down