Skip to content

Commit

Permalink
refactor(tooltip): no longer use capture events. (#9839)
Browse files Browse the repository at this point in the history
**Related Issue:** None

## Summary

- Update `TooltipManager` to no longer use capture events.
- preventDefault() on escape key pressed from a referenceElement or
active tooltip.
  • Loading branch information
driskull authored Oct 18, 2024
1 parent 17b958c commit 0e1bead
Showing 1 changed file with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,13 @@ export default class TooltipManager {
if (activeTooltip?.open) {
this.clearHoverTimeout();
this.closeActiveTooltip();

const referenceElement = getEffectiveReferenceElement(activeTooltip);
const composedPath = event.composedPath();

if (referenceElement instanceof Element && referenceElement.contains(event.target as HTMLElement)) {
if (
(referenceElement instanceof Element && composedPath.includes(referenceElement)) ||
composedPath.includes(activeTooltip)
) {
event.preventDefault();
}
}
Expand Down Expand Up @@ -173,26 +176,26 @@ export default class TooltipManager {
};

private addShadowListeners(shadowRoot: ShadowRoot): void {
shadowRoot.addEventListener("focusin", this.focusInHandler, { capture: true });
shadowRoot.addEventListener("focusin", this.focusInHandler);
}

private removeShadowListeners(shadowRoot: ShadowRoot): void {
shadowRoot.removeEventListener("focusin", this.focusInHandler, { capture: true });
shadowRoot.removeEventListener("focusin", this.focusInHandler);
}

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("focusin", this.focusInHandler, { capture: true });
window.addEventListener("keydown", this.keyDownHandler);
window.addEventListener("pointermove", this.pointerMoveHandler);
window.addEventListener("click", this.clickHandler);
window.addEventListener("focusin", this.focusInHandler);
window.addEventListener("blur", this.blurHandler);
}

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("focusin", this.focusInHandler, { capture: true });
window.removeEventListener("keydown", this.keyDownHandler);
window.removeEventListener("pointermove", this.pointerMoveHandler);
window.removeEventListener("click", this.clickHandler);
window.removeEventListener("focusin", this.focusInHandler);
window.removeEventListener("blur", this.blurHandler);
}

Expand Down

0 comments on commit 0e1bead

Please sign in to comment.