Skip to content

Commit 0366397

Browse files
committed
fix(tooltip): not closing when scrolling away
Fixes the tooltip not being closed when scrolling away due to the `onPositionChange` being executed outside the `NgZone` after some changes to the `ScrollDispatcher`.
1 parent 1a7a61a commit 0366397

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/lib/tooltip/tooltip.spec.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import {
1111
Component,
1212
DebugElement,
1313
ElementRef,
14-
ViewChild
14+
ViewChild,
15+
NgZone,
1516
} from '@angular/core';
1617
import {AnimationEvent} from '@angular/animations';
1718
import {By} from '@angular/platform-browser';
@@ -609,6 +610,26 @@ describe('MatTooltip', () => {
609610
expect(tooltipDirective._isTooltipVisible())
610611
.toBe(false, 'Expected tooltip hidden when scrolled out of view, after throttle limit');
611612
}));
613+
614+
it('should execute the `hide` call, after scrolling away, inside the NgZone', fakeAsync(() => {
615+
const inZoneSpy = jasmine.createSpy('in zone spy');
616+
617+
tooltipDirective.show();
618+
fixture.detectChanges();
619+
tick(0);
620+
621+
spyOn(tooltipDirective._tooltipInstance!, 'hide').and.callFake(() => {
622+
inZoneSpy(NgZone.isInAngularZone());
623+
});
624+
625+
fixture.componentInstance.scrollDown();
626+
tick(100);
627+
fixture.detectChanges();
628+
629+
expect(inZoneSpy).toHaveBeenCalled();
630+
expect(inZoneSpy).toHaveBeenCalledWith(true);
631+
}));
632+
612633
});
613634

614635
describe('with OnPush', () => {

src/lib/tooltip/tooltip.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ export class MatTooltip implements OnDestroy {
292292
if (change.scrollableViewProperties.isOverlayClipped && this._tooltipInstance.isVisible()) {
293293
// After position changes occur and the overlay is clipped by
294294
// a parent scrollable then close the tooltip.
295-
this.hide(0);
295+
this._ngZone.run(() => this.hide(0));
296296
} else {
297297
// Otherwise recalculate the origin based on the new position.
298298
this._tooltipInstance._setTransformOrigin(change.connectionPair);

0 commit comments

Comments
 (0)