Skip to content

Commit 830c8b5

Browse files
committed
refactor(material/form-field): switch custom typography styles to tokens (#27590)
Switches our custom typography styles on top of MDC to use tokens. (cherry picked from commit c0b3aa5)
1 parent b1bd998 commit 830c8b5

File tree

6 files changed

+69
-47
lines changed

6 files changed

+69
-47
lines changed

src/material/core/tokens/m2/mat/_form-field.scss

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
@use '../../../style/sass-utils';
44
@use '../../../theming/theming';
55
@use '../../../theming/palette';
6+
@use '../../../typography/typography-utils';
67

78
// The prefix used to generate the fully qualified name for tokens in this file.
89
$prefix: (mat, form-field);
@@ -57,7 +58,34 @@ $prefix: (mat, form-field);
5758

5859
// Tokens that can be configured through Angular Material's typography theming API.
5960
@function get-typography-tokens($config) {
60-
@return ();
61+
$fallback-font-family: typography-utils.font-family($config);
62+
63+
@return (
64+
// MDC uses `subtitle1` for the input value, placeholder and floating label. The spec
65+
// shows `body1` for text fields though, so we manually override the typography.
66+
// Note: Form controls inherit the typography from the parent form field.
67+
container-text-font: typography-utils.font-family($config, body-1) or $fallback-font-family,
68+
container-text-line-height: typography-utils.line-height($config, body-1),
69+
container-text-size: typography-utils.font-size($config, body-1),
70+
container-text-tracking: typography-utils.letter-spacing($config, body-1),
71+
container-text-weight: typography-utils.font-weight($config, body-1),
72+
73+
// In the container styles, we updated the floating label to use the `body-1` typography level.
74+
// The MDC notched outline overrides this accidentally (only when the label floats) to a
75+
// `rem`-based value. This results in different label widths when floated/docked and ultimately
76+
// breaks the notch width as it has been measured in the docked state (where `body-1` is
77+
// applied). We try to unset these styles set by the `mdc-notched-outline`:
78+
// https://github.com/material-components/material-components-web/blob/master/packages/mdc-notched-outline/_mixins.scss#L272-L292.
79+
// This is why we can't use their `label-text-populated-size` token and we have to declare
80+
// our own version of it.
81+
outlined-label-text-populated-size: typography-utils.font-size($config, body-1),
82+
83+
subscript-text-font: typography-utils.font-family($config, caption) or $fallback-font-family,
84+
subscript-text-line-height: typography-utils.line-height($config, caption),
85+
subscript-text-size: typography-utils.font-size($config, caption),
86+
subscript-text-tracking: typography-utils.letter-spacing($config, caption),
87+
subscript-text-weight: typography-utils.font-weight($config, caption),
88+
);
6189
}
6290

6391
// Tokens that can be configured through Angular Material's density theming API.

src/material/core/tokens/m2/mdc/_filled-text-field.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ $prefix: (mdc, filled-text-field);
8888
error-hover-state-layer-opacity: null,
8989
hover-state-layer-color: null,
9090
hover-state-layer-opacity: null,
91+
label-text-line-height: null, // We override the line height to `normal` so don't emit a slot.
9192
);
9293
}
9394

@@ -151,7 +152,6 @@ $prefix: (mdc, filled-text-field);
151152

152153
@return (
153154
label-text-font: typography-utils.font-family($config, body-1) or $fallback-font-family,
154-
label-text-line-height: typography-utils.line-height($config, body-1),
155155
label-text-size: typography-utils.font-size($config, body-1),
156156
label-text-tracking: typography-utils.letter-spacing($config, body-1),
157157
label-text-weight: typography-utils.font-weight($config, body-1),

src/material/core/tokens/m2/mdc/_outlined-text-field.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ $prefix: (mdc, outlined-text-field);
8282
error-input-text-color: null,
8383
focus-input-text-color: null,
8484
hover-input-text-color: null,
85+
label-text-line-height: null, // We override the line height to `normal` so don't emit a slot.
8586
);
8687
}
8788

@@ -134,7 +135,6 @@ $prefix: (mdc, outlined-text-field);
134135

135136
@return (
136137
label-text-font: typography-utils.font-family($config, body-1) or $fallback-font-family,
137-
label-text-line-height: typography-utils.line-height($config, body-1),
138138
label-text-size: typography-utils.font-size($config, body-1),
139139
label-text-tracking: typography-utils.letter-spacing($config, body-1),
140140
label-text-weight: typography-utils.font-weight($config, body-1),

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
@use '../core/tokens/m2/mat/form-field' as tokens-mat-form-field;
55
@use '../core/tokens/token-utils';
6-
@use '../core/theming/theming';
7-
@use '../core/mdc-helpers/mdc-helpers';
86
@use './form-field-sizing';
97

108
@mixin private-form-field-subscript() {
@@ -59,22 +57,24 @@
5957
.mat-mdc-form-field-error {
6058
display: block;
6159

62-
@include token-utils.use-tokens(
63-
tokens-mat-form-field.$prefix, tokens-mat-form-field.get-token-slots()) {
60+
@include token-utils.use-tokens(tokens-mat-form-field.$prefix,
61+
tokens-mat-form-field.get-token-slots()) {
6462
@include token-utils.create-token-slot(color, error-text-color);
6563
}
6664
}
67-
}
68-
69-
// We need to define our own typography for the subscript because we don't include MDC's
70-
// helper text in our MDC based form field
71-
@mixin private-form-field-subscript-typography($config-or-theme) {
72-
$config: theming.get-typography-config($config-or-theme);
7365

7466
// The subscript wrapper has a minimum height to avoid that the form-field
7567
// jumps when hints or errors are displayed.
7668
.mat-mdc-form-field-subscript-wrapper,
7769
.mat-mdc-form-field-bottom-align::before {
78-
@include mdc-typography.typography(caption, $query: mdc-helpers.$mdc-typography-styles-query);
70+
@include token-utils.use-tokens(tokens-mat-form-field.$prefix,
71+
tokens-mat-form-field.get-token-slots()) {
72+
@include mdc-typography.smooth-font();
73+
@include token-utils.create-token-slot(font-family, subscript-text-font);
74+
@include token-utils.create-token-slot(line-height, subscript-text-line-height);
75+
@include token-utils.create-token-slot(font-size, subscript-text-size);
76+
@include token-utils.create-token-slot(letter-spacing, subscript-text-tracking);
77+
@include token-utils.create-token-slot(font-weight, subscript-text-weight);
78+
}
7979
}
8080
}

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

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
@use '@material/textfield/filled-text-field-theme' as mdc-filled-text-field-theme;
22
@use '@material/textfield/outlined-text-field-theme' as mdc-outlined-text-field-theme;
3-
@use '@material/typography/typography' as mdc-typography;
43

54
@use '../core/tokens/m2/mdc/filled-text-field' as tokens-mdc-filled-text-field;
65
@use '../core/tokens/m2/mdc/outlined-text-field' as tokens-mdc-outlined-text-field;
76
@use '../core/tokens/m2/mat/form-field' as tokens-mat-form-field;
87
@use '../core/theming/theming';
98
@use '../core/typography/typography';
10-
@use '../core/mdc-helpers/mdc-helpers';
119
@use '../core/style/sass-utils';
1210
@use '../core/tokens/token-utils';
1311
@use './form-field-density';
14-
@use './form-field-subscript';
1512

1613
@mixin color($config-or-theme) {
1714
$config: theming.get-color-config($config-or-theme);
@@ -53,36 +50,8 @@
5350
tokens-mdc-filled-text-field.get-typography-tokens($config));
5451
@include mdc-outlined-text-field-theme.theme(
5552
tokens-mdc-outlined-text-field.get-typography-tokens($config));
56-
}
57-
58-
@include mdc-helpers.using-mdc-typography($config) {
59-
@include form-field-subscript.private-form-field-subscript-typography($config);
60-
61-
// MDC uses `subtitle1` for the input value, placeholder and floating label. The spec
62-
// shows `body1` for text fields though, so we manually override the typography.
63-
// Note: Form controls inherit the typography from the parent form field.
64-
.mat-mdc-form-field {
65-
@include mdc-typography.typography(body1, $query: mdc-helpers.$mdc-typography-styles-query);
66-
}
67-
68-
// TODO(crisbeto): we may be able to set this style with the `label-text-populated-size` token.
69-
// Above, we updated the floating label to use the `body1` typography level. The MDC notched
70-
// outline overrides this accidentally (only when the label floats) to a `rem`-based value.
71-
// This results in different label widths when floated/docked and ultimately breaks the notch
72-
// width as it has been measured in the docked state (where `body1` is applied). We try to
73-
// unset these styles set by the `mdc-notched-outline`:
74-
// https://github.com/material-components/material-components-web/blob/master/packages/mdc-notched-outline/_mixins.scss#L272-L292.
75-
.mat-mdc-form-field .mdc-text-field--outlined {
76-
// For the non-upgraded notch label (i.e. when rendered on the server), also
77-
// use the correct `body1` typography level.
78-
.mdc-floating-label--float-above {
79-
font-size: calc(#{
80-
mdc-typography.get-size(body1)} * var(--mat-mdc-form-field-floating-label-scale, 0.75));
81-
}
82-
.mdc-notched-outline--upgraded .mdc-floating-label--float-above {
83-
font-size: mdc-typography.get-size(body1);
84-
}
85-
}
53+
@include token-utils.create-token-values(tokens-mat-form-field.$prefix,
54+
tokens-mat-form-field.get-typography-tokens($config));
8655
}
8756
}
8857

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
@use '@material/line-ripple/line-ripple' as mdc-line-ripple;
99
@use '@material/line-ripple/line-ripple-theme' as mdc-line-ripple-theme;
1010
@use '@material/theme/custom-properties' as mdc-custom-properties;
11+
@use '@material/typography' as mdc-typography;
1112
@use '../core/tokens/token-utils';
1213
@use '../core/mdc-helpers/mdc-helpers';
1314
@use '../core/style/vendor-prefixes';
@@ -95,6 +96,30 @@
9596
[dir='rtl'] & {
9697
text-align: right;
9798
}
99+
100+
@include token-utils.use-tokens(tokens-mat-form-field.$prefix,
101+
tokens-mat-form-field.get-token-slots()) {
102+
@include mdc-typography.smooth-font();
103+
@include token-utils.create-token-slot(font-family, container-text-font);
104+
@include token-utils.create-token-slot(line-height, container-text-line-height);
105+
@include token-utils.create-token-slot(font-size, container-text-size);
106+
@include token-utils.create-token-slot(letter-spacing, container-text-tracking);
107+
@include token-utils.create-token-slot(font-weight, container-text-weight);
108+
109+
.mdc-text-field--outlined {
110+
$token-value: var(#{token-utils.get-token-variable(outlined-label-text-populated-size)});
111+
112+
// For the non-upgraded notch label (i.e. when rendered on the server), also
113+
// use the correct typography.
114+
.mdc-floating-label--float-above {
115+
font-size: calc(#{$token-value} * var(--mat-mdc-form-field-floating-label-scale));
116+
}
117+
118+
.mdc-notched-outline--upgraded .mdc-floating-label--float-above {
119+
font-size: $token-value;
120+
}
121+
}
122+
}
98123
}
99124

100125
// Container that contains the prefixes, infix and suffixes. These elements should

0 commit comments

Comments
 (0)