Skip to content

Commit

Permalink
fix(components): delayed tooltip shown even after blur event (#4053) (#…
Browse files Browse the repository at this point in the history
…4103)

cherry pick from main
  • Loading branch information
leagrdv authored Nov 29, 2024
1 parent 102e717 commit e9cf591
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 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.
43 changes: 30 additions & 13 deletions packages/components/src/components/post-tooltip/post-tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { version } from '@root/package.json';
import isFocusable from 'ally.js/is/focusable';
import 'long-press-event';
import { getAttributeObserver } from '@/utils/attribute-observer';
import { checkEmptyOrType, timeout } from '@/utils';
import { checkEmptyOrType } from '@/utils';

const OPEN_DELAY = 650; // matches HTML title delay

Expand All @@ -19,6 +19,8 @@ let tooltipInstances = 0;
let hideTooltipTimeout: number = null;
const tooltipTargetAttribute = 'data-tooltip-target';
const tooltipTargetAttributeSelector = `[${tooltipTargetAttribute}]`;
let globalCurrentTarget: HTMLElement;
let tooltipTimeout = null;

/**
* Global event listener to show tooltips. This is globalized so that triggers that are rendered
Expand All @@ -34,7 +36,11 @@ const globalInterestHandler = (e: PointerEvent | FocusEvent) => {
const targetElement = (e.target as HTMLElement).closest(
tooltipTargetAttributeSelector,
) as HTMLElement;
if (!targetElement || !('getAttribute' in targetElement)) return;
globalCurrentTarget = targetElement;
if (!targetElement || !('getAttribute' in targetElement)) {
clearTimeout(tooltipTimeout);
return;
}
const tooltipTarget = targetElement.getAttribute(tooltipTargetAttribute);
if (!tooltipTarget || tooltipTarget === '') return;
const tooltip = document.getElementById(tooltipTarget) as HTMLPostTooltipElement;
Expand Down Expand Up @@ -199,19 +205,30 @@ export class PostTooltip {
*/
@Method()
async show(target: HTMLElement, triggeredByFocus = false) {
if (this.delayed) await timeout(OPEN_DELAY);

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

// Disable pointer events if triggered by focus, otherwise enable them
if (this.wasOpenedByFocus) {
this.host.style.pointerEvents = 'none';
const showTooltip = () => {
// 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;

// Disable pointer events if triggered by focus, otherwise enable them
if (this.wasOpenedByFocus) {
this.host.style.pointerEvents = 'none';
} else {
this.host.style.pointerEvents = 'auto';
}

this.popoverRef.show(target);
};

if (this.delayed) {
tooltipTimeout = setTimeout(() => {
showTooltip();
}, OPEN_DELAY);
} else {
this.host.style.pointerEvents = 'auto';
showTooltip();
}

this.popoverRef.show(target);
}

/**
Expand Down

0 comments on commit e9cf591

Please sign in to comment.