Skip to content

Commit

Permalink
fix(material-experimental/mdc-tooltip): fix text alignment of multili… (
Browse files Browse the repository at this point in the history
#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

(cherry picked from commit 81cd26b)
  • Loading branch information
wagnermaciel authored and mmalerba committed Jun 18, 2021
1 parent 405a219 commit da604bf
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
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

0 comments on commit da604bf

Please sign in to comment.