Skip to content

Commit a20faee

Browse files
committed
fix(tooltip): make touch events passive
1 parent 4b2f46e commit a20faee

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

projects/igniteui-angular/src/lib/directives/tooltip/tooltip-target.directive.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,26 +258,27 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
258258
/**
259259
* @hidden
260260
*/
261-
@HostListener('touchstart')
262261
public onTouchStart() {
263262
if (this.tooltipDisabled) {
264263
return;
265264
}
266265

266+
this.target.tooltipTarget = this;
267267
this.showTooltip();
268268
}
269269

270270
/**
271271
* @hidden
272272
*/
273-
@HostListener('document:touchstart', ['$event'])
274273
public onDocumentTouchStart(event) {
275274
if (this.tooltipDisabled) {
276275
return;
277276
}
278277

279-
if (this.nativeElement !== event.target &&
280-
!this.nativeElement.contains(event.target)
278+
const tooltipTarget = this.target.tooltipTarget.nativeElement;
279+
280+
if (tooltipTarget !== event.target &&
281+
!tooltipTarget.contains(event.target)
281282
) {
282283
this.hideTooltip();
283284
}
@@ -308,13 +309,17 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
308309
event.cancel = true;
309310
}
310311
});
312+
313+
this.nativeElement.addEventListener('touchstart', this.onTouchStart.bind(this), { passive: true });
314+
this.target.onDocumentTouchStart = this.onDocumentTouchStart.bind(this);
311315
}
312316

313317
/**
314318
* @hidden
315319
*/
316320
public ngOnDestroy() {
317321
this.hideTooltip();
322+
this.nativeElement.removeEventListener('touchstart', this.onTouchStart);
318323
this.destroy$.next();
319324
this.destroy$.complete();
320325
}

projects/igniteui-angular/src/lib/directives/tooltip/tooltip.directive.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import {
2-
Directive, ElementRef, Input, ChangeDetectorRef, Optional, HostBinding, Inject
2+
Directive, ElementRef, Input, ChangeDetectorRef, Optional, HostBinding, Inject,
3+
OnDestroy
34
} from '@angular/core';
45
import { IgxOverlayService } from '../../services/overlay/overlay';
56
import { OverlaySettings } from '../../services/public_api';
67
import { IgxNavigationService } from '../../core/navigation';
78
import { IgxToggleDirective } from '../toggle/toggle.directive';
9+
import { first, Subject, takeUntil } from 'rxjs';
810

911
let NEXT_ID = 0;
1012
/**
@@ -26,7 +28,7 @@ let NEXT_ID = 0;
2628
selector: '[igxTooltip]',
2729
standalone: true
2830
})
29-
export class IgxTooltipDirective extends IgxToggleDirective {
31+
export class IgxTooltipDirective extends IgxToggleDirective implements OnDestroy {
3032
/**
3133
* @hidden
3234
*/
@@ -102,6 +104,14 @@ export class IgxTooltipDirective extends IgxToggleDirective {
102104
*/
103105
public toBeShown = false;
104106

107+
/** @hidden */
108+
public tooltipTarget;
109+
110+
/** @hidden */
111+
public onDocumentTouchStart: (event?: Event) => void;
112+
113+
private _destroy$ = new Subject<boolean>();
114+
105115
/** @hidden */
106116
constructor(
107117
elementRef: ElementRef,
@@ -110,6 +120,19 @@ export class IgxTooltipDirective extends IgxToggleDirective {
110120
@Optional() navigationService: IgxNavigationService) {
111121
// D.P. constructor duplication due to es6 compilation, might be obsolete in the future
112122
super(elementRef, cdr, overlayService, navigationService);
123+
124+
this.opening.pipe(first(), takeUntil(this._destroy$)).subscribe(() => {
125+
document.addEventListener('touchstart', this.onDocumentTouchStart, { passive: true });
126+
});
127+
}
128+
129+
/** @hidden */
130+
public override ngOnDestroy() {
131+
super.ngOnDestroy();
132+
133+
document.removeEventListener('touchstart', this.onDocumentTouchStart);
134+
this._destroy$.next(true);
135+
this._destroy$.complete();
113136
}
114137

115138
/**

0 commit comments

Comments
 (0)