@ng-matero/extensions 15.4.2 → 15.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,242 @@
1
+ // TODO(mmalerba): this file should be split into separate cohesive partials for things like
2
+ // "theming", "typography", "core".
3
+
4
+ @use '../theming/theming';
5
+ @use '../typography/typography';
6
+ @use '../typography/typography-utils';
7
+ @use '@material/feature-targeting' as mdc-feature-targeting;
8
+ @use '@material/typography' as mdc-typography;
9
+ @use '@material/theme/theme-color' as mdc-theme-color;
10
+ @use '@material/theme/css' as mdc-theme-css;
11
+ @use 'sass:map';
12
+ @use 'sass:meta';
13
+
14
+ // A set of standard queries to use with MDC's queryable mixins.
15
+ $mdc-base-styles-query: mdc-feature-targeting.without(mdc-feature-targeting.any(color, typography));
16
+ $mdc-base-styles-without-animation-query:
17
+ mdc-feature-targeting.all($mdc-base-styles-query, mdc-feature-targeting.without(animation));
18
+ $mdc-theme-styles-query: color;
19
+ $mdc-typography-styles-query: typography;
20
+
21
+ // Mappings from Angular Material's typography levels to MDC's typography levels.
22
+ $mat-typography-mdc-level-mappings: (
23
+ headline-1: headline1,
24
+ headline-2: headline2,
25
+ headline-3: headline3,
26
+ headline-4: headline4,
27
+ headline-5: headline5,
28
+ headline-6: headline6,
29
+ subtitle-1: subtitle1,
30
+ subtitle-2: subtitle2,
31
+ body-1: body1,
32
+ body-2: body2,
33
+ caption: caption,
34
+ button: button,
35
+ overline: overline
36
+ );
37
+
38
+ // Converts an Angular Material typography level config to an MDC one.
39
+ @function typography-level-config-to-mdc($mat-config, $mat-level) {
40
+ $mdc-level: map.get($mat-typography-mdc-level-mappings, $mat-level);
41
+
42
+ $result-with-nulls: map.merge(
43
+ if($mdc-level,
44
+ map.get(mdc-typography.$styles, $mdc-level),
45
+ (
46
+ text-decoration: none,
47
+ -moz-osx-font-smoothing: grayscale,
48
+ -webkit-font-smoothing: antialiased
49
+ )),
50
+ if($mat-level,
51
+ (
52
+ font-size: typography-utils.font-size($mat-config, $mat-level),
53
+ line-height: typography-utils.line-height($mat-config, $mat-level),
54
+ font-weight: typography-utils.font-weight($mat-config, $mat-level),
55
+ letter-spacing: typography-utils.letter-spacing($mat-config, $mat-level),
56
+ font-family: typography-utils.font-family($mat-config, $mat-level),
57
+ // Angular Material doesn't use text-transform, so disable it.
58
+ text-transform: none,
59
+ ),
60
+ ()));
61
+
62
+ // We need to strip out any keys with a null value. Leaving them in will cause MDC to emit CSS
63
+ // variables with no fallback value, which breaks some builds.
64
+ $result: ();
65
+ @each $property, $value in $result-with-nulls {
66
+ @if $value != null {
67
+ $result: map.merge($result, ($property: $value));
68
+ }
69
+ }
70
+ @return $result;
71
+ }
72
+
73
+ // Converts an Angular Material typography config to an MDC one.
74
+ @function typography-config-to-mdc($mat-config) {
75
+ $mdc-config: ();
76
+
77
+ @each $mat-level, $mdc-level in $mat-typography-mdc-level-mappings {
78
+ $mdc-config: map.merge(
79
+ $mdc-config,
80
+ ($mdc-level: typography-level-config-to-mdc($mat-config, $mat-level)));
81
+ }
82
+
83
+ @return $mdc-config;
84
+ }
85
+
86
+ // Converts an MDC typography level config to an Angular Material one.
87
+ @function typography-config-level-from-mdc($mdc-level) {
88
+ $mdc-level-config: map.get(mdc-typography.$styles, $mdc-level);
89
+
90
+ // Explicitly set the font family to null since we'll apply it globally
91
+ // through the `define-typgraphy-config`/`define-legacy-typography-config`.
92
+ @return typography.define-typography-level(
93
+ $font-family: null,
94
+ $font-size: map.get($mdc-level-config, font-size),
95
+ $line-height: map.get($mdc-level-config, line-height),
96
+ $font-weight: map.get($mdc-level-config, font-weight),
97
+ $letter-spacing: map.get($mdc-level-config, letter-spacing)
98
+ );
99
+ }
100
+
101
+ // MDC logs a warning if the `contrast-tone` function is called with a CSS variable.
102
+ // This function falls back to determining the tone based on whether the theme is light or dark.
103
+ @function _variable-safe-contrast-tone($value, $is-dark) {
104
+ @if ($value == 'dark' or $value == 'light' or type-of($value) == 'color') {
105
+ @return mdc-theme-color.contrast-tone($value);
106
+ }
107
+
108
+ @return if($is-dark, 'light', 'dark');
109
+ }
110
+
111
+ @function _variable-safe-ink-color-for-fill($text-style, $fill-color, $is-dark) {
112
+ $contrast-tone: _variable-safe-contrast-tone($fill-color, $is-dark);
113
+ @return map.get(map.get(mdc-theme-color.$text-colors, $contrast-tone), $text-style);
114
+ }
115
+
116
+ // Configures MDC's global variables to reflect the given theme, applies the given styles,
117
+ // then resets the global variables to prevent unintended side effects.
118
+ @mixin using-mdc-theme($config) {
119
+ $primary: theming.get-color-from-palette(map.get($config, primary));
120
+ $accent: theming.get-color-from-palette(map.get($config, accent));
121
+ $warn: theming.get-color-from-palette(map.get($config, warn));
122
+ $background-palette: map.get($config, background);
123
+ $is-dark: map.get($config, is-dark);
124
+
125
+ // Save the original values.
126
+ $orig-primary: mdc-theme-color.$primary;
127
+ $orig-on-primary: mdc-theme-color.$on-primary;
128
+ $orig-secondary: mdc-theme-color.$secondary;
129
+ $orig-on-secondary: mdc-theme-color.$on-secondary;
130
+ $orig-background: mdc-theme-color.$background;
131
+ $orig-surface: mdc-theme-color.$surface;
132
+ $orig-on-surface: mdc-theme-color.$on-surface;
133
+ $orig-error: mdc-theme-color.$error;
134
+ $orig-on-error: mdc-theme-color.$on-error;
135
+ $orig-property-values: mdc-theme-color.$property-values;
136
+
137
+ // Set new values based on the given Angular Material theme.
138
+ mdc-theme-color.$primary: $primary;
139
+ mdc-theme-color.$on-primary:
140
+ if(_variable-safe-contrast-tone(mdc-theme-color.$primary, $is-dark) == 'dark', #000, #fff);
141
+ mdc-theme-color.$secondary: $accent;
142
+ mdc-theme-color.$on-secondary:
143
+ if(_variable-safe-contrast-tone(mdc-theme-color.$secondary, $is-dark) == 'dark', #000, #fff);
144
+ mdc-theme-color.$background: theming.get-color-from-palette($background-palette, background);
145
+ mdc-theme-color.$surface: theming.get-color-from-palette($background-palette, card);
146
+ mdc-theme-color.$on-surface:
147
+ if(_variable-safe-contrast-tone(mdc-theme-color.$surface, $is-dark) == 'dark', #000, #fff);
148
+ mdc-theme-color.$error: $warn;
149
+ mdc-theme-color.$on-error:
150
+ if(_variable-safe-contrast-tone(mdc-theme-color.$error, $is-dark) == 'dark', #000, #fff);
151
+ mdc-theme-color.$property-values: (
152
+ // Primary
153
+ primary: mdc-theme-color.$primary,
154
+ // Secondary
155
+ secondary: mdc-theme-color.$secondary,
156
+ // Background
157
+ background: mdc-theme-color.$background,
158
+ // Surface
159
+ surface: mdc-theme-color.$surface,
160
+ // Error
161
+ error: mdc-theme-color.$error,
162
+ on-primary: mdc-theme-color.$on-primary,
163
+ on-secondary: mdc-theme-color.$on-secondary,
164
+ on-surface: mdc-theme-color.$on-surface,
165
+ on-error: mdc-theme-color.$on-error,
166
+ // Text-primary on "background" background
167
+ text-primary-on-background:
168
+ _variable-safe-ink-color-for-fill(primary, mdc-theme-color.$background, $is-dark),
169
+ text-secondary-on-background:
170
+ _variable-safe-ink-color-for-fill(secondary, mdc-theme-color.$background, $is-dark),
171
+ text-hint-on-background:
172
+ _variable-safe-ink-color-for-fill(hint, mdc-theme-color.$background, $is-dark),
173
+ text-disabled-on-background:
174
+ _variable-safe-ink-color-for-fill(disabled, mdc-theme-color.$background, $is-dark),
175
+ text-icon-on-background:
176
+ _variable-safe-ink-color-for-fill(icon, mdc-theme-color.$background, $is-dark),
177
+ // Text-primary on "light" background
178
+ text-primary-on-light: _variable-safe-ink-color-for-fill(primary, light, $is-dark),
179
+ text-secondary-on-light: _variable-safe-ink-color-for-fill(secondary, light, $is-dark),
180
+ text-hint-on-light: _variable-safe-ink-color-for-fill(hint, light, $is-dark),
181
+ text-disabled-on-light: _variable-safe-ink-color-for-fill(disabled, light, $is-dark),
182
+ text-icon-on-light: _variable-safe-ink-color-for-fill(icon, light, $is-dark),
183
+ // Text-primary on "dark" background
184
+ text-primary-on-dark: _variable-safe-ink-color-for-fill(primary, dark, $is-dark),
185
+ text-secondary-on-dark: _variable-safe-ink-color-for-fill(secondary, dark, $is-dark),
186
+ text-hint-on-dark: _variable-safe-ink-color-for-fill(hint, dark, $is-dark),
187
+ text-disabled-on-dark: _variable-safe-ink-color-for-fill(disabled, dark, $is-dark),
188
+ text-icon-on-dark: _variable-safe-ink-color-for-fill(icon, dark, $is-dark)
189
+ );
190
+
191
+ // Apply given rules.
192
+ @include disable-mdc-fallback-declarations {
193
+ @content;
194
+ }
195
+
196
+ // Reset the original values.
197
+ mdc-theme-color.$primary: $orig-primary;
198
+ mdc-theme-color.$on-primary: $orig-on-primary;
199
+ mdc-theme-color.$secondary: $orig-secondary;
200
+ mdc-theme-color.$on-secondary: $orig-on-secondary;
201
+ mdc-theme-color.$background: $orig-background;
202
+ mdc-theme-color.$surface: $orig-surface;
203
+ mdc-theme-color.$on-surface: $orig-on-surface;
204
+ mdc-theme-color.$error: $orig-error;
205
+ mdc-theme-color.$on-error: $orig-on-error;
206
+ mdc-theme-color.$property-values: $orig-property-values;
207
+ }
208
+
209
+ // Configures MDC's global variables to reflect the given typography config,
210
+ // applies the given styles, then resets the global variables to prevent unintended side effects.
211
+ @mixin using-mdc-typography($config) {
212
+ // Save the original values.
213
+ $orig-mdc-typography-styles: mdc-typography.$styles;
214
+
215
+ // Set new values based on the given Angular Material typography configuration.
216
+ @if $config {
217
+ mdc-typography.$styles: typography-config-to-mdc($config);
218
+ }
219
+
220
+ // Apply given rules.
221
+ @include disable-mdc-fallback-declarations {
222
+ @content;
223
+ }
224
+
225
+ // Reset the original values.
226
+ mdc-typography.$styles: $orig-mdc-typography-styles;
227
+ }
228
+
229
+ // Disables MDC's CSS custom property fallbacks for the specified mixin content.
230
+ @mixin disable-mdc-fallback-declarations {
231
+ $previous-value: mdc-theme-css.$enable-fallback-declarations;
232
+ mdc-theme-css.$enable-fallback-declarations: false;
233
+ @content;
234
+ mdc-theme-css.$enable-fallback-declarations: $previous-value;
235
+ }
236
+
237
+ // Excludes the passed-in CSS content if the layout is too dense to supports touch targets.
238
+ @mixin if-touch-targets-unsupported($scale) {
239
+ @if ($scale == 'minimum' or (meta.type-of($scale) == 'number' and $scale < -1)) {
240
+ @content;
241
+ }
242
+ }