Skip to content

Commit 734eccc

Browse files
willshowellkara
authored andcommitted
feat(tooltip): add mdTooltipClass for customizing (#4893)
1 parent 40f92c5 commit 734eccc

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
@@ -7,13 +7,19 @@
77
*/
88

99
import {NgModule} from '@angular/core';
10+
import {CommonModule} from '@angular/common';
1011
import {OverlayModule, MdCommonModule} from '../core';
1112
import {PlatformModule} from '../core/platform/index';
1213
import {MdTooltip, TooltipComponent} from './tooltip';
1314

1415

1516
@NgModule({
16-
imports: [OverlayModule, MdCommonModule, PlatformModule],
17+
imports: [
18+
CommonModule,
19+
OverlayModule,
20+
MdCommonModule,
21+
PlatformModule
22+
],
1723
exports: [MdTooltip, TooltipComponent, MdCommonModule],
1824
declarations: [MdTooltip, TooltipComponent],
1925
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
@@ -17,6 +17,7 @@ import {
1717
OnDestroy,
1818
Renderer2,
1919
ChangeDetectorRef,
20+
ViewEncapsulation,
2021
} from '@angular/core';
2122
import {
2223
style,
@@ -75,6 +76,7 @@ export class MdTooltip implements OnDestroy {
7576

7677
private _position: TooltipPosition = 'below';
7778
private _disabled: boolean = false;
79+
private _tooltipClass: string|string[]|Set<string>|{[key: string]: any};
7880

7981
/** Allows the user to define the position of the tooltip relative to the parent element */
8082
@Input('mdTooltipPosition')
@@ -125,6 +127,16 @@ export class MdTooltip implements OnDestroy {
125127
}
126128
}
127129

130+
/** Classes to be passed to the tooltip. Supports the same syntax as `ngClass`. */
131+
@Input('mdTooltipClass')
132+
get tooltipClass() { return this._tooltipClass; }
133+
set tooltipClass(value: string|string[]|Set<string>|{[key: string]: any}) {
134+
this._tooltipClass = value;
135+
if (this._tooltipInstance) {
136+
this._setTooltipClass(this._tooltipClass);
137+
}
138+
}
139+
128140
/** @deprecated */
129141
@Input('md-tooltip')
130142
get _deprecatedMessage(): string { return this.message; }
@@ -155,6 +167,11 @@ export class MdTooltip implements OnDestroy {
155167
get _matShowDelay() { return this.showDelay; }
156168
set _matShowDelay(v) { this.showDelay = v; }
157169

170+
// Properties with `mat-` prefix for nonconflict mode.
171+
@Input('matTooltipClass')
172+
get _matClass() { return this.tooltipClass; }
173+
set _matClass(v) { this.tooltipClass = v; }
174+
158175
constructor(
159176
private _overlay: Overlay,
160177
private _elementRef: ElementRef,
@@ -190,6 +207,7 @@ export class MdTooltip implements OnDestroy {
190207
this._createTooltip();
191208
}
192209

210+
this._setTooltipClass(this._tooltipClass);
193211
this._setTooltipMessage(this._message);
194212
this._tooltipInstance.show(this._position, delay);
195213
}
@@ -322,6 +340,12 @@ export class MdTooltip implements OnDestroy {
322340
}
323341
});
324342
}
343+
344+
/** Updates the tooltip class */
345+
private _setTooltipClass(tooltipClass: string|string[]|Set<string>|{[key: string]: any}) {
346+
this._tooltipInstance.tooltipClass = tooltipClass;
347+
this._tooltipInstance._markForCheck();
348+
}
325349
}
326350

327351
export type TooltipVisibility = 'initial' | 'visible' | 'hidden';
@@ -335,6 +359,7 @@ export type TooltipVisibility = 'initial' | 'visible' | 'hidden';
335359
selector: 'md-tooltip-component, mat-tooltip-component',
336360
templateUrl: 'tooltip.html',
337361
styleUrls: ['tooltip.css'],
362+
encapsulation: ViewEncapsulation.None,
338363
animations: [
339364
trigger('state', [
340365
state('void', style({transform: 'scale(0)'})),
@@ -356,6 +381,9 @@ export class TooltipComponent {
356381
/** Message to display in the tooltip */
357382
message: string;
358383

384+
/** Classes to be added to the tooltip. Supports the same syntax as `ngClass`. */
385+
tooltipClass: string|string[]|Set<string>|{[key: string]: any};
386+
359387
/** The timeout ID of any current timer set to show the tooltip */
360388
_showTimeoutId: number;
361389

0 commit comments

Comments
 (0)