-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
_theme.scss
369 lines (344 loc) · 11.6 KB
/
_theme.scss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
//
// Copyright 2017 Google Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
@use 'sass:list';
@use 'sass:map';
@use 'sass:meta';
@use '@material/feature-targeting/feature-targeting';
@use './css';
@use './custom-properties';
@use './gss';
@use './keys';
@use './replace';
@use './theme-color';
@mixin core-styles($query: feature-targeting.all()) {
$feat-color: feature-targeting.create-target($query, color);
:root {
@include feature-targeting.targets($feat-color) {
@each $style in theme-color.get-theme-keys() {
@include custom-properties.declaration(
keys.create-custom-property($style)
);
}
}
}
@each $style in theme-color.get-theme-keys() {
@if $style != 'background' and $style != 'surface' {
.mdc-theme--#{$style} {
@include feature-targeting.targets($feat-color) {
@include property(color, $style);
}
}
} @else {
.mdc-theme--#{$style} {
@include feature-targeting.targets($feat-color) {
@include property(background-color, $style);
}
}
}
}
// CSS rules for using primary and secondary (plus light/dark variants) as background colors.
@each $style in ('primary', 'secondary') {
.mdc-theme--#{$style}-bg {
@include feature-targeting.targets($feat-color) {
@include property(background-color, $style);
}
}
}
}
/// Applies a dynamic value to the specified property. This mixin should be used
/// in theme style mixins when setting properties.
///
/// The value may be any of the following:
/// - a standard CSS value
/// - a custom property Map, e.g. (varname: --mdc-foo, fallback: blue)
/// - a Material theme key String, e.g. 'primary', 'on-primary'
///
/// @example
/// @include theme.property(color, teal);
/// @include theme.property(color, custom-properties.create(foo, blue));
/// @include theme.property(color, primary);
///
/// A `$replace` Map parameter may be provided to replace key/value pairs for
/// string values. This can be used to substitute parameters in complex string
/// values such as `calc()` with custom properties.
///
/// @example
/// @include theme.property(
/// width,
/// calc(foo + bar),
/// $replace: (foo: custom-properties.create(foo), bar: 8px)
/// );
///
/// Note: Material theme key Strings (e.g. `primary`) are not supported as
/// replacement values.
///
/// A CSS custom property declaration may be emitted by providing a custom
/// property Map to `$property`. The fallback value (or `$value` if provided)
/// will be used as the declaration value.
///
/// @example - scss
/// .foo {
/// @include theme.property(custom-properties.create(foo, teal));
/// @include theme.property(custom-properties.create(bar, teal), blue);
/// }
///
/// @example - css
/// .foo {
/// --mdc-foo: teal;
/// --mdc-bar: blue;
/// }
///
/// @param {String | Map} $property - The name of the CSS property. May also be
/// a custom property Map to emit a custom propery declaration.
/// @param {String | Number | Color | List | Map} $value - The property's value.
/// This parameter may be omitted if `$property` is a custom property Map.
/// @param {Map} $gss - Optional Map of GSS annotations to set.
/// @param {Map} $replace - An optional Map of replacement key/value pairs if
/// the `$value` is a string.
/// @param {Bool} $important - Set to true to add an `!important` rule. Defaults
/// to false.
@mixin property(
$property,
$value: null,
$gss: (),
$replace: null,
$important: false
) {
@if custom-properties.is-custom-prop($property) {
// $property is a custom property Map
// --mdc-foo: value;
@if $value {
$property: custom-properties.set-fallback(
$property,
$value,
$shallow: true
);
}
@include custom-properties.declaration(
$property,
$gss: $gss,
$important: $important
);
} @else if custom-properties.is-custom-prop($value) {
// $value is a custom property Map
// property: var(--mdc-foo, fallback);
@include custom-properties.declaration(
$property,
$value,
$gss: $gss,
$important: $important
);
} @else if keys.is-key($value) {
// $value is a key String
// property: key;
$custom-prop: keys.create-custom-property($value);
@if theme-color.is-theme-key($value) {
// Determine if we need to use a compile-time updated value to support
// Angular.
$key: $value;
// (changed: Bool, value: *)
$result: theme-color.deprecated-get-global-theme-key-value-if-changed(
$key
);
@if map.get($result, changed) {
// $mdc-theme-property-values was changed at compile time. Use the
// global value instead. Otherwise if it was not changed, continue
// using the key store normally.
$custom-prop: keys.create-custom-property($key);
$custom-prop: custom-properties.set-fallback(
$custom-prop,
map.get($result, value)
);
}
}
@include custom-properties.declaration(
$property,
$custom-prop,
$gss: $gss,
$important: $important
);
} @else {
// $value is a standard CSS value
// property: value;
$fallback: null;
@if $replace {
// If any replacements are null, treat the entire value as null (do not
// emit anything).
@each $name, $replacement in $replace {
@if $replacement == null {
$value: null;
}
}
}
@if $replace and $value {
@if meta.type-of($replace) != 'map' {
@error 'mdc-theme: Invalid replacement #{$replace}. Must be a Map.';
}
$replace-map-fallback: ();
$replace-map-value: ();
$needs-fallback: false;
@each $name, $replacement in $replace {
@if custom-properties.is-custom-prop($replacement) {
$replace-value: custom-properties.get-declaration-value($replacement);
$replace-fallback: custom-properties.get-declaration-fallback(
$replacement
);
@if $replace-fallback {
$needs-fallback: true;
}
$replace-map-value: map.set(
$replace-map-value,
$name,
$replace-value
);
$replace-map-fallback: map.set(
$replace-map-fallback,
$name,
$replace-fallback
);
} @else {
$replace-map-value: map.set($replace-map-value, $name, $replacement);
$replace-map-fallback: map.set(
$replace-map-fallback,
$name,
$replacement
);
}
}
@if meta.type-of($value) == 'string' {
@if $needs-fallback {
$fallback: replace.replace-string($value, $replace-map-fallback);
}
$value: replace.replace-string($value, $replace-map-value);
} @else if meta.type-of($value) == 'list' {
@if $needs-fallback {
$fallback: replace.replace-list($value, $replace-map-fallback);
}
$value: replace.replace-list($value, $replace-map-value);
} @else {
@error 'mdc-theme: Invalid replacement value #{$value}. $replace may only be used with string or list values.';
}
}
@include css.declaration(
$property,
$value,
$fallback-value: $fallback,
$gss: $gss,
$important: $important
);
}
}
// @deprecated use the `property()` mixin instead
@mixin prop($property, $style) {
@include property($property, $style);
}
/// Validates theme configuration keys by comparing it with original theme
/// configuration, also validates theme values to see if it has any unsupported
/// value formats.
///
/// Use this in internal `theme()` mixins to validate library-provided
/// `$theme` maps and ensure that all tokens are correct and present.
///
/// @example
/// @mixin theme($theme) {
/// @include theme.validate-theme($light-theme, $theme);
/// ...
/// }
///
/// @see validate-theme-styles to validate only theme keys.
///
/// @param {Map} $origin-theme - Original theme configuration in Sass map format
/// that has all supported keys.
/// @param {Map} $custom-theme - Provided theme configuration in Sass map format
/// that should be validated against `$origin-theme`.
@mixin validate-theme($origin-theme, $custom-theme, $test-only: false) {
@include validate-theme-styles(
$origin-theme,
$custom-theme,
$test-only: $test-only
);
@include _validate-theme-values($custom-theme, $test-only: $test-only);
}
/// Validates theme configuration keys by comparing it with original theme
/// configuration.
///
/// Use this in internal `theme-styles()` mixins to validate library-provided
/// `$theme` maps and ensure that all tokens are correct and present.
///
/// @example
/// @mixin theme-styles($theme) {
/// @include theme.validate-theme-styles($light-theme, $theme);
/// ...
/// }
///
/// @see validate-theme to validate both theme keys and theme values.
///
/// @param {Map} $origin-theme - Original theme configuration in Sass map format
/// that has all supported keys.
/// @param {Map} $custom-theme - Provided theme configuration in Sass map format
/// that should be validated against `$origin-theme`.
@mixin validate-theme-styles($origin-theme, $custom-theme, $test-only: false) {
$origin-keys: map.keys($origin-theme);
$unsupported-keys: ();
@each $key, $value in $custom-theme {
@if (not list.index($origin-keys, $key)) {
$unsupported-keys: list.append(
$unsupported-keys,
$key,
$separator: comma
);
}
}
@if list.length($unsupported-keys) > 0 {
$error-message: 'Unsupported keys found: #{$unsupported-keys}. Expected one of: #{$origin-keys}.';
@if $test-only {
content: $error-message;
} @else {
@error $error-message;
}
}
}
/// Validates theme configuration values to see if it has any unsupported value
/// formats.
/// @see Use `validate-theme()` to validate both theme keys and theme values.
/// @param {Map} $custom-theme - Provided theme configuration in Sass map format
/// that needs to be validated.
@mixin _validate-theme-values($custom-theme, $test-only: false) {
$unsupported-custom-prop-keys: ();
@each $key, $value in $custom-theme {
@if custom-properties.is-custom-prop($value) {
$unsupported-custom-prop-keys: list.append(
$unsupported-custom-prop-keys,
$key,
$separator: comma
);
}
}
@if list.length($unsupported-custom-prop-keys) > 0 {
$error-message: 'Custom properties are not supported for theme map keys: #{$unsupported-custom-prop-keys}';
@if $test-only {
content: $error-message;
} @else {
@error $error-message;
}
}
}