Skip to content

Commit 278e67b

Browse files
authored
fix(material/form-field): safely coerce line-height to em (#23215)
Our typography mixins for form-field related components coerce line-height to `em` units in several places. If a user provides a `line-height` via typography config with a unit specified, this results in an error as the result with be em squared. This fix changes the coercion to only occur if the line-height is unitless.
1 parent 2a2cd9c commit 278e67b

7 files changed

+20
-7
lines changed

src/material/core/typography/_typography-utils.scss

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@use 'sass:list';
22
@use 'sass:map';
3+
@use 'sass:math';
34
@use 'sass:meta';
45
@use 'sass:string';
56
@use '../style/private';
@@ -96,3 +97,9 @@
9697
@include font-shorthand($font-size, $font-weight, $line-height, $font-family);
9798
letter-spacing: letter-spacing($config, $level);
9899
}
100+
101+
/// Coerce a value to `em` if it is a unitless number, otherwise returns
102+
/// the value provided.
103+
@function private-coerce-unitless-to-em($value) {
104+
@return if(math.is-unitless($value), 1em * $value, $value);
105+
}

src/material/form-field/_form-field-fill-theme.scss

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ $fill-dedupe: 0;
7272
// The padding below the infix.
7373
$infix-padding-bottom: 0.75em;
7474
// The margin applied to the form-field-infix to reserve space for the floating label.
75-
$infix-margin-top: 1em * $line-height * $subscript-font-scale;
75+
$infix-margin-top:
76+
$subscript-font-scale * typography-utils.private-coerce-unitless-to-em($line-height);
7677
// The amount we offset the label from the input text in the fill appearance.
7778
$fill-appearance-label-offset: -0.5em;
7879

src/material/form-field/_form-field-legacy-theme.scss

+3-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ $legacy-dedupe: 0;
8585
// of the text itself, not the edge of the line; therefore we subtract off the line spacing.
8686
$infix-padding: 0.5em - $line-spacing;
8787
// The margin applied to the form-field-infix to reserve space for the floating label.
88-
$infix-margin-top: 1em * $line-height * $subscript-font-scale;
88+
// If the line-height is given as a unitless number, coerce it to `em`.
89+
$infix-margin-top:
90+
$subscript-font-scale * typography-utils.private-coerce-unitless-to-em($line-height);
8991
// The space between the bottom of the .mat-form-field-flex area and the subscript wrapper.
9092
// Mocks show half of the text size, but this margin is applied to an element with the subscript
9193
// text font size, so we need to divide by the scale factor to make it half of the original text

src/material/form-field/_form-field-outline-theme.scss

+2-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ $outline-dedupe: 0;
9393
// The padding above and below the infix.
9494
$infix-padding: 1em;
9595
// The margin applied to the form-field-infix to reserve space for the floating label.
96-
$infix-margin-top: 1em * $line-height * $subscript-font-scale;
96+
$infix-margin-top:
97+
$subscript-font-scale * typography-utils.private-coerce-unitless-to-em($line-height);
9798
// The space between the bottom of the .mat-form-field-flex area and the subscript wrapper.
9899
// Mocks show half of the text size, but this margin is applied to an element with the subscript
99100
// text font size, so we need to divide by the scale factor to make it half of the original text

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ $dedupe: 0;
146146
// The padding on the infix. Mocks show half of the text size.
147147
$infix-padding: 0.5em;
148148
// The margin applied to the form-field-infix to reserve space for the floating label.
149-
$infix-margin-top: 1em * $line-height * $subscript-font-scale;
149+
// If the line-height is given as a unitless number, coerce it to `em`.
150+
$infix-margin-top: $subscript-font-scale *
151+
typography-utils.private-coerce-unitless-to-em($line-height);
150152
// Font size to use for the label and subscript text.
151153
$subscript-font-size: $subscript-font-scale * 100%;
152154
// Font size to use for the for the prefix and suffix icons.
@@ -183,7 +185,7 @@ $dedupe: 0;
183185
width: $prefix-suffix-icon-font-scale * 1em;
184186

185187
.mat-icon {
186-
height: $line-height * 1em;
188+
height: typography-utils.private-coerce-unitless-to-em($line-height);
187189
line-height: $line-height;
188190
}
189191
}

src/material/input/_input-theme.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
// <input> elements seem to have their height set slightly too large on Safari causing the text to
7575
// be misaligned w.r.t. the placeholder. Adding this margin corrects it.
7676
input.mat-input-element {
77-
margin-top: -$line-spacing * 1em;
77+
margin-top: typography-utils.private-coerce-unitless-to-em(-$line-spacing);
7878
}
7979
}
8080

src/material/select/_select-theme.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
}
7676

7777
.mat-select-trigger {
78-
height: $line-height * 1em;
78+
height: typography-utils.private-coerce-unitless-to-em($line-height);
7979
}
8080
}
8181

0 commit comments

Comments
 (0)