UNPKG

14.3 kBSCSSView Raw
1//
2// Copyright 2022 Google Inc.
3//
4// Permission is hereby granted, free of charge, to any person obtaining a copy
5// of this software and associated documentation files (the "Software"), to deal
6// in the Software without restriction, including without limitation the rights
7// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8// copies of the Software, and to permit persons to whom the Software is
9// furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20// THE SOFTWARE.
21//
22
23// Selector '.mdc-*' should only be used in this project.
24// stylelint-disable selector-class-pattern --
25// NOTE: this is the implementation of the aforementioned classes.
26
27@use 'sass:map';
28@use '@material/tokens/resolvers';
29@use '@material/floating-label/mixins' as floating-label-mixins;
30@use '@material/theme/theme';
31@use '@material/typography/typography';
32
33$_light-theme: (
34 active-indicator-color: null,
35 active-indicator-height: null,
36 caret-color: null,
37 container-color: null,
38 container-height: null,
39 container-shape: null,
40 disabled-active-indicator-color: null,
41 disabled-active-indicator-height: null,
42 disabled-active-indicator-opacity: null,
43 disabled-container-color: null,
44 disabled-container-opacity: null,
45 disabled-input-text-color: null,
46 disabled-input-text-opacity: null,
47 disabled-label-text-color: null,
48 disabled-label-text-opacity: null,
49 disabled-leading-icon-color: null,
50 disabled-leading-icon-opacity: null,
51 disabled-outline-color: null,
52 disabled-outline-opacity: null,
53 disabled-outline-width: null,
54 disabled-supporting-text-color: null,
55 disabled-supporting-text-opacity: null,
56 disabled-trailing-icon-color: null,
57 disabled-trailing-icon-opacity: null,
58 error-active-indicator-color: null,
59 // Token key `error-caret-color` not available on DSDB.
60 error-caret-color: null,
61 error-focus-active-indicator-color: null,
62 error-focus-caret-color: null,
63 error-focus-input-text-color: null,
64 error-focus-label-text-color: null,
65 error-focus-leading-icon-color: null,
66 error-focus-outline-color: null,
67 error-focus-supporting-text-color: null,
68 error-focus-trailing-icon-color: null,
69 error-hover-active-indicator-color: null,
70 // Token key `error-hover-caret-color` not available on DSDB.
71 error-hover-caret-color: null,
72 error-hover-input-text-color: null,
73 error-hover-label-text-color: null,
74 error-hover-leading-icon-color: null,
75 error-hover-outline-color: null,
76 error-hover-state-layer-color: null,
77 error-hover-state-layer-opacity: null,
78 error-hover-supporting-text-color: null,
79 error-hover-trailing-icon-color: null,
80 error-input-text-color: null,
81 error-label-text-color: null,
82 error-leading-icon-color: null,
83 error-outline-color: null,
84 error-supporting-text-color: null,
85 error-trailing-icon-color: null,
86 focus-active-indicator-color: null,
87 focus-active-indicator-height: null,
88 // Token key `focus-caret-color` not available on DSDB.
89 focus-caret-color: null,
90 focus-input-text-color: null,
91 focus-label-text-color: null,
92 focus-leading-icon-color: null,
93 focus-outline-color: null,
94 focus-outline-width: null,
95 focus-supporting-text-color: null,
96 focus-trailing-icon-color: null,
97 hover-active-indicator-color: null,
98 hover-active-indicator-height: null,
99 // Token key `hover-caret-color` not available on DSDB.
100 hover-caret-color: null,
101 hover-input-text-color: null,
102 hover-label-text-color: null,
103 hover-leading-icon-color: null,
104 hover-outline-color: null,
105 hover-outline-width: null,
106 hover-state-layer-color: null,
107 hover-state-layer-opacity: null,
108 hover-supporting-text-color: null,
109 hover-trailing-icon-color: null,
110 input-text-color: null,
111 input-text-font: null,
112 input-text-line-height: null,
113 input-text-size: null,
114 input-text-tracking: null,
115 input-text-type: null,
116 input-text-weight: null,
117 label-text-color: null,
118 label-text-font: null,
119 label-text-line-height: null,
120 label-text-populated-line-height: null,
121 label-text-populated-size: null,
122 label-text-size: null,
123 label-text-tracking: null,
124 label-text-type: null,
125 label-text-weight: null,
126 leading-icon-color: null,
127 leading-icon-size: null,
128 outline-color: null,
129 outline-width: null,
130 supporting-text-color: null,
131 supporting-text-font: null,
132 supporting-text-line-height: null,
133 supporting-text-size: null,
134 supporting-text-tracking: null,
135 supporting-text-type: null,
136 supporting-text-weight: null,
137 trailing-icon-color: null,
138 trailing-icon-size: null,
139);
140
141@mixin theme-styles($theme, $resolvers: resolvers.$material) {
142 @include theme.validate-theme-keys($_light-theme, $theme);
143
144 @include _caret-color(
145 (
146 default: map.get($theme, caret-color),
147 hover: map.get($theme, hover-caret-color),
148 focus: map.get($theme, focus-caret-color),
149 )
150 );
151 @include _error-caret-color(
152 (
153 default: map.get($theme, error-caret-color),
154 hover: map.get($theme, error-hover-caret-color),
155 focus: map.get($theme, error-focus-caret-color),
156 )
157 );
158 @include _input-text-color(
159 (
160 default: map.get($theme, input-text-color),
161 hover: map.get($theme, hover-input-text-color),
162 focus: map.get($theme, focus-input-text-color),
163 disabled: map.get($theme, disabled-input-text-color),
164 )
165 );
166 @include _error-input-text-color(
167 (
168 default: map.get($theme, error-input-text-color),
169 hover: map.get($theme, error-hover-input-text-color),
170 focus: map.get($theme, error-focus-input-text-color),
171 )
172 );
173 @include _label-text-color(
174 (
175 default: map.get($theme, label-text-color),
176 hover: map.get($theme, hover-label-text-color),
177 focus: map.get($theme, focus-label-text-color),
178 disabled: map.get($theme, disabled-label-text-color),
179 )
180 );
181 @include _error-label-text-color(
182 (
183 default: map.get($theme, error-label-text-color),
184 hover: map.get($theme, error-hover-label-text-color),
185 focus: map.get($theme, error-focus-label-text-color),
186 )
187 );
188 @include _leading-icon-color(
189 (
190 default: map.get($theme, leading-icon-color),
191 hover: map.get($theme, hover-leading-icon-color),
192 focus: map.get($theme, focus-leading-icon-color),
193 disabled: map.get($theme, disabled-leading-icon-color),
194 )
195 );
196 @include _error-leading-icon-color(
197 (
198 default: map.get($theme, error-leading-icon-color),
199 hover: map.get($theme, error-hover-leading-icon-color),
200 focus: map.get($theme, error-focus-leading-icon-color),
201 )
202 );
203 @include _trailing-icon-color(
204 (
205 default: map.get($theme, trailing-icon-color),
206 hover: map.get($theme, hover-trailing-icon-color),
207 focus: map.get($theme, focus-trailing-icon-color),
208 disabled: map.get($theme, disabled-trailing-icon-color),
209 )
210 );
211 @include _error-trailing-icon-color(
212 (
213 default: map.get($theme, error-trailing-icon-color),
214 hover: map.get($theme, error-hover-trailing-icon-color),
215 focus: map.get($theme, error-focus-trailing-icon-color),
216 )
217 );
218 @include _supporting-text-color(
219 (
220 default: map.get($theme, supporting-text-color),
221 hover: map.get($theme, hover-supporting-text-color),
222 focus: map.get($theme, focus-supporting-text-color),
223 disabled: map.get($theme, disabled-supporting-text-color),
224 )
225 );
226 @include _error-supporting-text-color(
227 (
228 default: map.get($theme, error-supporting-text-color),
229 hover: map.get($theme, error-hover-supporting-text-color),
230 focus: map.get($theme, error-focus-supporting-text-color),
231 )
232 );
233 @include _input-text-typography(
234 (
235 font: map.get($theme, input-text-font),
236 line-height: map.get($theme, input-text-line-height),
237 size: map.get($theme, input-text-size),
238 tracking: map.get($theme, input-text-tracking),
239 weight: map.get($theme, input-text-weight),
240 )
241 );
242 @include _label-text-typography(
243 (
244 font: map.get($theme, label-text-font),
245 line-height: map.get($theme, label-text-line-height),
246 size: map.get($theme, label-text-size),
247 tracking: map.get($theme, label-text-tracking),
248 weight: map.get($theme, label-text-weight),
249 )
250 );
251 @include _label-text-populated-typography(
252 (
253 line-height: map.get($theme, label-text-populated-line-height),
254 size: map.get($theme, label-text-populated-size),
255 )
256 );
257 @include _supporting-text-typography(
258 (
259 font: map.get($theme, supporting-text-font),
260 line-height: map.get($theme, supporting-text-line-height),
261 size: map.get($theme, supporting-text-size),
262 tracking: map.get($theme, supporting-text-tracking),
263 weight: map.get($theme, supporting-text-weight),
264 )
265 );
266}
267
268@mixin _caret-color($colors) {
269 @include if-enabled {
270 @include _set-caret-color(map.get($colors, default));
271
272 @include if-hovered {
273 @include _set-caret-color(map.get($colors, hover));
274 }
275
276 @include if-focused {
277 @include _set-caret-color(map.get($colors, focus));
278 }
279 }
280}
281
282@mixin _set-caret-color($color) {
283 .mdc-text-field__input {
284 caret-color: $color;
285 }
286}
287
288@mixin _error-caret-color($colors) {
289 &.mdc-text-field--invalid {
290 @include _caret-color($colors);
291 }
292}
293
294@mixin _input-text-color($colors) {
295 @include if-enabled {
296 @include _set-input-text-color(map.get($colors, default));
297
298 @include if-hovered {
299 @include _set-input-text-color(map.get($colors, hover));
300 }
301
302 @include if-focused {
303 @include _set-input-text-color(map.get($colors, focus));
304 }
305 }
306
307 @include if-disabled {
308 @include _set-input-text-color(map.get($colors, disabled));
309 }
310}
311
312@mixin _set-input-text-color($color) {
313 @if $color {
314 .mdc-text-field__input {
315 @include theme.property(color, $color);
316 }
317 }
318}
319
320@mixin _error-input-text-color($color) {
321 &.mdc-text-field--invalid {
322 @include _input-text-color($color);
323 }
324}
325
326@mixin _label-text-color($colors) {
327 @include if-enabled {
328 @include _set-label-text-color(map.get($colors, default));
329
330 @include if-focused {
331 @include _set-label-text-color(map.get($colors, focus));
332 }
333
334 @include if-hovered {
335 @include _set-label-text-color(map.get($colors, hover));
336 }
337 }
338
339 @include if-disabled {
340 @include _set-label-text-color(map.get($colors, disabled));
341 }
342}
343
344@mixin _set-label-text-color($color) {
345 .mdc-floating-label,
346 .mdc-floating-label--float-above {
347 @include floating-label-mixins.ink-color($color);
348 }
349}
350
351@mixin _error-label-text-color($color) {
352 &.mdc-text-field--invalid {
353 @include _label-text-color($color);
354 }
355}
356
357@mixin _leading-icon-color($colors) {
358 @include if-enabled {
359 @include _set-leading-icon-color(map.get($colors, default));
360
361 @include if-hovered {
362 @include _set-leading-icon-color(map.get($colors, hover));
363 }
364
365 @include if-focused {
366 @include _set-leading-icon-color(map.get($colors, focus));
367 }
368 }
369
370 @include if-disabled {
371 @include _set-leading-icon-color(map.get($colors, disabled));
372 }
373}
374
375@mixin _set-leading-icon-color($color) {
376 .mdc-text-field__icon--leading {
377 @include theme.property(color, $color);
378 }
379}
380
381@mixin _error-leading-icon-color($color) {
382 &.mdc-text-field--invalid {
383 @include _leading-icon-color($color);
384 }
385}
386
387@mixin _trailing-icon-color($colors) {
388 @include if-enabled {
389 @include _set-trailing-icon-color(map.get($colors, default));
390
391 @include if-hovered {
392 @include _set-trailing-icon-color(map.get($colors, hover));
393 }
394
395 @include if-focused {
396 @include _set-trailing-icon-color(map.get($colors, focus));
397 }
398 }
399
400 @include if-disabled {
401 @include _set-trailing-icon-color(map.get($colors, disabled));
402 }
403}
404
405@mixin _set-trailing-icon-color($color) {
406 .mdc-text-field__icon--trailing {
407 @include theme.property(color, $color);
408 }
409}
410
411@mixin _error-trailing-icon-color($color) {
412 &.mdc-text-field--invalid {
413 @include _trailing-icon-color($color);
414 }
415}
416
417@mixin _supporting-text-color($colors) {
418 @include if-enabled {
419 @include _set-supporting-text-color(map.get($colors, default));
420
421 @include if-hovered {
422 @include _set-supporting-text-color(map.get($colors, hover));
423 }
424
425 @include if-focused {
426 @include _set-supporting-text-color(map.get($colors, focus));
427 }
428 }
429
430 @include if-disabled {
431 @include _set-supporting-text-color(map.get($colors, disabled));
432 }
433}
434
435@mixin _error-supporting-text-color($color) {
436 &.mdc-text-field--invalid {
437 @include _supporting-text-color($color);
438 }
439}
440
441@mixin _set-supporting-text-color($color) {
442 & + .mdc-text-field-helper-text {
443 @include theme.property(color, $color);
444 }
445}
446
447@mixin _input-text-typography($typography-theme) {
448 .mdc-text-field__input {
449 @include typography.theme-styles($typography-theme);
450 }
451}
452
453@mixin _label-text-typography($typography-theme) {
454 .mdc-floating-label {
455 @include typography.theme-styles($typography-theme);
456 }
457}
458
459@mixin _label-text-populated-typography($typography-theme) {
460 .mdc-floating-label--float-above {
461 @include typography.theme-styles($typography-theme);
462 }
463}
464
465@mixin _supporting-text-typography($typography-theme) {
466 & + .mdc-text-field-helper-text {
467 @include typography.theme-styles($typography-theme);
468 }
469}
470
471/// Selector for hovered state
472@mixin if-hovered {
473 &:not(.mdc-text-field--focused):hover {
474 @content;
475 }
476}
477
478/// Selector for focused state
479@mixin if-focused {
480 &.mdc-text-field--focused {
481 @content;
482 }
483}
484
485/// Selector for enabled state
486@mixin if-enabled {
487 &:not(.mdc-text-field--disabled) {
488 @content;
489 }
490}
491
492/// Selector for disabled state
493@mixin if-disabled {
494 &.mdc-text-field--disabled {
495 @content;
496 }
497}