Skip to content

feat(tooltip): support touch events #9487

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

Merged
merged 7 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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,4 +1,4 @@
import { getShadowRootNode } from "../../utils/dom";
import { getShadowRootNode, isPrimaryPointerButton } from "../../utils/dom";
import { ReferenceElement } from "../../utils/floating-ui";
import { TOOLTIP_OPEN_DELAY_MS, TOOLTIP_CLOSE_DELAY_MS } from "./resources";
import { getEffectiveReferenceElement } from "./utils";
Expand Down Expand Up @@ -119,9 +119,21 @@ export default class TooltipManager {
}
};

private clickHandler = (event: PointerEvent): void => {
private pointerDownHandler = (event: PointerEvent): void => {
if (!isPrimaryPointerButton(event)) {
return;
}

const clickedTooltip = this.queryTooltip(event.composedPath());

if (clickedTooltip !== this.activeTooltip) {
this.closeActiveTooltip();

if (clickedTooltip) {
this.toggleTooltip(clickedTooltip, true);
}
}

this.clickedTooltip = clickedTooltip;

if (clickedTooltip?.closeOnClick) {
Expand Down Expand Up @@ -151,15 +163,15 @@ export default class TooltipManager {
private addListeners(): void {
window.addEventListener("keydown", this.keyDownHandler, { capture: true });
window.addEventListener("pointermove", this.pointerMoveHandler, { capture: true });
window.addEventListener("click", this.clickHandler, { capture: true });
window.addEventListener("pointerdown", this.pointerDownHandler, { capture: true });
window.addEventListener("focusin", this.focusInHandler, { capture: true });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should stay as a click since it does fire on mobile as well (see example). Also, pointerdown does not share the same context.

Alternatively, this could use pointerdown along with pointerup to prep for #9506.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done! had to remove a test which is no longer appropriate.

window.addEventListener("focusout", this.focusOutHandler, { capture: true });
}

private removeListeners(): void {
window.removeEventListener("keydown", this.keyDownHandler, { capture: true });
window.removeEventListener("pointermove", this.pointerMoveHandler, { capture: true });
window.removeEventListener("click", this.clickHandler, { capture: true });
window.removeEventListener("pointerdown", this.pointerDownHandler, { capture: true });
window.removeEventListener("focusin", this.focusInHandler, { capture: true });
window.removeEventListener("focusout", this.focusOutHandler, { capture: true });
}
Expand Down
70 changes: 70 additions & 0 deletions packages/calcite-components/src/components/tooltip/tooltip.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,76 @@ describe("calcite-tooltip", () => {
expect(await tooltip.getProperty("open")).toBe(false);
});

it("should toggle tooltip on pointerdown for touch events", async () => {
const page = await newE2EPage();

await page.setContent(html`
<button id="test">test</button>
<calcite-tooltip id="tooltip" reference-element="ref">Content</calcite-tooltip>
<div tabindex="0" id="ref">Button</div>
`);

await page.waitForChanges();

const tooltip = await page.find("calcite-tooltip");

expect(await tooltip.getProperty("open")).toBe(false);

await page.evaluate(() => {
const ref = document.getElementById("ref");
const event1 = new PointerEvent("pointerdown", {
view: window,
cancelable: true,
isPrimary: true,
clientX: 100,
clientY: 100,
bubbles: true,
button: 0,
});
ref.dispatchEvent(event1);
});

await page.waitForChanges();

expect(await tooltip.getProperty("open")).toBe(true);

await page.evaluate(() => {
const ref = document.getElementById("ref");
const event1 = new PointerEvent("pointerdown", {
view: window,
cancelable: true,
isPrimary: true,
clientX: 100,
clientY: 100,
bubbles: true,
button: 0,
});
ref.dispatchEvent(event1);
});

await page.waitForChanges();

expect(await tooltip.getProperty("open")).toBe(true);

await page.evaluate(() => {
const ref = document.getElementById("test");
const event1 = new PointerEvent("pointerdown", {
view: window,
cancelable: true,
isPrimary: true,
clientX: 100,
clientY: 100,
bubbles: true,
button: 0,
});
ref.dispatchEvent(event1);
});

await page.waitForChanges();

expect(await tooltip.getProperty("open")).toBe(false);
});

it("should not open tooltip when clicked", async () => {
const page = await newE2EPage();

Expand Down
Loading