Skip to content

Commit 9906aa3

Browse files
committed
feat(material/theming): Add APIs to check what information theme has
1 parent e608f5f commit 9906aa3

File tree

3 files changed

+151
-100
lines changed

3 files changed

+151
-100
lines changed

src/material/_index.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,4 @@ list-density, list-base;
146146
// New theming APIs, currently in development:
147147
@forward './core/theming/inspection' as private-* show private-get-theme-version,
148148
private-get-theme-type, private-get-theme-color, private-get-theme-typography,
149-
private-get-theme-density;
149+
private-get-theme-density, private-theme-has;

src/material/core/theming/_inspection.scss

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ $_typography-properties: (font, font-family, line-height, font-size, letter-spac
5353
// TODO(mmalerba): implement for old style theme objects.
5454
@error #{'get-theme-type does not support legacy theme objects.'};
5555
}
56+
@if not theme-has($theme, color) {
57+
@error 'Color information is not available on this theme.';
58+
}
5659
@return map.get($theme, $_internals, theme-type) or light;
5760
}
5861

@@ -86,6 +89,9 @@ $_typography-properties: (font, font-family, line-height, font-size, letter-spac
8689
// TODO(mmalerba): implement for old style theme objects.
8790
@error #{'get-theme-color does not support legacy theme objects.'};
8891
}
92+
@if not theme-has($theme, color) {
93+
@error 'Color information is not available on this theme.';
94+
}
8995
$color-roles: map.get($theme, $_internals, color-tokens, (mdc, theme));
9096
$result: map.get($color-roles, $color-role-name);
9197
@if not $result {
@@ -105,6 +111,9 @@ $_typography-properties: (font, font-family, line-height, font-size, letter-spac
105111
// TODO(mmalerba): implement for old style theme objects.
106112
@error #{'get-theme-color does not support legacy theme objects.'};
107113
}
114+
@if not theme-has($theme, color) {
115+
@error 'Color information is not available on this theme.';
116+
}
108117
$palettes: map.get($theme, $_internals, palettes);
109118
$palette: map.get($palettes, $palette-name);
110119
@if not $palette {
@@ -131,6 +140,9 @@ $_typography-properties: (font, font-family, line-height, font-size, letter-spac
131140
// TODO(mmalerba): implement for old style theme objects.
132141
@error #{'get-theme-typography does not support legacy theme objects.'};
133142
}
143+
@if not theme-has($theme, typography) {
144+
@error 'Typography information is not available on this theme.';
145+
}
134146
@if not list.index($_m3-typescales, $typescale) {
135147
@error #{'Valid typescales are: #{$_m3-typescales}. Got:'} $typescale;
136148
}
@@ -158,9 +170,39 @@ $_typography-properties: (font, font-family, line-height, font-size, letter-spac
158170
// TODO(mmalerba): implement for old style theme objects.
159171
@error #{'get-theme-density does not support legacy theme objects.'};
160172
}
173+
@if not theme-has($theme, density) {
174+
@error 'Density information is not available on this theme.';
175+
}
161176
@return map.get($theme, $_internals, density-scale);
162177
}
163178

179+
/// Checks whether the theme has information about given theming system.
180+
/// @param {Map} $theme The theme
181+
/// @param {String} $system The system to check
182+
/// @param {Boolean} Whether the theme has information about the system.
183+
@function theme-has($theme, $system) {
184+
$err: _validate-theme-object($theme);
185+
@if $err {
186+
// TODO(mmalerba): implement for old style theme objects.
187+
@error #{'get-theme-density does not support legacy theme objects.'};
188+
}
189+
@if $system == base {
190+
@return map.get($theme, $_internals, base-tokens) != null;
191+
}
192+
@if $system == color {
193+
@return map.get($theme, $_internals, color-tokens) != null and
194+
map.get($theme, $_internals, theme-type) != null and
195+
map.get($theme, $_internals, palettes) != null;
196+
}
197+
@if $system == typography {
198+
@return map.get($theme, $_internals, typography-tokens) != null;
199+
}
200+
@if $system == density {
201+
@return map.get($theme, $_internals, density-scale) != null
202+
}
203+
@error 'Valid systems are: base, color, typography, density. Got:' $system;
204+
}
205+
164206
/// Gets the set of tokens from the given theme, limited to those affected by the requested theming
165207
/// systems.
166208
/// @param {Map} $theme The theme to get tokens from.

0 commit comments

Comments
 (0)