Skip to content

Commit b4d733a

Browse files
committed
feat(tooltip): init add animations
1 parent 04e2201 commit b4d733a

File tree

5 files changed

+267
-133
lines changed

5 files changed

+267
-133
lines changed
Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
<div class="demo-tooltip">
22
<h1>Tooltip Demo</h1>
3+
34
<p class="centered">
4-
<button
5+
<button #tooltip="mdTooltip"
56
md-raised-button
67
color="primary"
7-
md-tooltip="to see a tooltip"
8+
[md-tooltip]="message"
89
[tooltip-position]="position">
9-
Mouse over this link
10+
Mouse over to see the tooltip
1011
</button>
1112
</p>
13+
1214
<p>
1315
<md-radio-group [(ngModel)]="position">
1416
<md-radio-button value="below">Below</md-radio-button>
@@ -17,4 +19,20 @@ <h1>Tooltip Demo</h1>
1719
<md-radio-button value="after">After</md-radio-button>
1820
</md-radio-group>
1921
</p>
22+
23+
<p>
24+
<strong>Message: </strong>
25+
<md-input type="text" [(ngModel)]="message"></md-input>
26+
</p>
27+
28+
<strong>Mouse over to</strong>
29+
<button md-raised-button color="primary" (mouseenter)="tooltip.show()">
30+
Show tooltip
31+
</button>
32+
<button md-raised-button color="primary" (mouseenter)="tooltip.hide(0)">
33+
Hide tooltip
34+
</button>
35+
<button md-raised-button color="primary" (mouseenter)="tooltip.toggle()">
36+
Toggle tooltip
37+
</button>
2038
</div>

src/demo-app/tooltip/tooltip-demo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ import {TooltipPosition} from '@angular/material';
1010
})
1111
export class TooltipDemo {
1212
position: TooltipPosition = 'below';
13+
message: string = 'Here is the tooltip';
1314
}

src/lib/tooltip/tooltip.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<div class="md-tooltip"
2+
[style.transform-origin]="_transformOrigin"
3+
[@state]="_visibility"
4+
(@state.done)="this._afterVisibilityAnimation($event)">
5+
{{message}}
6+
</div>

src/lib/tooltip/tooltip.spec.ts

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
1+
import {async, ComponentFixture, TestBed, tick, fakeAsync} from '@angular/core/testing';
22
import {Component, DebugElement} from '@angular/core';
33
import {By} from '@angular/platform-browser';
4-
import {TooltipPosition, MdTooltip} from './tooltip';
4+
import {TooltipPosition, MdTooltip, MATERIAL_TOOLTIP_HIDE_DELAY} from './tooltip';
55
import {OverlayContainer} from '../core';
66
import {MdTooltipModule} from './tooltip';
77

8+
const initialTooltipMessage = 'initial tooltip message';
89

9-
describe('MdTooltip', () => {
10+
fdescribe('MdTooltip', () => {
1011
let overlayContainerElement: HTMLElement;
1112

13+
1214
beforeEach(async(() => {
1315
TestBed.configureTestingModule({
1416
imports: [MdTooltipModule.forRoot()],
@@ -38,25 +40,66 @@ describe('MdTooltip', () => {
3840
tooltipDirective = buttonDebugElement.injector.get(MdTooltip);
3941
});
4042

41-
it('should show/hide on mouse enter/leave', () => {
42-
expect(tooltipDirective.visible).toBeFalsy();
43+
it('should show and hide the tooltip', fakeAsync(() => {
44+
expect(tooltipDirective._tooltipRef).toBeUndefined();
4345

44-
tooltipDirective._handleMouseEnter(null);
45-
expect(tooltipDirective.visible).toBeTruthy();
46+
tooltipDirective.show();
47+
expect(tooltipDirective._isTooltipVisible()).toBe(true);
4648

4749
fixture.detectChanges();
48-
expect(overlayContainerElement.textContent).toBe('some message');
50+
expect(overlayContainerElement.textContent).toContain(initialTooltipMessage);
51+
52+
tooltipDirective.hide();
53+
expect(tooltipDirective._isTooltipVisible()).toBe(true);
54+
55+
// After hidden, expect that the tooltip is not visible.
56+
tick(MATERIAL_TOOLTIP_HIDE_DELAY);
57+
expect(tooltipDirective._isTooltipVisible()).toBe(false);
58+
}));
59+
60+
it('should remove the tooltip when changing position', () => {
61+
const initialPosition: TooltipPosition = 'below';
62+
const changedPosition: TooltipPosition = 'above';
4963

50-
tooltipDirective._handleMouseLeave(null);
51-
expect(overlayContainerElement.textContent).toBe('');
64+
expect(tooltipDirective._tooltipRef).toBeUndefined();
65+
66+
tooltipDirective.position = initialPosition;
67+
tooltipDirective.show();
68+
expect(tooltipDirective._tooltipRef).toBeDefined();
69+
70+
// Same position value should not remove the tooltip
71+
tooltipDirective.position = initialPosition;
72+
expect(tooltipDirective._tooltipRef).toBeDefined();
73+
74+
// Different position value should destroy the tooltip
75+
tooltipDirective.position = changedPosition;
76+
expect(tooltipDirective._tooltipRef).toBeNull();
77+
expect(tooltipDirective._overlayRef).toBeNull();
78+
});
79+
80+
it('should be able to modify the tooltip message', () => {
81+
expect(tooltipDirective._tooltipRef).toBeUndefined();
82+
83+
tooltipDirective.show();
84+
expect(tooltipDirective._tooltipRef._visibility).toBe('visible');
85+
86+
fixture.detectChanges();
87+
expect(overlayContainerElement.textContent).toContain(initialTooltipMessage);
88+
89+
const newMessage = 'new tooltip message';
90+
tooltipDirective.message = newMessage;
91+
92+
fixture.detectChanges();
93+
expect(overlayContainerElement.textContent).toContain(newMessage);
5294
});
5395
});
5496
});
5597

5698
@Component({
5799
selector: 'app',
58-
template: `<button md-tooltip="some message" [tooltip-position]="position">Button</button>`
100+
template: `<button [md-tooltip]="message" [tooltip-position]="position">Button</button>`
59101
})
60102
class BasicTooltipDemo {
61103
position: TooltipPosition = 'below';
104+
message: string = initialTooltipMessage;
62105
}

0 commit comments

Comments
 (0)