Skip to content

Commit d836e27

Browse files
committed
fix(material-experimental/mdc-form-field): use a CSS var for the floating label scale (#25178)
* fix(material-experimental/mdc-form-field): use a CSS var for the floating label scale This allows the value to be shared across CSS & JS. * fixup! fix(material-experimental/mdc-form-field): use a CSS var for the floating label scale * fixup! fix(material-experimental/mdc-form-field): use a CSS var for the floating label scale * fixup! fix(material-experimental/mdc-form-field): use a CSS var for the floating label scale * fixup! fix(material-experimental/mdc-form-field): use a CSS var for the floating label scale (cherry picked from commit 47dfcb3)
1 parent 97c00a0 commit d836e27

File tree

6 files changed

+16
-15
lines changed

6 files changed

+16
-15
lines changed

src/material-experimental/mdc-form-field/_form-field-density.scss

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@
8888
.mat-mdc-text-field-wrapper.mdc-text-field--outlined .mdc-notched-outline--upgraded
8989
.mdc-floating-label--float-above {
9090
--mat-mdc-form-field-label-transform: translateY(
91-
-#{mdc-textfield.get-outlined-label-position-y($height)}) scale(0.75);
91+
-#{mdc-textfield.get-outlined-label-position-y($height)})
92+
scale(var(--mat-mdc-form-field-floating-label-scale, 0.75));
9293
transform: var(--mat-mdc-form-field-label-transform);
9394
}
9495

src/material-experimental/mdc-form-field/_form-field-theme.scss

+2-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@
9090
// For the non-upgraded notch label (i.e. when rendered on the server), also
9191
// use the correct `body1` typography level.
9292
.mdc-floating-label--float-above {
93-
font-size: mdc-typography.get-size(body1) * 0.75;
93+
font-size: calc(#{
94+
mdc-typography.get-size(body1)} * var(--mat-mdc-form-field-floating-label-scale, 0.75));
9495
}
9596
.mdc-notched-outline--upgraded .mdc-floating-label--float-above {
9697
font-size: mdc-typography.get-size(body1);

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ import {
3535
encapsulation: ViewEncapsulation.None,
3636
})
3737
export class MatFormFieldNotchedOutline implements AfterViewInit {
38-
/** Width of the notch. */
39-
@Input('matFormFieldNotchedOutlineWidth') width: number = 0;
38+
/** Width of the label (original scale) */
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, 0.75) + ${NOTCH_ELEMENT_PADDING}px)`
67+
: '0px';
6668
}
6769

6870
return null;

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

+1-1
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.scss

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
// Host element of the form-field. It contains the mdc-text-field wrapper
3737
// and the subscript wrapper.
3838
.mat-mdc-form-field {
39+
// The scale to use for the form-field's label when its in the floating position.
40+
--mat-mdc-form-field-floating-label-scale: 0.75;
41+
3942
display: inline-flex;
4043
// This container contains the text-field and the subscript. The subscript
4144
// should be displayed below the text-field. Hence the column direction.

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

+3-9
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,6 @@ const FLOATING_LABEL_DEFAULT_DOCKED_TRANSFORM = `translateY(-50%)`;
114114
*/
115115
const WRAPPER_HORIZONTAL_PADDING = 16;
116116

117-
/** Amount by which to scale the label when the form field is focused. */
118-
const LABEL_SCALE = 0.75;
119-
120117
/** Container for form controls that applies Material Design styling and behavior. */
121118
@Component({
122119
selector: 'mat-form-field',
@@ -270,8 +267,8 @@ export class MatFormField
270267
/** State of the mat-hint and mat-error animations. */
271268
_subscriptAnimationState = '';
272269

273-
/** Width of the outline notch. */
274-
_outlineNotchWidth = 0;
270+
/** Width of the label element (at scale=1). */
271+
_labelWidth = 0;
275272

276273
/** Gets the current form field control */
277274
get _control(): MatFormFieldControl<any> {
@@ -558,10 +555,7 @@ export class MatFormField
558555
if (!this._hasOutline() || !this._floatingLabel) {
559556
return;
560557
}
561-
// The outline notch should be based on the label width, but needs to respect the scaling
562-
// applied to the label if it actively floats. Since the label always floats when the notch
563-
// is open, the MDC text-field floating label scaling is respected in notch width calculation.
564-
this._outlineNotchWidth = this._floatingLabel.getWidth() * LABEL_SCALE;
558+
this._labelWidth = this._floatingLabel.getWidth();
565559
}
566560

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

0 commit comments

Comments
 (0)