Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(material/tooltip): replicate tooltipClass to default MatTooltipDefaultOptions #29467

Merged
merged 3 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions src/material/tooltip/tooltip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,65 @@ describe('MDC-based MatTooltip', () => {
expect(tooltipDirective._getOverlayPosition().fallback.overlayX).toBe('end');
}));

it('should be able to define a default (global) tooltip class', fakeAsync(() => {
TestBed.resetTestingModule()
.configureTestingModule({
declarations: [TooltipDemoWithoutTooltipClassBinding],
imports: [MatTooltipModule, OverlayModule],
providers: [
{
provide: MAT_TOOLTIP_DEFAULT_OPTIONS,
useValue: {tooltipClass: 'my-default-tooltip-class'},
},
],
})
.compileComponents();

const fixture = TestBed.createComponent(TooltipDemoWithoutTooltipClassBinding);
fixture.detectChanges();
tooltipDirective = fixture.componentInstance.tooltip;
tooltipDirective.show();
fixture.detectChanges();
tick();
const overlayRef = tooltipDirective._overlayRef!;
const tooltipElement = overlayRef.overlayElement.querySelector(
'.mat-mdc-tooltip',
) as HTMLElement;

expect(tooltipDirective.tooltipClass).toBe('my-default-tooltip-class');
expect(tooltipElement.classList).toContain('my-default-tooltip-class');
}));

it('should be able to provide tooltip class over the custom default one', fakeAsync(() => {
TestBed.resetTestingModule()
.configureTestingModule({
declarations: [TooltipDemoWithTooltipClassBinding],
imports: [MatTooltipModule, OverlayModule],
providers: [
{
provide: MAT_TOOLTIP_DEFAULT_OPTIONS,
useValue: {tooltipClass: 'my-default-tooltip-class'},
},
],
})
.compileComponents();

const fixture = TestBed.createComponent(TooltipDemoWithTooltipClassBinding);
fixture.detectChanges();
tooltipDirective = fixture.componentInstance.tooltip;
tooltipDirective.show();
fixture.detectChanges();
tick();
const overlayRef = tooltipDirective._overlayRef!;
const tooltipElement = overlayRef.overlayElement.querySelector(
'.mat-mdc-tooltip',
) as HTMLElement;

expect(tooltipDirective.tooltipClass).not.toBe('my-default-tooltip-class');
expect(tooltipElement.classList).not.toContain('my-default-tooltip-class');
expect(tooltipElement.classList).toContain('fixed-tooltip-class');
}));

it('should position on the bottom-left by default', fakeAsync(() => {
// We don't bind mouse events on mobile devices.
if (platform.IOS || platform.ANDROID) {
Expand Down Expand Up @@ -1660,6 +1719,28 @@ class TooltipDemoWithoutPositionBinding {
@ViewChild('button') button: ElementRef<HTMLButtonElement>;
}

@Component({
selector: 'app',
template: `<button #button [matTooltip]="message">Button</button>`,
})
class TooltipDemoWithoutTooltipClassBinding {
message = initialTooltipMessage;
@ViewChild(MatTooltip) tooltip: MatTooltip;
@ViewChild('button') button: ElementRef<HTMLButtonElement>;
}

@Component({
selector: 'app',
template: `
<button #button matTooltipClass="fixed-tooltip-class" [matTooltip]="message">Button</button>
`,
})
class TooltipDemoWithTooltipClassBinding {
message: any = initialTooltipMessage;
@ViewChild(MatTooltip) tooltip: MatTooltip;
@ViewChild('button') button: ElementRef<HTMLButtonElement>;
}

@Component({
selector: 'app',
styles: `button { width: 500px; height: 500px; }`,
Expand Down
10 changes: 10 additions & 0 deletions src/material/tooltip/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ export interface MatTooltipDefaultOptions {

/** Disables the ability for the user to interact with the tooltip element. */
disableTooltipInteractivity?: boolean;

/**
* Default classes to be applied to the tooltip. These default classes will not be applied if
* `tooltipClass` is defined directly on the tooltip element, as it will override the default.
*/
tooltipClass?: string | string[];
}

/**
Expand Down Expand Up @@ -389,6 +395,10 @@ export class MatTooltip implements OnDestroy, AfterViewInit {
if (_defaultOptions.touchGestures) {
this.touchGestures = _defaultOptions.touchGestures;
}

if (_defaultOptions.tooltipClass) {
this.tooltipClass = _defaultOptions.tooltipClass;
}
}

_dir.change.pipe(takeUntil(this._destroyed)).subscribe(() => {
Expand Down
Loading