Skip to content

Commit a018fb0

Browse files
authoredJul 22, 2024··
feat(material/tooltip): replicate tooltipClass to default MatTooltipDefaultOptions (#29467)
* feat(material/form-field): replicate tooltipClass to default MatTooltipDefaultOptions The `tooltipClass` property has been added to the default configuration options in `MatTooltipDefaultOptions`. This new property is optional and supports the same syntax as `ngClass`, just like the component's default attribute. As with some existing configurations, if a CSS class is defined directly on the tooltip component, it will automatically override the default class. An example has been added to the `tooltip-demo` file. Additionally, two tests have been created to ensure the solution works as expected. Fixes #29355 * feat(material/form-field): replicate tooltipClass to default MatTooltipDefaultOptions The `tooltipClass` property has been added to the default configuration options in `MatTooltipDefaultOptions`. This new property is optional. As with some existing configurations, if a CSS class is defined directly on the tooltip component, it will automatically override the default class. Two tests have been created to ensure the solution works as expected. Fixes #29355 * feat(material/form-field): replicate tooltipClass to default MatTooltipDefaultOptions The `tooltipClass` property has been added to the default configuration options in `MatTooltipDefaultOptions`. This new property is optional. As with some existing configurations, if a CSS class is defined directly on the tooltip component, it will automatically override the default class. Two tests have been created to ensure the solution works as expected. Fixes #29355
1 parent 10da6c6 commit a018fb0

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
 

‎src/material/tooltip/tooltip.spec.ts

+81
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,65 @@ describe('MDC-based MatTooltip', () => {
233233
expect(tooltipDirective._getOverlayPosition().fallback.overlayX).toBe('end');
234234
}));
235235

236+
it('should be able to define a default (global) tooltip class', fakeAsync(() => {
237+
TestBed.resetTestingModule()
238+
.configureTestingModule({
239+
declarations: [TooltipDemoWithoutTooltipClassBinding],
240+
imports: [MatTooltipModule, OverlayModule],
241+
providers: [
242+
{
243+
provide: MAT_TOOLTIP_DEFAULT_OPTIONS,
244+
useValue: {tooltipClass: 'my-default-tooltip-class'},
245+
},
246+
],
247+
})
248+
.compileComponents();
249+
250+
const fixture = TestBed.createComponent(TooltipDemoWithoutTooltipClassBinding);
251+
fixture.detectChanges();
252+
tooltipDirective = fixture.componentInstance.tooltip;
253+
tooltipDirective.show();
254+
fixture.detectChanges();
255+
tick();
256+
const overlayRef = tooltipDirective._overlayRef!;
257+
const tooltipElement = overlayRef.overlayElement.querySelector(
258+
'.mat-mdc-tooltip',
259+
) as HTMLElement;
260+
261+
expect(tooltipDirective.tooltipClass).toBe('my-default-tooltip-class');
262+
expect(tooltipElement.classList).toContain('my-default-tooltip-class');
263+
}));
264+
265+
it('should be able to provide tooltip class over the custom default one', fakeAsync(() => {
266+
TestBed.resetTestingModule()
267+
.configureTestingModule({
268+
declarations: [TooltipDemoWithTooltipClassBinding],
269+
imports: [MatTooltipModule, OverlayModule],
270+
providers: [
271+
{
272+
provide: MAT_TOOLTIP_DEFAULT_OPTIONS,
273+
useValue: {tooltipClass: 'my-default-tooltip-class'},
274+
},
275+
],
276+
})
277+
.compileComponents();
278+
279+
const fixture = TestBed.createComponent(TooltipDemoWithTooltipClassBinding);
280+
fixture.detectChanges();
281+
tooltipDirective = fixture.componentInstance.tooltip;
282+
tooltipDirective.show();
283+
fixture.detectChanges();
284+
tick();
285+
const overlayRef = tooltipDirective._overlayRef!;
286+
const tooltipElement = overlayRef.overlayElement.querySelector(
287+
'.mat-mdc-tooltip',
288+
) as HTMLElement;
289+
290+
expect(tooltipDirective.tooltipClass).not.toBe('my-default-tooltip-class');
291+
expect(tooltipElement.classList).not.toContain('my-default-tooltip-class');
292+
expect(tooltipElement.classList).toContain('fixed-tooltip-class');
293+
}));
294+
236295
it('should position on the bottom-left by default', fakeAsync(() => {
237296
// We don't bind mouse events on mobile devices.
238297
if (platform.IOS || platform.ANDROID) {
@@ -1660,6 +1719,28 @@ class TooltipDemoWithoutPositionBinding {
16601719
@ViewChild('button') button: ElementRef<HTMLButtonElement>;
16611720
}
16621721

1722+
@Component({
1723+
selector: 'app',
1724+
template: `<button #button [matTooltip]="message">Button</button>`,
1725+
})
1726+
class TooltipDemoWithoutTooltipClassBinding {
1727+
message = initialTooltipMessage;
1728+
@ViewChild(MatTooltip) tooltip: MatTooltip;
1729+
@ViewChild('button') button: ElementRef<HTMLButtonElement>;
1730+
}
1731+
1732+
@Component({
1733+
selector: 'app',
1734+
template: `
1735+
<button #button matTooltipClass="fixed-tooltip-class" [matTooltip]="message">Button</button>
1736+
`,
1737+
})
1738+
class TooltipDemoWithTooltipClassBinding {
1739+
message: any = initialTooltipMessage;
1740+
@ViewChild(MatTooltip) tooltip: MatTooltip;
1741+
@ViewChild('button') button: ElementRef<HTMLButtonElement>;
1742+
}
1743+
16631744
@Component({
16641745
selector: 'app',
16651746
styles: `button { width: 500px; height: 500px; }`,

‎src/material/tooltip/tooltip.ts

+10
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ export interface MatTooltipDefaultOptions {
147147

148148
/** Disables the ability for the user to interact with the tooltip element. */
149149
disableTooltipInteractivity?: boolean;
150+
151+
/**
152+
* Default classes to be applied to the tooltip. These default classes will not be applied if
153+
* `tooltipClass` is defined directly on the tooltip element, as it will override the default.
154+
*/
155+
tooltipClass?: string | string[];
150156
}
151157

152158
/**
@@ -389,6 +395,10 @@ export class MatTooltip implements OnDestroy, AfterViewInit {
389395
if (_defaultOptions.touchGestures) {
390396
this.touchGestures = _defaultOptions.touchGestures;
391397
}
398+
399+
if (_defaultOptions.tooltipClass) {
400+
this.tooltipClass = _defaultOptions.tooltipClass;
401+
}
392402
}
393403

394404
_dir.change.pipe(takeUntil(this._destroyed)).subscribe(() => {

0 commit comments

Comments
 (0)
Please sign in to comment.