Skip to content

Commit 81cd26b

Browse files
authored
fix(material-experimental/mdc-tooltip): fix text alignment of multili… (#22981)
* fix(material-experimental/mdc-tooltip): fix text alignment of multiline tooltips * Add the 'mdc-tooltip--multiline' class when a tooltip overflows to make the text align left (or right in rtl) instead of center * fixup! fix(material-experimental/mdc-tooltip): fix text alignment of multiline tooltips * fixup! fix(material-experimental/mdc-tooltip): fix text alignment of multiline tooltips * fixup! fix(material-experimental/mdc-tooltip): fix text alignment of multiline tooltips * test(material-experimental/mdc-tooltip): ensure the multiline class is set on tooltips with messages that overflow * fixup! fix(material-experimental/mdc-tooltip): fix text alignment of multiline tooltips * fixup! fix(material-experimental/mdc-tooltip): fix text alignment of multiline tooltips
1 parent 4531748 commit 81cd26b

File tree

5 files changed

+42
-1
lines changed

5 files changed

+42
-1
lines changed

src/material-experimental/mdc-tooltip/tooltip.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<div
22
class="mdc-tooltip mdc-tooltip--shown mat-mdc-tooltip"
33
[ngClass]="tooltipClass"
4+
[class.mdc-tooltip--multiline]="_isMultiline"
45
[@state]="_visibility"
56
(@state.start)="_animationStart()"
67
(@state.done)="_animationDone($event)">

src/material-experimental/mdc-tooltip/tooltip.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,23 @@ describe('MDC-based MatTooltip', () => {
865865
fixture.destroy();
866866
}));
867867

868+
it('should set the multiline class on tooltips with messages that overflow', fakeAsync(() => {
869+
fixture.componentInstance.message = 'This is a very long message that should cause the'
870+
+ 'tooltip message body to overflow onto a new line.';
871+
tooltipDirective.show();
872+
fixture.detectChanges();
873+
tick();
874+
875+
// Need to detect changes again to wait for the multiline class to be applied.
876+
fixture.detectChanges();
877+
878+
const tooltipElement =
879+
overlayContainerElement.querySelector('.mat-mdc-tooltip') as HTMLElement;
880+
881+
expect(tooltipElement.classList).toContain('mdc-tooltip--multiline');
882+
expect(tooltipDirective._tooltipInstance?._isMultiline).toBeTrue();
883+
}));
884+
868885
});
869886

870887
describe('fallback positions', () => {

src/material-experimental/mdc-tooltip/tooltip.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,21 @@ export class MatTooltip extends _MatTooltipBase<TooltipComponent> {
115115
}
116116
})
117117
export class TooltipComponent extends _TooltipComponentBase {
118-
constructor(changeDetectorRef: ChangeDetectorRef) {
118+
/* Whether the tooltip text overflows to multiple lines */
119+
_isMultiline: boolean = false;
120+
121+
constructor(changeDetectorRef: ChangeDetectorRef, private _elementRef: ElementRef) {
119122
super(changeDetectorRef);
120123
}
124+
125+
/** @override */
126+
protected _onShow(): void {
127+
this._isMultiline = this._isTooltipMultiline();
128+
}
129+
130+
/** Whether the tooltip text has overflown to the next line */
131+
private _isTooltipMultiline() {
132+
const rect = this._elementRef.nativeElement.getBoundingClientRect();
133+
return rect.height > numbers.MIN_HEIGHT && rect.width >= numbers.MAX_WIDTH;
134+
}
121135
}

src/material/tooltip/tooltip.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,7 @@ export abstract class _TooltipComponentBase implements OnDestroy {
791791
this._showTimeoutId = setTimeout(() => {
792792
this._visibility = 'visible';
793793
this._showTimeoutId = undefined;
794+
this._onShow();
794795

795796
// Mark for check so if any parent component has set the
796797
// ChangeDetectionStrategy to OnPush it will be checked anyways
@@ -867,6 +868,13 @@ export abstract class _TooltipComponentBase implements OnDestroy {
867868
_markForCheck(): void {
868869
this._changeDetectorRef.markForCheck();
869870
}
871+
872+
/**
873+
* Callback for when the timeout in this.show() gets completed.
874+
* This method is only needed by the mdc-tooltip, and so it is only implemented
875+
* in the mdc-tooltip, not here.
876+
*/
877+
protected _onShow(): void {}
870878
}
871879

872880
/**

tools/public_api_guard/material/tooltip.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export declare abstract class _TooltipComponentBase implements OnDestroy {
5656
_animationStart(): void;
5757
_handleBodyInteraction(): void;
5858
_markForCheck(): void;
59+
protected _onShow(): void;
5960
afterHidden(): Observable<void>;
6061
hide(delay: number): void;
6162
isVisible(): boolean;

0 commit comments

Comments
 (0)