Skip to content

Commit

Permalink
feat(tooltip): show tooltip on longpress; remove delay on mouseleave (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewseguin authored and kara committed Nov 16, 2016
1 parent d7a54ef commit 1552d70
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
18 changes: 12 additions & 6 deletions src/lib/tooltip/tooltip.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import {
async, ComponentFixture, TestBed, tick, fakeAsync,
async,
ComponentFixture,
TestBed,
tick,
fakeAsync,
flushMicrotasks
} from '@angular/core/testing';
import {Component, DebugElement, AnimationTransitionEvent} from '@angular/core';
import {By} from '@angular/platform-browser';
import {TooltipPosition, MdTooltip, TOOLTIP_HIDE_DELAY, MdTooltipModule} from './tooltip';
import {TooltipPosition, MdTooltip, MdTooltipModule} from './tooltip';
import {OverlayContainer} from '../core';

const initialTooltipMessage = 'initial tooltip message';
Expand Down Expand Up @@ -52,11 +56,12 @@ describe('MdTooltip', () => {
expect(overlayContainerElement.textContent).toContain(initialTooltipMessage);

// After hide called, a timeout delay is created that will to hide the tooltip.
tooltipDirective.hide();
const tooltipDelay = 1000;
tooltipDirective.hide(tooltipDelay);
expect(tooltipDirective._isTooltipVisible()).toBe(true);

// After the tooltip delay elapses, expect that the tooltip is not visible.
tick(TOOLTIP_HIDE_DELAY);
tick(tooltipDelay);
fixture.detectChanges();
expect(tooltipDirective._isTooltipVisible()).toBe(false);

Expand All @@ -70,12 +75,13 @@ describe('MdTooltip', () => {
expect(tooltipDirective._isTooltipVisible()).toBe(true);

// After hide called, a timeout delay is created that will to hide the tooltip.
tooltipDirective.hide();
const tooltipDelay = 1000;
tooltipDirective.hide(tooltipDelay);
expect(tooltipDirective._isTooltipVisible()).toBe(true);

// Before delay time has passed, call show which should cancel intent to hide tooltip.
tooltipDirective.show();
tick(TOOLTIP_HIDE_DELAY);
tick(tooltipDelay);
expect(tooltipDirective._isTooltipVisible()).toBe(true);
}));

Expand Down
12 changes: 5 additions & 7 deletions src/lib/tooltip/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {Subject} from 'rxjs/Subject';
export type TooltipPosition = 'before' | 'after' | 'above' | 'below';

/** Time in ms to delay before changing the tooltip visibility to hidden */
export const TOOLTIP_HIDE_DELAY = 1500;
export const TOUCHEND_HIDE_DELAY = 1500;

/**
* Directive that attaches a material design tooltip to the host element. Animates the showing and
Expand All @@ -41,6 +41,8 @@ export const TOOLTIP_HIDE_DELAY = 1500;
@Directive({
selector: '[md-tooltip]',
host: {
'(longpress)': 'show()',
'(touchend)': 'hide(' + TOUCHEND_HIDE_DELAY + ')',
'(mouseenter)': 'show()',
'(mouseleave)': 'hide()',
},
Expand Down Expand Up @@ -100,12 +102,8 @@ export class MdTooltip {
this._tooltipInstance.show(this._position);
}

/**
* Create the overlay config and position strategy
* Hides the tooltip after the provided delay in ms. Defaults the delay to the material design
* prescribed delay time
*/
hide(delay: number = TOOLTIP_HIDE_DELAY): void {
/** Hides the tooltip after the provided delay in ms, defaulting to 0ms. */
hide(delay = 0): void {
if (this._tooltipInstance) {
this._tooltipInstance.hide(delay);
}
Expand Down

0 comments on commit 1552d70

Please sign in to comment.