Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Material mat-form-field Placeholder text on multiple lines

I use a regular mat-form-field with a textarea inside. My issue is that the placeholder text for this Textarea is rather long. In mobile, where space is more limited, this placeholder text is truncated by Angular Material with an ellipsis.

I would like to have the placeholder text adjust to space restrictions by shifting down onto the next line. So, for example, instead of:

This is a really long text and it cannot fit on a single li...

I would like:

This is a really long text and it cannot fit on a single
line

I have already tried various approaches to changing the CSS of the mat-input-placeholder and mat-input-placeholder-wrapper, with no success. I know that part of the solution involves changing the text-overflow property (below), but I can't get the other parts.

::ng-deep .mat-input-placeholder {
  text-overflow: clip;
}

Can anyone help?

Thanks!

like image 922
Alex Verzea Avatar asked Oct 17 '22 18:10

Alex Verzea


2 Answers

::ng-deep is deprecated.

Instead, you should use ViewEncapsulation and control the CSS inside the component.

@Component({
  selector: 'app-example',
  templateUrl: './example.component.html',
  styleUrls: ['./example.component.css'],
  encapsulation: ViewEncapsulation.None,
})

For placeholder (remove placeholder from element):

<mat-placeholder style="white-space: normal;">{{question.label}}</mat-placeholder>

Then add your CSS styles without ::ng-deep in the example.component.css

.mat-form-field-label {
  white-space: normal;
}

textarea.mat-input-element {
  padding: 0px 0;
  margin: 5px 0;
}

.mat-input-placeholder {
  white-space: normal;
}
like image 71
Rijvi Rajib Avatar answered Oct 21 '22 00:10

Rijvi Rajib


wrap text:

<textarea></textarea>

<textarea
  matInput
  #fileName="ngModel"
  name="fileName"
  [(ngModel)]="action!.params.file!.originalName"
  type="text"
  autocomplete="off"
  autocapitalize="off"
  readonly
  required
  [matTooltip]="action!.params.file!.originalName"
></textarea>

overflow-clip text:

<input/>

<input
  matInput
  #fileName="ngModel"
  name="fileName"
  [(ngModel)]="action!.params.file!.originalName"
  type="text"
  autocomplete="off"
  autocapitalize="off"
  readonly
  required
  [matTooltip]="action!.params.file!.originalName"
/>
like image 26
Lorka Avatar answered Oct 21 '22 00:10

Lorka