Skip to content

Commit

Permalink
Don't set multiple event listeners
Browse files Browse the repository at this point in the history
Refs #469
  • Loading branch information
ericholscher committed Jan 8, 2025
1 parent b737c51 commit d90f566
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/linkpreviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ function setupTooltip(el, doctoolname, doctoolversion, selector) {
const anchorElement = el;
let relatedTooltip;

el.addEventListener("mouseenter", delayShowTooltip);
el.addEventListener("mouseleave", delayHideTooltip);
if (!el.hasEventListener) {
el.addEventListener("mouseenter", delayShowTooltip);
el.addEventListener("mouseleave", delayHideTooltip);
el.hasEventListener = true;
}

let showTooltipTimeoutId = null;
let hideTooltipTimeoutId = null;
Expand Down Expand Up @@ -153,7 +156,8 @@ function setupTooltip(el, doctoolname, doctoolversion, selector) {
return relatedTooltip;
}

const existingTooltip = anchorElement.parentElement.querySelector(
// Check if there is already a tooltip for this url
const existingTooltip = document.querySelector(
`div[${TOOLTIP_DATA_HREF}="${anchorElement.href}"]`,
);
if (existingTooltip) {
Expand All @@ -171,8 +175,11 @@ function setupTooltip(el, doctoolname, doctoolversion, selector) {
anchorElement.insertAdjacentElement("afterend", newTooltip);
// Let's add event listeners on the tooltip as well, to prevent hiding, when
// mouse moves away from the anchor element
newTooltip.addEventListener("mouseenter", cancelHideDelay);
newTooltip.addEventListener("mouseleave", delayHideTooltip);
if (!newTooltip.hasEventListener) {
newTooltip.addEventListener("mouseenter", cancelHideDelay);
newTooltip.addEventListener("mouseleave", delayHideTooltip);
newTooltip.hasEventListener = true;
}
relatedTooltip = newTooltip;
return newTooltip;
}
Expand Down

0 comments on commit d90f566

Please sign in to comment.