Skip to content

fix(tooltip): hiding and reopening for consecutive show calls #13326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/lib/tooltip/tooltip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,26 @@ describe('MatTooltip', () => {
expect(overlayContainerElement.querySelector('.mat-tooltip')).toBeNull();
}));

it('should not hide the tooltip when calling `show` twice in a row', fakeAsync(() => {
tooltipDirective.show();
tick(0);
expect(tooltipDirective._isTooltipVisible()).toBe(true);
fixture.detectChanges();
tick(500);

const overlayRef = tooltipDirective._overlayRef!;

spyOn(overlayRef, 'detach').and.callThrough();

tooltipDirective.show();
tick(0);
expect(tooltipDirective._isTooltipVisible()).toBe(true);
fixture.detectChanges();
tick(500);

expect(overlayRef.detach).not.toHaveBeenCalled();
}));

});

describe('fallback positions', () => {
Expand Down
13 changes: 10 additions & 3 deletions src/lib/tooltip/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,10 @@ export class MatTooltip implements OnDestroy {

/** Shows the tooltip after the delay in ms, defaults to tooltip-delay-show or 0ms if no input */
show(delay: number = this.showDelay): void {
if (this.disabled || !this.message) { return; }
if (this.disabled || !this.message || (this._isTooltipVisible() &&
!this._tooltipInstance!._showTimeoutId && !this._tooltipInstance!._hideTimeoutId)) {
return;
}

const overlayRef = this._createOverlay();

Expand Down Expand Up @@ -524,10 +527,10 @@ export class TooltipComponent {
tooltipClass: string|string[]|Set<string>|{[key: string]: any};

/** The timeout ID of any current timer set to show the tooltip */
_showTimeoutId: number;
_showTimeoutId: number | null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: you could use zero (so that you don't need the | null) since setTimeout always returns a positive integer.


/** The timeout ID of any current timer set to hide the tooltip */
_hideTimeoutId: number;
_hideTimeoutId: number | null;

/** Property watched by the animation framework to show or hide the tooltip */
_visibility: TooltipVisibility = 'initial';
Expand All @@ -553,12 +556,14 @@ export class TooltipComponent {
// Cancel the delayed hide if it is scheduled
if (this._hideTimeoutId) {
clearTimeout(this._hideTimeoutId);
this._hideTimeoutId = null;
}

// Body interactions should cancel the tooltip if there is a delay in showing.
this._closeOnInteraction = true;
this._showTimeoutId = setTimeout(() => {
this._visibility = 'visible';
this._showTimeoutId = null;

// Mark for check so if any parent component has set the
// ChangeDetectionStrategy to OnPush it will be checked anyways
Expand All @@ -574,10 +579,12 @@ export class TooltipComponent {
// Cancel the delayed show if it is scheduled
if (this._showTimeoutId) {
clearTimeout(this._showTimeoutId);
this._showTimeoutId = null;
}

this._hideTimeoutId = setTimeout(() => {
this._visibility = 'hidden';
this._hideTimeoutId = null;

// Mark for check so if any parent component has set the
// ChangeDetectionStrategy to OnPush it will be checked anyways
Expand Down