mat-form-field floatLabel="never" not working - angular-material

For some reason using Angular 8 this parameter of never does not work for me on a mat-select.
<mat-form-field appearance="outline" floatLabel="never">
<mat-label>Lesson</mat-label>
<mat-select [(value)]="selected" (selectionChange)="onMenuChange($event.value)">
<mat-option value="T1">Lesson 1</mat-option>
<mat-option value="T2">Lesson 2</mat-option>
<mat-option value="T3">Lesson 3</mat-option>
</mat-select>
</mat-form-field>
Always and Auto work, but never won't seem to make it go away.

Managed to come up with the following scss to imitate the label not floating:
.some-custom-class-name {
&.mat-form-field-appearance-outline.mat-form-field-can-float.mat-form-field-should-float .mat-form-field-label {
transform: none;
width: initial;
opacity: 0;
}
&.mat-form-field-appearance-outline.mat-form-field-can-float.mat-form-field-should-float .mat-form-field-outline-gap {
border-top-color: initial;
}
/* Not required, only used to reposition the label slightly
&.mat-form-field-appearance-outline .mat-form-field-label {
top: 1.54375em;
margin-top: -0.25em;
}
*/
}
<mat-form-field appearance="outline" class="some-custom-class-name">
<mat-label>Shouldn't float</mat-label>
<input matInput>
</mat-form-field>
Just append the some-custom-class-name class to the mat-form-field and it should work.

try to use floatLabel="never" or floatLabel="always"

My solution is kinda a shortcut of solution suggest by Dane Brower.
::ng-deep &.mat-form-field-appearance-outline.mat-form-field-should-float {
.mat-form-field-label {
display: none;
}
.mat-form-field-outline-gap {
border-top-color: initial;
}
}
In comparison to his solution, this one just removes the label element from DOM (via display: none) since we don't need it anymore. We also need ::ng-deep pseudo-class here since mat-form-field-... elements are encapsulated within mat-form-field.
Note: you have to be carefull with ::ng-deep and try to prefix it with :host in order to not apply its style globally (in my case :host ::ng-deep didn't work here).

Please refer to https://github.com/angular/components/issues/18267
Just add a place holder in the mat-select.
Like this: <mat-select placeholder="FL">

TLDR:
Just remove the <mat-label> and use a placeholder on <mat-form-field> instead.
You can hide the floating label, you just have to approach the 'outline' variant differently. And there is absolutely no need to forcefully override the label via some weird css hack.
From the Angular Material docs:
Note: only the legacy appearance supports the never option. never was originally added as a way to make the floating label emulate the behavior of a standard input placeholder. However the form field now supports both floating labels and placeholders. Therefore in the non-legacy appearances the never option has been disabled in favor of just using the placeholder.
So it's actually the intended behavior for the 'outline' variant.
All you have to do is to just use the placeholder attribute on <mat-form-field> instead of adding a <mat-label>.
In your case:
<mat-form-field appearance="outline" floatLabel="never" placeholder="Lesson">
<mat-select [(value)]="selected" (selectionChange)="onMenuChange($event.value)">
<mat-option value="T1">Lesson 1</mat-option>
<mat-option value="T2">Lesson 2</mat-option>
<mat-option value="T3">Lesson 3</mat-option>
</mat-select>
</mat-form-field>
So basically people usually add a label they don't even want and need, only to look for css hacks to hide it...

Related

Angular Material's mat-autocomplete input is not getting disabled as expected

I'm trying to disable an Angular Material Autocomplete component. I would have expected to just be able to set disabled on the input, but that does nothing. (I also tried setting disabled on mat-form-field and mat-autocomplete.) Setting matAutocompleteDisabled on input prevented the options from showing, but still allowed typing in the field. Setting readonly on input prevented typing, but it doesn't change the UI, so seems like that will be confusing for the user. Is this a bug, or am I missing something?
Here's the closest I've come so far, using readonly (and disabled isn't working as expected)
<form class="example-form">
<mat-form-field class="example-full-width">
<input type="text"
disabled readonly
placeholder="Pick one"
aria-label="Number"
matInput
[formControl]="myControl"
[matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of options" [value]="option">
{{option}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</form>
You should use formControl to set it, something like:
this.formGroupName.controls['myControl'].disable()
Use [attr.disabled]="true". It will set the disabled to true.
You can do css trick for this purpose.
Apply some class to parent tag of input. In you case upper tag is <mat-form-field class="example-full-width"> so i add disable-block class in this. And applied below css.
.disable-block {
pointer-events: none;
opacity: .7;
}
Full code here.
HTML
<form class="example-form">
<mat-form-field class="example-full-width disable-block">
<input type="text" disabled readonly placeholder="Pick one" aria-label="Number" matInput [formControl]="myControl" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of options" [value]="option">
{{option}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</form>
CSS
.disable-block {
pointer-events: none;
opacity: .7;
}
If you want more lighter text field then you can decrease opacity to .6 or .5 or more want you want.
Hope this will solve you problem.

Angular Material: Change font-size of <mat-list-item>

I am using Angular Material. I have a form with various fields and list items. I have changed the size of mat-form-field inputs using below
mat-form-field.mat-form-field {
font-size: 14px;
}
However since list are not a part of mat-form-field they are still their default 16px. How can I change the size of the <mat-list-item>?
<mat-list-item *ngFor="let file of files">{{file.name}}<mat-list-item>
Try the following CSS definition:
:host /deep/ .mat-list-item-content {
font-size: 18px;
}
I was actually doing this yesterday for my mat-list-items. h6 turned out to be an appropriate size for me.
<mat-list-item *ngFor="let file of files">
<span style="font-size:12px">
{{file.name}}
</span>
<mat-list-item>
or
<mat-list-item *ngFor="let file of files">
<h6>
{{file.name}}
</h6>
<mat-list-item>
You can add the styling directly to the markup. If you use class to reference CSS file, sometimes it won't work.
<mat-list-item style="font-size: 14px; height: 25px" >Test</mat-list-item>

How to remove underline of mat-select component

I am using basic mat-select component in my project.
<mat-form-field>
<mat-select placeholder="Favorite food">
<mat-option *ngFor="let food of foods" [value]="food.value">
{{food.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>
how can i remove the underline,I have tried this answer too still no result.
You can do this:
::ng-deep .mat-form-field-underline {
display: none;
}
StackBlitz
You can also use the appearance property on the <form-field> . Setting appearance="none" will remove the underline without any css hack. And you can also get rid of ::ng-deep which is not recommended.
So your code will be like this.
<mat-form-field appearance="none">
<mat-select placeholder="Favorite food">
<mat-option *ngFor="let food of foods" [value]="food.value">
{{food.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>
For more information on appearance, you can check here. https://material.angular.io/components/form-field/overview#form-field-appearance-variants
::ng-deep is going to be depracated.
Use css hierarchy. If you have a global css or scss file... then in the component, wrap this in a div and give it a class.
For example,
<div class="content-wrapper">
...all your html code here
</div>
Then, in your global css/scss file, you can target the .mat-form-field-underline class like so:
.content-wrapper .mat-form-field-underline {
display: none;
}
Here, notice that we don't use comma after class names. This is using hierarchy. It saying that we only want to target the native .mat-form-field-underline class that is wrapped inside the .content-wrapper
Hope this helps. Glad my senior developer had shared me this as we used ViewEncapsualtion, which bled css all over the place. This is the right solution to our use case, hope it helps yours.
This also works for other native mat classes for customization of your code/html.
If you want to hide underline in particular input only then you can do something like below.
<div class="select-block">
<mat-form-field>
<mat-select placeholder="Select Option">
<mat-option>One</mat-option>
<mat-option>Two</mat-option>
<mat-option>Three</mat-option>
</mat-select>
</mat-form-field>
</div>
Add the below-mentioned line of code in the parent class of mat-form-field.
.select-block{
/deep/ mat-form-field {
&.mat-form-field-appearance-legacy {
.mat-form-field-underline {
display: none;
}
}
}
}
Try this way to hide the underline.
Thanks.
Two options to solve the problem:
You can directly setup appearance="none" in mat-form-field tag
ex. <mat-form-field appearance="none">.
You can hide using css
ex. ::ng-deep .mat-form-field-underline { display: none; }
I had appearance="fill" on the mat-form-field. These settings worked to remove the underline for me:
scss:
.mat-form-field-appearance-fill {
.mat-form-field-underline {
display: none;
}
}
Add set encapsulation to ViewEncapsulation.None in the component:
encapsulation: ViewEncapsulation.None

Voiceover reading pseudo-elements despite aria-hidden attributes within <label />

I'm creating custom radio / checkbox icons by adding a pseudo-element on a label element with the :before css rule. I've added aria-hidden to the label element, but VO on iOS is still reading the pseudo-element.
I understand that some screen readers will ignore an aria-hidden attribute if the element is providing additional context (this is the case for label elements, since they provide additional information about a connected input element). To get around this I've added a aria-label attribute, but again, this is ignore by VO on iOS. This seems to fix the same problem for other screen reader, browser, and device combinations (Narrator and IE / Edge for example).
I've also tried to add a child span or i element to the label and add the :before css rule and aria-hidden attribute to that, but VO on iOS is still reading the pseudo-element.
Does anyone have any advice for having the screen reader read the correct content?
My basic approach is below (note: won't work in a jsfiddle since I'm not loading my font-face).
You can also view the first example here:
http://uatwww.surveygizmo.com/s3/4102902/Basic-Radio
<input type="radio" id="radio1" value="1" name="example" />
<label for="radio1" class="custom-icon" aria-hidden="true" aria-label="example 1">Example 1</label>
<input type="radio" id="radio2" value="2" name="example" />
<label for="radio2">
<span class="custom-icon" aria-hidden="true" aria-label="example 2"></span>
Example 2
</label>
<input type="radio" id="radio3" value="3" name="example" />
<label for="radio3">
<i class="custom-icon" aria-hidden="true" aria-label="example 3"></i>
Example 3
</label>
<style>
input[type=radio] {
opacity: 0;
position: absolute;
border: 0;
height: 0;
margin: 0;
overflow: hidden;
padding: 0;
}
input[type=radio] + .custom-icon:before,
input[type=radio] + label .custom-icon:before {
content: "\26aa";
}
input[type=radio]:checked + .custom-icon:before,
input[type=radio]:checked + label .custom-icon:before {
content: "\26ab";
}
</style>
I think the problem is that you are giving confusing instructions to both the browser and screenreader. You have an invisible input with CSS content attached to it, which is then associated to a label which is aria-hidden but also has an aria-label. You’re definitely going to get inconsistent interpretations of that markup across different browser/screenreader combinations.
I’ve used Heydon Pickering’s custom control method successfully on a bunch of sites with no problems. It seems like a simpler version of what you’re aiming for. It accessibly hides the input from everyone except screenreader software, puts the CSS content on a span instead of the label or input. He doesn’t use any ARIA, but if more recent versions of VoiceOver announce the CSS content you can just put aria-hidden on the span and let screenreaders treat the label and input as normal.
Concerning radio1, the W3C says:
If the current node is hidden and is not referenced by aria-labelledby or aria-describedby, nor referenced by a native host language text alternative element or attribute, return the empty string.
So as long as you reference an element even though it has the aria-hidden attribute, it will be spoken out.
If you want to give an alternative text for an element, you have to set the aria-label attribute on the element:
<input type="radio" id="radio1" value="1" name="example" aria-label="example 1" />
<label for="radio1" class="custom-icon" aria-hidden="true">Example 1</label>
Pseudo elements have different beheviours on browsers, and as you can see the alternative text for :before elements will be given even though the associated element is marked with the aria-hidden attribute.

Force label float when no input data in Angular Material

In Angular Material, the default design of input directives is for the content within <label> to be displayed in the input element until the user enters some input, at which point it will float above the input element, as seen in all examples here.
Is there any way to force the labels to float above the input box at all times instead, even when no data has been entered?
I think the css class md-input-has-placeholder is what you need:
<md-input-container class="md-input-has-placeholder">
<label>Name</label>
<input type="text"/>
</md-input-container>
Plunker example here
Hope it helps.
The is an official feature for that: floatLabel="always"
The floatLabel property of can be used to change this default floating behavior. It can set to never to hide the label instead of float it when text is present in the form field control. It can be set to always to float the label even when no text is present in the form field control. It can also be set to auto to restore the default behavior.
<mat-form-field floatLabel="always">
<mat-label>Both a label and a placeholder</mat-label>
<input matInput [(ngModel)]="model.value">
</mat-form-field>
source, see the official form-field documentation
With Md-select this worked for me:
<md-input-container style="width: 200px;" md-input-has-placeholder>
<placeholder>Snack Types </placeholder>
<md-select ng-model="selectedOption">
<md-option ng-repeat="item in snacks" >
{{item.name}}
</md-option>
</md-select>
</md-input-container>
For the md-select element, occupy the following:
<md-input-container class="md-input-has-placeholder">
<label md-no-float="true" class="md-required">Snack Types</label>
<md-select ng-model="$ctrl.selection" ng-required="true" md-no-asterisk>
<md-option ng-value="option.id" ng-repeat="option in $ctrl.selection">{{ opcion.value}}</md-option>
</md-select>
</md-input-container>
I used the following CSS to change the label when data is being entered and highlight the other fields:
md-input-container:focus-within > label[class~="md-required"]{
transform: scale(1);
font-weight: bold;
}
And the following CSS so that the asterisk is always in color (you can put the color you want):
md-input-container.md-default-theme:not(.md-input-focused):not(.md-input-invalid) label.md-required:after, md-input-container:not(.md-input-focused):not(.md-input-invalid) label.md-required:after{
color: rgb(255,87,10);
}

Resources