Skip to content

Commit d7f26c3

Browse files
committed
fix(material-experimental/theming): align form field icons with M3 (#28463)
Emits a bunch of tokens related to the form field which allows us to align the appearance with M3. (cherry picked from commit 3ccee77)
1 parent 782cc76 commit d7f26c3

File tree

5 files changed

+82
-24
lines changed

5 files changed

+82
-24
lines changed

src/material-experimental/theming/_custom-tokens.scss

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,17 @@
312312
focus-state-layer-opacity: _hardcode(0, $exclude-hardcoded),
313313
focus-select-arrow-color: map.get($systems, md-sys-color, primary),
314314
outlined-label-text-populated-size: map.get($systems, md-sys-typeface, body-large-size),
315+
316+
leading-icon-color: map.get($systems, md-sys-color, on-surface-variant),
317+
disabled-leading-icon-color: mat.private-safe-color-change(
318+
map.get($systems, md-sys-color, on-surface), $alpha: 0.38),
319+
320+
trailing-icon-color: map.get($systems, md-sys-color, on-surface-variant),
321+
disabled-trailing-icon-color: mat.private-safe-color-change(
322+
map.get($systems, md-sys-color, on-surface), $alpha: 0.38),
323+
error-focus-trailing-icon-color: map.get($systems, md-sys-color, error),
324+
error-hover-trailing-icon-color: map.get($systems, md-sys-color, on-error-container),
325+
error-trailing-icon-color: map.get($systems, md-sys-color, error),
315326
)
316327
);
317328
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ $prefix: (mat, form-field);
3939
// https://developer.mozilla.org/en-US/docs/Web/CSS/system-color
4040
select-disabled-option-text-color: if($is-dark, palette.$dark-disabled-text, GrayText),
4141

42+
// These tokens are necessary for M3. MDC has them built in already, but:
43+
// 1. They are too specific, breaking a lot of internal clients.
44+
// 2. The larger selectors result in a larger bundle.
45+
// Note: MDC has tokens for all the various states of the icons. Some of them are ommitted,
46+
// because they resolve to the same value (e.g. focus and base states for the leading icon
47+
// are the same).
48+
leading-icon-color: unset,
49+
disabled-leading-icon-color: unset,
50+
51+
trailing-icon-color: unset,
52+
disabled-trailing-icon-color: unset,
53+
error-focus-trailing-icon-color: unset,
54+
error-hover-trailing-icon-color: unset,
55+
error-trailing-icon-color: unset,
56+
4257
// These values are taken from the MDC select implementation:
4358
// https://github.com/material-components/material-components-web/blob/master/packages/mdc-select/_select-theme.scss
4459
enabled-select-arrow-color: rgba($on-surface, 0.54),

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ $prefix: (mdc, filled-text-field);
3434
label-text-populated-size: null,
3535
label-text-type: null,
3636
disabled-label-text-opacity: null,
37-
hover-label-text-color: null,
38-
error-hover-label-text-color: null,
3937
supporting-text-color: null,
4038
supporting-text-font: null,
4139
supporting-text-line-height: null,
@@ -50,27 +48,27 @@ $prefix: (mdc, filled-text-field);
5048
focus-supporting-text-color: null,
5149
hover-supporting-text-color: null,
5250
input-text-prefix-color: null,
51+
input-text-suffix-color: null,
5352
container-height: null,
53+
trailing-icon-size: null,
54+
leading-icon-size: null,
55+
trailing-icon-color: null,
5456
disabled-trailing-icon-color: null,
5557
disabled-trailing-icon-opacity: null,
5658
error-focus-trailing-icon-color: null,
5759
error-hover-trailing-icon-color: null,
5860
error-trailing-icon-color: null,
5961
focus-trailing-icon-color: null,
6062
hover-trailing-icon-color: null,
61-
trailing-icon-color: null,
62-
trailing-icon-size: null,
63-
disabled-leading-icon-color: null,
63+
leading-icon-color: null,
6464
disabled-leading-icon-opacity: null,
65+
disabled-leading-icon-color: null,
6566
error-focus-leading-icon-color: null,
6667
error-hover-leading-icon-color: null,
6768
error-leading-icon-color: null,
6869
focus-leading-icon-color: null,
6970
hover-leading-icon-color: null,
70-
leading-icon-color: null,
71-
leading-icon-size: null,
7271
input-text-type: null,
73-
input-text-suffix-color: null,
7472
input-text-font: null,
7573
input-text-line-height: null,
7674
input-text-size: null,
@@ -104,12 +102,14 @@ $prefix: (mdc, filled-text-field);
104102
disabled-container-color: _variable-safe-mix($on-surface, $surface, 2%),
105103

106104
label-text-color: rgba($on-surface, 0.6),
105+
hover-label-text-color: rgba($on-surface, 0.6),
107106
disabled-label-text-color: rgba($on-surface, 0.38),
108107

109108
input-text-color: rgba($on-surface, 0.87),
110109
disabled-input-text-color: rgba($on-surface, 0.38),
111110
input-text-placeholder-color: rgba($on-surface, 0.6),
112111

112+
error-hover-label-text-color: $warn-color,
113113
error-focus-label-text-color: $warn-color,
114114
error-label-text-color: $warn-color,
115115
error-caret-color: $warn-color,

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ $prefix: (mdc, outlined-text-field);
3131
label-text-type: null,
3232
label-text-populated-line-height: null,
3333
label-text-populated-size: null,
34-
hover-label-text-color: null,
35-
error-hover-label-text-color: null,
3634
disabled-label-text-opacity: null,
3735
disabled-supporting-text-color: null,
3836
disabled-supporting-text-opacity: null,
@@ -48,24 +46,24 @@ $prefix: (mdc, outlined-text-field);
4846
supporting-text-tracking: null,
4947
supporting-text-type: null,
5048
supporting-text-weight: null,
49+
leading-icon-size: null,
50+
trailing-icon-size: null,
51+
leading-icon-color: null,
5152
disabled-leading-icon-color: null,
5253
disabled-leading-icon-opacity: null,
5354
error-focus-leading-icon-color: null,
5455
error-hover-leading-icon-color: null,
5556
error-leading-icon-color: null,
5657
focus-leading-icon-color: null,
5758
hover-leading-icon-color: null,
58-
leading-icon-color: null,
59-
leading-icon-size: null,
59+
trailing-icon-color: null,
6060
disabled-trailing-icon-color: null,
6161
disabled-trailing-icon-opacity: null,
6262
error-focus-trailing-icon-color: null,
6363
error-hover-trailing-icon-color: null,
6464
error-trailing-icon-color: null,
6565
focus-trailing-icon-color: null,
6666
hover-trailing-icon-color: null,
67-
trailing-icon-color: null,
68-
trailing-icon-size: null,
6967
input-text-prefix-color: null,
7068
input-text-type: null,
7169
container-height: null,
@@ -94,6 +92,7 @@ $prefix: (mdc, outlined-text-field);
9492

9593
@return map.merge($color-tokens, (
9694
label-text-color: rgba($on-surface, 0.6),
95+
hover-label-text-color: rgba($on-surface, 0.6),
9796
disabled-label-text-color: rgba($on-surface, 0.38),
9897

9998
input-text-color: rgba($on-surface, 0.87),
@@ -103,6 +102,7 @@ $prefix: (mdc, outlined-text-field);
103102
error-caret-color: $warn-color,
104103
error-focus-label-text-color: $warn-color,
105104
error-label-text-color: $warn-color,
105+
error-hover-label-text-color: $warn-color,
106106

107107
// Outline tokens
108108
outline-color: rgba($on-surface, 0.38),

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

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,48 @@ $_icon-prefix-infix-padding: 4px;
142142
// Needs a z-index to ensure it's on top of other content clickable. See #27043.
143143
position: relative;
144144
z-index: 1;
145+
146+
& > .mat-icon {
147+
padding: 12px;
148+
// It's common for apps to apply `box-sizing: border-box`
149+
// globally which will break the alignment.
150+
box-sizing: content-box;
151+
}
152+
}
153+
154+
.mat-mdc-form-field-icon-prefix {
155+
@include token-utils.use-tokens(tokens-mat-form-field.$prefix,
156+
tokens-mat-form-field.get-token-slots()) {
157+
@include token-utils.create-token-slot(color, leading-icon-color);
158+
159+
.mat-form-field-disabled & {
160+
@include token-utils.create-token-slot(color, disabled-leading-icon-color);
161+
}
162+
}
163+
}
164+
165+
.mat-mdc-form-field-icon-suffix {
166+
@include token-utils.use-tokens(tokens-mat-form-field.$prefix,
167+
tokens-mat-form-field.get-token-slots()) {
168+
@include token-utils.create-token-slot(color, trailing-icon-color);
169+
170+
.mat-form-field-disabled & {
171+
@include token-utils.create-token-slot(color, disabled-trailing-icon-color);
172+
}
173+
174+
.mat-form-field-invalid & {
175+
@include token-utils.create-token-slot(color, error-trailing-icon-color);
176+
}
177+
178+
.mat-form-field-invalid:not(.mat-focused):not(.mat-form-field-disabled)
179+
.mat-mdc-text-field-wrapper:hover & {
180+
@include token-utils.create-token-slot(color, error-hover-trailing-icon-color);
181+
}
182+
183+
.mat-form-field-invalid.mat-focused .mat-mdc-text-field-wrapper & {
184+
@include token-utils.create-token-slot(color, error-focus-trailing-icon-color);
185+
}
186+
}
145187
}
146188

147189
// The prefix/suffix needs a little extra padding between the icon and the infix. Because we need to
@@ -156,16 +198,6 @@ $_icon-prefix-infix-padding: 4px;
156198
padding: 0 0 0 $_icon-prefix-infix-padding;
157199
}
158200

159-
.mat-mdc-form-field-icon-prefix,
160-
.mat-mdc-form-field-icon-suffix {
161-
& > .mat-icon {
162-
padding: 12px;
163-
// It's common for apps to apply `box-sizing: border-box`
164-
// globally which will break the alignment.
165-
box-sizing: content-box;
166-
}
167-
}
168-
169201
// Scale down icons in the subscript and floating label to be the same size
170202
// as the text.
171203
.mat-mdc-form-field-subscript-wrapper,

0 commit comments

Comments
 (0)