Can't make multi-line matTooltip, always getting single-line

Viewed 2355

Using [matTooltip]="myMultiLineStringGenerator()" combined with [matTooltipClass]="'my-tooltip'" I was expecting to give me multi-line tooltips, whoever I get one liner tooltip, and I have no control when lines ends and moves to the next line.

Steps to reproduce

HTML part

<p  [matTooltipClass]="'my-tooltip'"
    [matTooltip]="getTooltopScript(nearestStations)">
            Hello World!
</p>

CSS part:

.mat-tooltip {
    white-space: pre-line;
}

::ng-deep .mat-tooltip {
   white-space: pre-line;
}

TypeScript

getTooltopScript(ns: any[]]){
      let part1 =  ns[0]['stationName'] ;
      let part2 = ns[0]['regionName'];
      return `${part1}.\r\n\
       ${part2}.`;
}

I have read a lot other stack flows, and all of them where ending to use whitespace:pre-line in css, and \r\n in js\type script to create string. I dont know what else I should do make it work

Please help :)

update in CSS from other stackflows, and first comment below. I still have no result

::ng-deep .mat-tooltip .cdk-overlay-container, ::ng-deep .mat-tooltip .cdk- 
    global-overlay-wrapper {
    white-space: pre-line;
}
2 Answers

If you are having the text coming from ts file, then please find below solution:

In .ts file:

getTooltopScript(){
  return 'Multiline Tooltip \n This is second line';
}

In HTML:

<p #tooltip2="matTooltip" [matTooltip]="getTooltopScript()">Hello World!</p>

In css file:

::ng-deep .cdk-overlay-container, .cdk-global-overlay-wrapper {
  white-space: pre-line;
}

In order to see the tooltip properly, we need to add the material style import in global stylesheet (that is style.css):

In style.css:

@import '~@angular/material/prebuilt-themes/deeppurple-amber.css';

Please find the working example below:

https://angular-sf-tooltip-issue-fix.stackblitz.io

https://stackblitz.com/edit/angular-sf-tooltip-issue-fix?file=src%2Fapp%2Fapp.module.ts

Related