From d5a2fca336219a7a3f11570f6853b0849f35cb8c Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Fri, 8 Dec 2017 18:53:49 +0100 Subject: [PATCH] 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`. --- src/lib/tooltip/tooltip.spec.ts | 23 ++++++++++++++++++++++- src/lib/tooltip/tooltip.ts | 2 +- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/lib/tooltip/tooltip.spec.ts b/src/lib/tooltip/tooltip.spec.ts index c117bd17f53..4a3d220ebd4 100644 --- a/src/lib/tooltip/tooltip.spec.ts +++ b/src/lib/tooltip/tooltip.spec.ts @@ -12,7 +12,8 @@ import { Component, DebugElement, ElementRef, - ViewChild + ViewChild, + NgZone, } from '@angular/core'; import {AnimationEvent} from '@angular/animations'; import {By} from '@angular/platform-browser'; @@ -628,6 +629,26 @@ describe('MatTooltip', () => { expect(tooltipDirective._isTooltipVisible()) .toBe(false, 'Expected tooltip hidden when scrolled out of view, after throttle limit'); })); + + it('should execute the `hide` call, after scrolling away, inside the NgZone', fakeAsync(() => { + const inZoneSpy = jasmine.createSpy('in zone spy'); + + tooltipDirective.show(); + fixture.detectChanges(); + tick(0); + + spyOn(tooltipDirective._tooltipInstance!, 'hide').and.callFake(() => { + inZoneSpy(NgZone.isInAngularZone()); + }); + + fixture.componentInstance.scrollDown(); + tick(100); + fixture.detectChanges(); + + expect(inZoneSpy).toHaveBeenCalled(); + expect(inZoneSpy).toHaveBeenCalledWith(true); + })); + }); describe('with OnPush', () => { diff --git a/src/lib/tooltip/tooltip.ts b/src/lib/tooltip/tooltip.ts index d0cc5c1e6f6..7788b03b105 100644 --- a/src/lib/tooltip/tooltip.ts +++ b/src/lib/tooltip/tooltip.ts @@ -307,7 +307,7 @@ export class MatTooltip implements OnDestroy { if (change.scrollableViewProperties.isOverlayClipped && this._tooltipInstance.isVisible()) { // After position changes occur and the overlay is clipped by // a parent scrollable then close the tooltip. - this.hide(0); + this._ngZone.run(() => this.hide(0)); } else { // Otherwise recalculate the origin based on the new position. this._tooltipInstance._setTransformOrigin(change.connectionPair);