Skip to content

Commit 2966ae5

Browse files
committed
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.
1 parent 3f32722 commit 2966ae5

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

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

Lines changed: 2 additions & 1 deletion
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));
9293
transform: var(--mat-mdc-form-field-label-transform);
9394
}
9495

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
// For the non-upgraded notch label (i.e. when rendered on the server), also
9090
// use the correct `body1` typography level.
9191
.mdc-floating-label--float-above {
92-
font-size: mdc-typography.get-size(body1) * 0.75;
92+
font-size: calc(#{mdc-typography.get-size(body1)} * var(--mat-mdc-form-field-floating-label-scale));
9393
}
9494
.mdc-notched-outline--upgraded .mdc-floating-label--float-above {
9595
font-size: mdc-typography.get-size(body1);

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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 9 additions & 4 deletions
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',
@@ -285,6 +282,7 @@ export class MatFormField
285282
private _isFocused: boolean | null = null;
286283
private _explicitFormFieldControl: MatFormFieldControl<any>;
287284
private _needsOutlineLabelOffsetUpdateOnStable = false;
285+
private _labelScale = 0;
288286

289287
constructor(
290288
private _elementRef: ElementRef,
@@ -558,10 +556,17 @@ export class MatFormField
558556
if (!this._hasOutline() || !this._floatingLabel) {
559557
return;
560558
}
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+
}
561566
// The outline notch should be based on the label width, but needs to respect the scaling
562567
// applied to the label if it actively floats. Since the label always floats when the notch
563568
// is open, the MDC text-field floating label scaling is respected in notch width calculation.
564-
this._outlineNotchWidth = this._floatingLabel.getWidth() * LABEL_SCALE;
569+
this._outlineNotchWidth = this._floatingLabel.getWidth() * this._labelScale;
565570
}
566571

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

0 commit comments

Comments
 (0)