Skip to content

Commit cea7675

Browse files
committed
fixup! fix(material-experimental/mdc-form-field): use a CSS var for the floating label scale
1 parent 41e49db commit cea7675

File tree

3 files changed

+7
-16
lines changed

3 files changed

+7
-16
lines changed

src/material-experimental/mdc-form-field/directives/notched-outline.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {
3636
})
3737
export class MatFormFieldNotchedOutline implements AfterViewInit {
3838
/** Width of the notch. */
39-
@Input('matFormFieldNotchedOutlineWidth') width: number = 0;
39+
@Input('matFormFieldNotchedOutlineLabelWidth') labelWidth: number = 0;
4040

4141
/** Whether the notch should be opened. */
4242
@Input('matFormFieldNotchedOutlineOpen') open: boolean = false;
@@ -62,7 +62,9 @@ export class MatFormFieldNotchedOutline implements AfterViewInit {
6262
_getNotchWidth() {
6363
if (this.open) {
6464
const NOTCH_ELEMENT_PADDING = 8;
65-
return `${this.width > 0 ? this.width + NOTCH_ELEMENT_PADDING : 0}px`;
65+
return this.labelWidth > 0
66+
? `calc(${this.labelWidth}px * var(--mat-mdc-form-field-floating-label-scale) + ${NOTCH_ELEMENT_PADDING}px)`
67+
: '0px';
6668
}
6769

6870
return null;

src/material-experimental/mdc-form-field/form-field.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
<div class="mat-mdc-form-field-flex">
4747
<div *ngIf="_hasOutline()" matFormFieldNotchedOutline
4848
[matFormFieldNotchedOutlineOpen]="_shouldLabelFloat()"
49-
[matFormFieldNotchedOutlineWidth]="_outlineNotchWidth">
49+
[matFormFieldNotchedOutlineLabelWidth]="_labelWidth">
5050
<ng-template [ngIf]="!_forceDisplayInfixLabel()">
5151
<ng-template [ngTemplateOutlet]="labelTemplate"></ng-template>
5252
</ng-template>

src/material-experimental/mdc-form-field/form-field.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ export class MatFormField
268268
_subscriptAnimationState = '';
269269

270270
/** Width of the outline notch. */
271-
_outlineNotchWidth = 0;
271+
_labelWidth = 0;
272272

273273
/** Gets the current form field control */
274274
get _control(): MatFormFieldControl<any> {
@@ -282,7 +282,6 @@ export class MatFormField
282282
private _isFocused: boolean | null = null;
283283
private _explicitFormFieldControl: MatFormFieldControl<any>;
284284
private _needsOutlineLabelOffsetUpdateOnStable = false;
285-
private _labelScale = 0;
286285

287286
constructor(
288287
private _elementRef: ElementRef,
@@ -556,17 +555,7 @@ export class MatFormField
556555
if (!this._hasOutline() || !this._floatingLabel) {
557556
return;
558557
}
559-
if (!this._labelScale) {
560-
this._labelScale = Number(
561-
getComputedStyle(this._elementRef.nativeElement).getPropertyValue(
562-
'--mat-mdc-form-field-floating-label-scale',
563-
),
564-
);
565-
}
566-
// The outline notch should be based on the label width, but needs to respect the scaling
567-
// applied to the label if it actively floats. Since the label always floats when the notch
568-
// is open, the MDC text-field floating label scaling is respected in notch width calculation.
569-
this._outlineNotchWidth = this._floatingLabel.getWidth() * this._labelScale;
558+
this._labelWidth = this._floatingLabel.getWidth();
570559
}
571560

572561
/** Does any extra processing that is required when handling the hints. */

0 commit comments

Comments
 (0)