Skip to content

Commit 2dd570d

Browse files
committed
fix(tooltip): handle document touch event in tooltipTarget
1 parent 644a2a9 commit 2dd570d

File tree

3 files changed

+30
-30
lines changed

3 files changed

+30
-30
lines changed

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,14 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
211211
return;
212212
}
213213

214-
this.target.tooltipTarget = this;
215-
216214
this.checkOutletAndOutsideClick();
217215
const shouldReturn = this.preMouseEnterCheck();
218216
if (shouldReturn) {
219217
return;
220218
}
221219

220+
this.target.tooltipTarget = this;
221+
222222
const showingArgs = { target: this, tooltip: this.target, cancel: false };
223223
this.tooltipShow.emit(showingArgs);
224224

@@ -265,7 +265,6 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
265265
return;
266266
}
267267

268-
this.target.tooltipTarget = this;
269268
this.showTooltip();
270269
}
271270

@@ -304,10 +303,18 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
304303
this._overlayDefaults.closeOnOutsideClick = false;
305304
this._overlayDefaults.closeOnEscape = true;
306305

306+
this.target.opening.pipe(takeUntil(this.destroy$)).subscribe(() => {
307+
if (this.target.tooltipTarget === this) {
308+
document.addEventListener('touchstart', this.onDocumentTouchStart = this.onDocumentTouchStart.bind(this), { passive: true });
309+
}
310+
});
311+
307312
this.target.closing.pipe(takeUntil(this.destroy$)).subscribe((event) => {
308313
if (this.target.tooltipTarget !== this) {
309314
return;
310315
}
316+
317+
document.removeEventListener('touchstart', this.onDocumentTouchStart);
311318
const hidingArgs = { target: this, tooltip: this.target, cancel: false };
312319
this.tooltipHide.emit(hidingArgs);
313320

@@ -316,8 +323,11 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
316323
}
317324
});
318325

326+
this.target.forceClosed.pipe(takeUntil(this.destroy$)).subscribe(() => {
327+
document.removeEventListener('touchstart', this.onDocumentTouchStart);
328+
});
329+
319330
this.nativeElement.addEventListener('touchstart', this.onTouchStart = this.onTouchStart.bind(this), { passive: true });
320-
this.target.onDocumentTouchStart = this.onDocumentTouchStart.bind(this);
321331
}
322332

323333
/**
@@ -326,6 +336,7 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
326336
public ngOnDestroy() {
327337
this.hideTooltip();
328338
this.nativeElement.removeEventListener('touchstart', this.onTouchStart);
339+
document.removeEventListener('touchstart', this.onDocumentTouchStart);
329340
this.destroy$.next();
330341
this.destroy$.complete();
331342
}
@@ -340,13 +351,12 @@ export class IgxTooltipTargetDirective extends IgxToggleActionDirective implemen
340351
public showTooltip() {
341352
clearTimeout(this.target.timeoutId);
342353

343-
this.target.tooltipTarget = this;
344-
345354
if (!this.target.collapsed) {
346355
// if close animation has started finish it, or close the tooltip with no animation
347356
this.target.forceClose(this.mergedOverlaySettings);
348357
this.target.toBeHidden = false;
349358
}
359+
this.target.tooltipTarget = this;
350360

351361
const showingArgs = { target: this, tooltip: this.target, cancel: false };
352362
this.tooltipShow.emit(showingArgs);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,8 +572,8 @@ describe('IgxTooltip', () => {
572572
touchElement(dummyDiv);
573573
flush();
574574

575-
expect(targetOne.hideTooltip).not.toHaveBeenCalled();
576-
expect(targetTwo.hideTooltip).toHaveBeenCalledTimes(1);
575+
expect(targetOne.hideTooltip).toHaveBeenCalledTimes(1);
576+
expect(targetTwo.hideTooltip).not.toHaveBeenCalled();
577577
}));
578578

579579
it('should not emit tooltipHide event multiple times', fakeAsync(() => {

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

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import {
22
Directive, ElementRef, Input, ChangeDetectorRef, Optional, HostBinding, Inject,
3-
OnDestroy
3+
Output, EventEmitter
44
} from '@angular/core';
55
import { IgxOverlayService } from '../../services/overlay/overlay';
66
import { OverlaySettings } from '../../services/public_api';
77
import { IgxNavigationService } from '../../core/navigation';
88
import { IgxToggleDirective } from '../toggle/toggle.directive';
9-
import { first, Subject, takeUntil } from 'rxjs';
109

1110
let NEXT_ID = 0;
1211
/**
@@ -28,7 +27,7 @@ let NEXT_ID = 0;
2827
selector: '[igxTooltip]',
2928
standalone: true
3029
})
31-
export class IgxTooltipDirective extends IgxToggleDirective implements OnDestroy {
30+
export class IgxTooltipDirective extends IgxToggleDirective {
3231
/**
3332
* @hidden
3433
*/
@@ -63,6 +62,11 @@ export class IgxTooltipDirective extends IgxToggleDirective implements OnDestroy
6362
@Input()
6463
public context;
6564

65+
/**
66+
* @hidden
67+
*/
68+
@Output() public forceClosed = new EventEmitter<void>();
69+
6670
/**
6771
* Identifier for the tooltip.
6872
* If this is property is not explicitly set, it will be automatically generated.
@@ -104,14 +108,11 @@ export class IgxTooltipDirective extends IgxToggleDirective implements OnDestroy
104108
*/
105109
public toBeShown = false;
106110

107-
/** @hidden */
111+
/**
112+
* @hidden
113+
*/
108114
public tooltipTarget;
109115

110-
/** @hidden */
111-
public onDocumentTouchStart: (event?: Event) => void;
112-
113-
private _destroy$ = new Subject<boolean>();
114-
115116
/** @hidden */
116117
constructor(
117118
elementRef: ElementRef,
@@ -120,19 +121,6 @@ export class IgxTooltipDirective extends IgxToggleDirective implements OnDestroy
120121
@Optional() navigationService: IgxNavigationService) {
121122
// D.P. constructor duplication due to es6 compilation, might be obsolete in the future
122123
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();
136124
}
137125

138126
/**
@@ -176,5 +164,7 @@ export class IgxTooltipDirective extends IgxToggleDirective implements OnDestroy
176164
this.close();
177165
overlaySettings.positionStrategy.settings.closeAnimation = animation;
178166
}
167+
168+
this.forceClosed.emit();
179169
}
180170
}

0 commit comments

Comments
 (0)