1- // This file contains functions used to inspect Angular Material theme objects.
2-
31@use ' sass:list' ;
42@use ' sass:map' ;
53@use ' ../style/validation' ;
4+ @use ' ./m2-inspection' ;
65
76$_internals : _mat- theming- internals- do- not- access;
87
@@ -48,15 +47,19 @@ $_typography-properties: (font, font-family, line-height, font-size, letter-spac
4847/// @param {Map} $theme The theme
4948/// @return {String} The type of theme (either `light` or `dark`).
5049@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 );
5553 }
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 ;
5862 }
59- @return map .get ($theme , $_internals , theme-type ) or light ;
6063}
6164
6265/// 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
7073/// interpreted as a palette name).
7174/// @return {Color} The requested theme color.
7275@function get-theme-color ($theme , $args ... ) {
76+ $version : get-theme-version ($theme );
7377 $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 ;
7680 }
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 ;
7995 }
80- @error #{' Expected 2 or 3 arguments. Got:' } $args-count + 1;
8196}
8297
8398/// Gets a role color from a theme object.
@@ -136,72 +151,83 @@ $_typography-properties: (font, font-family, line-height, font-size, letter-spac
136151/// (font, font-family, font-size, font-weight, line-height, or letter-spacing).
137152/// @return {*} The value of the requested font property.
138153@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+ }
163182}
164183
165184/// Gets the density scale from a theme object.
166185/// @param {Map} $theme The theme
167186/// @return {Number} The density scale.
168187@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 );
173197 }
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 ;
176200 }
177- @return map .get ($theme , $_internals , density-scale );
178201}
179202
180203/// Checks whether the theme has information about given theming system.
181204/// @param {Map} $theme The theme
182205/// @param {String} $system The system to check
183206/// @param {Boolean} Whether the theme has information about the system.
184207@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
195218 map .get ($theme , $_internals , theme-type ) != null and
196219 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 ;
197230 }
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 ;
205231}
206232
207233/// Gets the set of tokens from the given theme, limited to those affected by the requested theming
0 commit comments