Skip to content

Commit e05ffe5

Browse files
committed
feat(tooltip): add mdTooltipClass
1 parent 0c03946 commit e05ffe5

File tree

7 files changed

+78
-4
lines changed

7 files changed

+78
-4
lines changed

src/demo-app/tooltip/tooltip-demo.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ <h1>Tooltip Demo</h1>
99
[mdTooltipPosition]="position"
1010
[mdTooltipDisabled]="disabled"
1111
[mdTooltipShowDelay]="showDelay"
12-
[mdTooltipHideDelay]="hideDelay">
12+
[mdTooltipHideDelay]="hideDelay"
13+
[mdTooltipClass]="{'red-tooltip': showExtraClass}">
1314
Mouse over to see the tooltip
1415
</button>
1516
<div>Scroll down while tooltip is open to see it hide automatically</div>
@@ -60,4 +61,7 @@ <h1>Tooltip Demo</h1>
6061
<button md-raised-button color="primary" (mouseenter)="tooltip.toggle()">
6162
Toggle tooltip
6263
</button>
64+
<button md-raised-button color="primary" (mouseenter)="showExtraClass = !showExtraClass">
65+
Toggle tooltipClass
66+
</button>
6367
</div>

src/demo-app/tooltip/tooltip-demo.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@
1212
display: block;
1313
}
1414
}
15+
16+
.red-tooltip {
17+
background-color: rgb(255, 128, 128);
18+
color: black;
19+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component} from '@angular/core';
1+
import {Component, ViewEncapsulation} from '@angular/core';
22
import {TooltipPosition} from '@angular/material';
33

44

@@ -7,11 +7,13 @@ import {TooltipPosition} from '@angular/material';
77
selector: 'tooltip-demo',
88
templateUrl: 'tooltip-demo.html',
99
styleUrls: ['tooltip-demo.css'],
10+
encapsulation: ViewEncapsulation.None,
1011
})
1112
export class TooltipDemo {
1213
position: TooltipPosition = 'below';
1314
message: string = 'Here is the tooltip';
1415
disabled = false;
1516
showDelay = 0;
1617
hideDelay = 1000;
18+
showExtraClass = false;
1719
}

src/lib/tooltip/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import {NgModule} from '@angular/core';
2+
import {CommonModule} from '@angular/common';
23
import {OverlayModule, MdCommonModule} from '../core';
34
import {PlatformModule} from '../core/platform/index';
45
import {MdTooltip, TooltipComponent} from './tooltip';
56

67

78
@NgModule({
8-
imports: [OverlayModule, MdCommonModule, PlatformModule],
9+
imports: [
10+
CommonModule,
11+
OverlayModule,
12+
MdCommonModule,
13+
PlatformModule
14+
],
915
exports: [MdTooltip, TooltipComponent, MdCommonModule],
1016
declarations: [MdTooltip, TooltipComponent],
1117
entryComponents: [TooltipComponent],

src/lib/tooltip/tooltip.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<div class="mat-tooltip"
2+
[ngClass]="tooltipClass"
23
[style.transform-origin]="_transformOrigin"
34
[@state]="_visibility"
45
(@state.done)="_afterVisibilityAnimation($event)">

src/lib/tooltip/tooltip.spec.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,32 @@ describe('MdTooltip', () => {
240240
expect(overlayContainerElement.textContent).toContain(newMessage);
241241
}));
242242

243+
it('should allow extra classes to be set on the tooltip', fakeAsync(() => {
244+
expect(tooltipDirective._tooltipInstance).toBeUndefined();
245+
246+
tooltipDirective.show();
247+
tick(0); // Tick for the show delay (default is 0)
248+
fixture.detectChanges();
249+
250+
// Make sure classes aren't prematurely added
251+
let tooltipElement = overlayContainerElement.querySelector('.mat-tooltip') as HTMLElement;
252+
expect(tooltipElement.classList).not.toContain('custom-one',
253+
'Expected to not have the class before enabling mdTooltipClass');
254+
expect(tooltipElement.classList).not.toContain('custom-two',
255+
'Expected to not have the class before enabling mdTooltipClass');
256+
257+
// Enable the classes via ngClass syntax
258+
fixture.componentInstance.showTooltipClass = true;
259+
fixture.detectChanges();
260+
261+
// Make sure classes are correctly added
262+
tooltipElement = overlayContainerElement.querySelector('.mat-tooltip') as HTMLElement;
263+
expect(tooltipElement.classList).toContain('custom-one',
264+
'Expected to have the class after enabling mdTooltipClass');
265+
expect(tooltipElement.classList).toContain('custom-two',
266+
'Expected to have the class after enabling mdTooltipClass');
267+
}));
268+
243269
it('should be removed after parent destroyed', fakeAsync(() => {
244270
tooltipDirective.show();
245271
tick(0); // Tick for the show delay (default is 0)
@@ -468,14 +494,16 @@ describe('MdTooltip', () => {
468494
template: `
469495
<button *ngIf="showButton"
470496
[mdTooltip]="message"
471-
[mdTooltipPosition]="position">
497+
[mdTooltipPosition]="position"
498+
[mdTooltipClass]="{'custom-one': showTooltipClass, 'custom-two': showTooltipClass }">
472499
Button
473500
</button>`
474501
})
475502
class BasicTooltipDemo {
476503
position: string = 'below';
477504
message: string = initialTooltipMessage;
478505
showButton: boolean = true;
506+
showTooltipClass = false;
479507
@ViewChild(MdTooltip) tooltip: MdTooltip;
480508
}
481509

src/lib/tooltip/tooltip.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
OnDestroy,
1010
Renderer2,
1111
ChangeDetectorRef,
12+
ViewEncapsulation,
1213
} from '@angular/core';
1314
import {
1415
style,
@@ -68,6 +69,7 @@ export class MdTooltip implements OnDestroy {
6869

6970
private _position: TooltipPosition = 'below';
7071
private _disabled: boolean = false;
72+
private _tooltipClass: string|string[]|Set<string>|{[key: string]: any};
7173

7274
/** Allows the user to define the position of the tooltip relative to the parent element */
7375
@Input('mdTooltipPosition')
@@ -118,6 +120,16 @@ export class MdTooltip implements OnDestroy {
118120
}
119121
}
120122

123+
/** Classes to be passed to the tooltip. Supports the same syntax as `ngClass`. */
124+
@Input('mdTooltipClass')
125+
get tooltipClass() { return this._tooltipClass; }
126+
set tooltipClass(value: string|string[]|Set<string>|{[key: string]: any}) {
127+
this._tooltipClass = value;
128+
if (this._tooltipInstance) {
129+
this._setTooltipClass(this._tooltipClass);
130+
}
131+
}
132+
121133
/** @deprecated */
122134
@Input('md-tooltip')
123135
get _deprecatedMessage(): string { return this.message; }
@@ -148,6 +160,11 @@ export class MdTooltip implements OnDestroy {
148160
get _matShowDelay() { return this.showDelay; }
149161
set _matShowDelay(v) { this.showDelay = v; }
150162

163+
// Properties with `mat-` prefix for nonconflict mode.
164+
@Input('matTooltipClass')
165+
get _matClass() { return this.tooltipClass; }
166+
set _matClass(v) { this.tooltipClass = v; }
167+
151168
constructor(
152169
private _overlay: Overlay,
153170
private _elementRef: ElementRef,
@@ -183,6 +200,7 @@ export class MdTooltip implements OnDestroy {
183200
this._createTooltip();
184201
}
185202

203+
this._setTooltipClass(this._tooltipClass);
186204
this._setTooltipMessage(this._message);
187205
this._tooltipInstance.show(this._position, delay);
188206
}
@@ -313,6 +331,12 @@ export class MdTooltip implements OnDestroy {
313331
}
314332
});
315333
}
334+
335+
/** Updates the tooltip class */
336+
private _setTooltipClass(tooltipClass: string|string[]|Set<string>|{[key: string]: any}) {
337+
this._tooltipInstance.tooltipClass = tooltipClass;
338+
this._tooltipInstance._markForCheck();
339+
}
316340
}
317341

318342
export type TooltipVisibility = 'initial' | 'visible' | 'hidden';
@@ -326,6 +350,7 @@ export type TooltipVisibility = 'initial' | 'visible' | 'hidden';
326350
selector: 'md-tooltip-component, mat-tooltip-component',
327351
templateUrl: 'tooltip.html',
328352
styleUrls: ['tooltip.css'],
353+
encapsulation: ViewEncapsulation.None,
329354
animations: [
330355
trigger('state', [
331356
state('void', style({transform: 'scale(0)'})),
@@ -344,6 +369,9 @@ export class TooltipComponent {
344369
/** Message to display in the tooltip */
345370
message: string;
346371

372+
/** Classes to be added to the tooltip. Supports the same syntax as `ngClass`. */
373+
tooltipClass: string|string[]|Set<string>|{[key: string]: any};
374+
347375
/** The timeout ID of any current timer set to show the tooltip */
348376
_showTimeoutId: number;
349377

0 commit comments

Comments
 (0)