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

fix(material-experimental/mdc-tooltip): fix text alignment of multili… #22981

Merged
merged 7 commits into from
Jun 18, 2021
1 change: 1 addition & 0 deletions src/material-experimental/mdc-tooltip/tooltip.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<div
class="mdc-tooltip mdc-tooltip--shown mat-mdc-tooltip"
[ngClass]="tooltipClass"
[class.mdc-tooltip--multiline]="_isMultiline"
[@state]="_visibility"
(@state.start)="_animationStart()"
(@state.done)="_animationDone($event)">
Expand Down
17 changes: 17 additions & 0 deletions src/material-experimental/mdc-tooltip/tooltip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,23 @@ describe('MDC-based MatTooltip', () => {
fixture.destroy();
}));

it('should set the multiline class on tooltips with messages that overflow', fakeAsync(() => {
fixture.componentInstance.message = 'This is a very long message that should cause the'
+ 'tooltip message body to overflow onto a new line.';
tooltipDirective.show();
fixture.detectChanges();
tick();

// Need to detect changes again to wait for the multiline class to be applied.
fixture.detectChanges();

const tooltipElement =
overlayContainerElement.querySelector('.mat-mdc-tooltip') as HTMLElement;

expect(tooltipElement.classList).toContain('mdc-tooltip--multiline');
expect(tooltipDirective._tooltipInstance?._isMultiline).toBeTrue();
}));

});

describe('fallback positions', () => {
Expand Down
16 changes: 15 additions & 1 deletion src/material-experimental/mdc-tooltip/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,21 @@ export class MatTooltip extends _MatTooltipBase<TooltipComponent> {
}
})
export class TooltipComponent extends _TooltipComponentBase {
constructor(changeDetectorRef: ChangeDetectorRef) {
/* Whether the tooltip text overflows to multiple lines */
_isMultiline: boolean = false;

constructor(changeDetectorRef: ChangeDetectorRef, private _elementRef: ElementRef) {
super(changeDetectorRef);
}

/** @override */
protected _onShow(): void {
this._isMultiline = this._isTooltipMultiline();
}

/** Whether the tooltip text has overflown to the next line */
private _isTooltipMultiline() {
const rect = this._elementRef.nativeElement.getBoundingClientRect();
return rect.height > numbers.MIN_HEIGHT && rect.width >= numbers.MAX_WIDTH;
}
}
8 changes: 8 additions & 0 deletions src/material/tooltip/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,7 @@ export abstract class _TooltipComponentBase implements OnDestroy {
this._showTimeoutId = setTimeout(() => {
this._visibility = 'visible';
this._showTimeoutId = undefined;
this._onShow();

// Mark for check so if any parent component has set the
// ChangeDetectionStrategy to OnPush it will be checked anyways
Expand Down Expand Up @@ -867,6 +868,13 @@ export abstract class _TooltipComponentBase implements OnDestroy {
_markForCheck(): void {
this._changeDetectorRef.markForCheck();
}

/**
* Callback for when the timeout in this.show() gets completed.
* This method is only needed by the mdc-tooltip, and so it is only implemented
* in the mdc-tooltip, not here.
*/
protected _onShow(): void {}
}

/**
Expand Down
1 change: 1 addition & 0 deletions tools/public_api_guard/material/tooltip.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export declare abstract class _TooltipComponentBase implements OnDestroy {
_animationStart(): void;
_handleBodyInteraction(): void;
_markForCheck(): void;
protected _onShow(): void;
afterHidden(): Observable<void>;
hide(delay: number): void;
isVisible(): boolean;
Expand Down