1
- // This file contains functions used to inspect Angular Material theme objects.
2
-
3
1
@use ' sass:list' ;
4
2
@use ' sass:map' ;
5
3
@use ' ../style/validation' ;
4
+ @use ' ./m2-inspection' ;
6
5
7
6
$_internals : _mat- theming- internals- do- not- access;
8
7
@@ -48,15 +47,19 @@ $_typography-properties: (font, font-family, line-height, font-size, letter-spac
48
47
/// @param {Map} $theme The theme
49
48
/// @return {String} The type of theme (either `light` or `dark`).
50
49
@function get-theme-type ($theme ) {
51
- $err : _validate-theme-object ($theme );
52
- @if $err {
53
- // TODO(mmalerba): implement for old style theme objects.
54
- @error #{' get-theme-type does not support legacy theme objects.' } ;
50
+ $version : get-theme-version ($theme );
51
+ @if $version == 0 {
52
+ @return m2-inspection .get-theme-type ($theme );
55
53
}
56
- @if not theme-has ($theme , color ) {
57
- @error ' Color information is not available on this theme.' ;
54
+ @else if $version == 1 {
55
+ @if not theme-has ($theme , color ) {
56
+ @error ' Color information is not available on this theme.' ;
57
+ }
58
+ @return map .get ($theme , $_internals , theme-type ) or light ;
59
+ }
60
+ @else {
61
+ @error #{' Unrecognized theme version:' } $version ;
58
62
}
59
- @return map .get ($theme , $_internals , theme-type ) or light ;
60
63
}
61
64
62
65
/// Gets a color from a theme object. This function can take 2 or 3 arguments. If 2 arguments are
@@ -70,14 +73,26 @@ $_typography-properties: (font, font-family, line-height, font-size, letter-spac
70
73
/// interpreted as a palette name).
71
74
/// @return {Color} The requested theme color.
72
75
@function get-theme-color ($theme , $args ... ) {
76
+ $version : get-theme-version ($theme );
73
77
$args-count : list .length ($args );
74
- @if $args-count == 1 {
75
- @return _get-theme-role-color ( $theme , $args ... ) ;
78
+ @if $args-count != 1 and $args-count != 2 {
79
+ @error #{ ' Expected 2 or 3 arguments. Got: ' } $args-count + 1 ;
76
80
}
77
- @else if $args-count == 2 {
78
- @return _get-theme-palette-color ($theme , $args ... );
81
+
82
+ @if $version == 0 {
83
+ @return m2-inspection .get-theme-color ($theme , $args ... );
84
+ }
85
+ @else if $version == 1 {
86
+ @if $args-count == 1 {
87
+ @return _get-theme-role-color ($theme , $args ... );
88
+ }
89
+ @else if $args-count == 2 {
90
+ @return _get-theme-palette-color ($theme , $args ... );
91
+ }
92
+ }
93
+ @else {
94
+ @error #{' Unrecognized theme version:' } $version ;
79
95
}
80
- @error #{' Expected 2 or 3 arguments. Got:' } $args-count + 1;
81
96
}
82
97
83
98
/// Gets a role color from a theme object.
@@ -136,72 +151,83 @@ $_typography-properties: (font, font-family, line-height, font-size, letter-spac
136
151
/// (font, font-family, font-size, font-weight, line-height, or letter-spacing).
137
152
/// @return {*} The value of the requested font property.
138
153
@function get-theme-typography ($theme , $typescale , $property : font ) {
139
- $err : _validate-theme-object ($theme );
140
- @if $err {
141
- // TODO(mmalerba): implement for old style theme objects.
142
- @error #{' get-theme-typography does not support legacy theme objects.' } ;
143
- }
144
- @if not theme-has ($theme , typography ) {
145
- @error ' Typography information is not available on this theme.' ;
146
- }
147
- @if not list .index ($_m3-typescales , $typescale ) {
148
- @error #{' Valid typescales are: #{$_m3-typescales } . Got:' } $typescale ;
149
- }
150
- @if not list .index ($_typography-properties , $property ) {
151
- @error #{' Valid typography properties are: #{$_typography-properties } . Got:' } $property ;
152
- }
153
- $property-key : map .get ((
154
- font : ' ' ,
155
- font-family : ' -font' ,
156
- line-height : ' -line-height' ,
157
- font-size : ' -size' ,
158
- letter-spacing : ' -tracking' ,
159
- font-weight : ' -weight'
160
- ), $property );
161
- $token-name : ' #{$typescale }#{$property-key } ' ;
162
- @return map .get ($theme , $_internals , typography-tokens , (mdc, typography), $token-name );
154
+ $version : get-theme-version ($theme );
155
+ @if $version == 0 {
156
+ @return m2-inspection .get-theme-typography ($theme , $typescale , $property );
157
+ }
158
+ @else if $version == 1 {
159
+ @if not theme-has ($theme , typography ) {
160
+ @error ' Typography information is not available on this theme.' ;
161
+ }
162
+ @if not list .index ($_m3-typescales , $typescale ) {
163
+ @error #{' Valid typescales are: #{$_m3-typescales } . Got:' } $typescale ;
164
+ }
165
+ @if not list .index ($_typography-properties , $property ) {
166
+ @error #{' Valid typography properties are: #{$_typography-properties } . Got:' } $property ;
167
+ }
168
+ $property-key : map .get ((
169
+ font : ' ' ,
170
+ font-family : ' -font' ,
171
+ line-height : ' -line-height' ,
172
+ font-size : ' -size' ,
173
+ letter-spacing : ' -tracking' ,
174
+ font-weight : ' -weight'
175
+ ), $property );
176
+ $token-name : ' #{$typescale }#{$property-key } ' ;
177
+ @return map .get ($theme , $_internals , typography-tokens , (mdc, typography), $token-name );
178
+ }
179
+ @else {
180
+ @error #{' Unrecognized theme version:' } $version ;
181
+ }
163
182
}
164
183
165
184
/// Gets the density scale from a theme object.
166
185
/// @param {Map} $theme The theme
167
186
/// @return {Number} The density scale.
168
187
@function get-theme-density ($theme ) {
169
- $err : _validate-theme-object ($theme );
170
- @if $err {
171
- // TODO(mmalerba): implement for old style theme objects.
172
- @error #{' get-theme-density does not support legacy theme objects.' } ;
188
+ $version : get-theme-version ($theme );
189
+ @if $version == 0 {
190
+ @return m2-inspection .get-theme-density ($theme );
191
+ }
192
+ @else if $version == 1 {
193
+ @if not theme-has ($theme , density ) {
194
+ @error ' Density information is not available on this theme.' ;
195
+ }
196
+ @return map .get ($theme , $_internals , density-scale );
173
197
}
174
- @if not theme-has ( $theme , density ) {
175
- @error ' Density information is not available on this theme. ' ;
198
+ @else {
199
+ @error #{ ' Unrecognized theme version: ' } $version ;
176
200
}
177
- @return map .get ($theme , $_internals , density-scale );
178
201
}
179
202
180
203
/// Checks whether the theme has information about given theming system.
181
204
/// @param {Map} $theme The theme
182
205
/// @param {String} $system The system to check
183
206
/// @param {Boolean} Whether the theme has information about the system.
184
207
@function theme-has ($theme , $system ) {
185
- $err : _validate -theme-object ($theme );
186
- @if $err {
187
- // TODO(mmalerba): implement for old style theme objects.
188
- @error #{ ' get-theme-density does not support legacy theme objects. ' } ;
189
- }
190
- @if $system == base {
191
- @return map .get ($theme , $_internals , base-tokens ) != null;
192
- }
193
- @if $system == color {
194
- @return map .get ($theme , $_internals , color-tokens ) != null and
208
+ $version : get -theme-version ($theme );
209
+ @if $version == 0 {
210
+ @return m2-inspection . theme-has ( $ theme, $system );
211
+ }
212
+ @else if $version == 1 {
213
+ @if $system == base {
214
+ @return map .get ($theme , $_internals , base-tokens ) != null;
215
+ }
216
+ @if $system == color {
217
+ @return map .get ($theme , $_internals , color-tokens ) != null and
195
218
map .get ($theme , $_internals , theme-type ) != null and
196
219
map .get ($theme , $_internals , palettes ) != null;
220
+ }
221
+ @if $system == typography {
222
+ @return map .get ($theme , $_internals , typography-tokens ) != null;
223
+ }
224
+ @if $system == density {
225
+ @return map .get ($theme , $_internals , density-scale ) != null;
226
+ }
227
+ @error ' Valid systems are: base, color, typography, density. Got:' $system ; }
228
+ @else {
229
+ @error #{' Unrecognized theme version:' } $version ;
197
230
}
198
- @if $system == typography {
199
- @return map .get ($theme , $_internals , typography-tokens ) != null;
200
- }
201
- @if $system == density {
202
- @return map .get ($theme , $_internals , density-scale ) != null;
203
- }
204
- @error ' Valid systems are: base, color, typography, density. Got:' $system ;
205
231
}
206
232
207
233
/// Gets the set of tokens from the given theme, limited to those affected by the requested theming
0 commit comments