Skip to content

Commit

Permalink
fix(components): delayed tooltip shown even after blur event
Browse files Browse the repository at this point in the history
  • Loading branch information
leagrdv committed Nov 22, 2024
1 parent de229aa commit 740b7ee
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-windows-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@swisspost/design-system-components': patch
---

Fixed bug that showed delayed tooltip even after blur event.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ let tooltipInstances = 0;
let hideTooltipTimeout: number = null;
const tooltipTargetAttribute = 'data-tooltip-target';
const tooltipTargetAttributeSelector = `[${tooltipTargetAttribute}]`;
let globalCurrentTarget: HTMLElement;

/**
* Global event listener to show tooltips. This is globalized so that triggers that are rendered
Expand All @@ -34,6 +35,7 @@ const globalInterestHandler = (e: PointerEvent | FocusEvent) => {
const targetElement = (e.target as HTMLElement).closest(
tooltipTargetAttributeSelector,
) as HTMLElement;
globalCurrentTarget = targetElement;
if (!targetElement || !('getAttribute' in targetElement)) return;
const tooltipTarget = targetElement.getAttribute(tooltipTargetAttribute);
if (!tooltipTarget || tooltipTarget === '') return;
Expand Down Expand Up @@ -201,6 +203,9 @@ export class PostTooltip {
async show(target: HTMLElement, triggeredByFocus = false) {
if (this.delayed) await timeout(OPEN_DELAY);

// If focus or pointer event is not on the button anymore, don't show the tooltip
if (globalCurrentTarget !== target) return;

// Determine if the tooltip was opened by a focus event
this.wasOpenedByFocus = triggeredByFocus;

Expand Down

0 comments on commit 740b7ee

Please sign in to comment.