Skip to content

Commit d5a2fca

Browse files
crisbetommalerba
authored andcommitted
fix(tooltip): not closing when scrolling away (#8688)
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 0d2a419 commit d5a2fca

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
@@ -12,7 +12,8 @@ import {
1212
Component,
1313
DebugElement,
1414
ElementRef,
15-
ViewChild
15+
ViewChild,
16+
NgZone,
1617
} from '@angular/core';
1718
import {AnimationEvent} from '@angular/animations';
1819
import {By} from '@angular/platform-browser';
@@ -628,6 +629,26 @@ describe('MatTooltip', () => {
628629
expect(tooltipDirective._isTooltipVisible())
629630
.toBe(false, 'Expected tooltip hidden when scrolled out of view, after throttle limit');
630631
}));
632+
633+
it('should execute the `hide` call, after scrolling away, inside the NgZone', fakeAsync(() => {
634+
const inZoneSpy = jasmine.createSpy('in zone spy');
635+
636+
tooltipDirective.show();
637+
fixture.detectChanges();
638+
tick(0);
639+
640+
spyOn(tooltipDirective._tooltipInstance!, 'hide').and.callFake(() => {
641+
inZoneSpy(NgZone.isInAngularZone());
642+
});
643+
644+
fixture.componentInstance.scrollDown();
645+
tick(100);
646+
fixture.detectChanges();
647+
648+
expect(inZoneSpy).toHaveBeenCalled();
649+
expect(inZoneSpy).toHaveBeenCalledWith(true);
650+
}));
651+
631652
});
632653

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

src/lib/tooltip/tooltip.ts

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

0 commit comments

Comments
 (0)