Skip to content

Commit

Permalink
feat(material/theming): Add APIs to check what information theme has
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalerba committed Aug 14, 2023
1 parent e608f5f commit 9906aa3
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 100 deletions.
2 changes: 1 addition & 1 deletion src/material/_index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,4 @@ list-density, list-base;
// New theming APIs, currently in development:
@forward './core/theming/inspection' as private-* show private-get-theme-version,
private-get-theme-type, private-get-theme-color, private-get-theme-typography,
private-get-theme-density;
private-get-theme-density, private-theme-has;
42 changes: 42 additions & 0 deletions src/material/core/theming/_inspection.scss
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ $_typography-properties: (font, font-family, line-height, font-size, letter-spac
// TODO(mmalerba): implement for old style theme objects.
@error #{'get-theme-type does not support legacy theme objects.'};
}
@if not theme-has($theme, color) {
@error 'Color information is not available on this theme.';
}
@return map.get($theme, $_internals, theme-type) or light;
}

Expand Down Expand Up @@ -86,6 +89,9 @@ $_typography-properties: (font, font-family, line-height, font-size, letter-spac
// TODO(mmalerba): implement for old style theme objects.
@error #{'get-theme-color does not support legacy theme objects.'};
}
@if not theme-has($theme, color) {
@error 'Color information is not available on this theme.';
}
$color-roles: map.get($theme, $_internals, color-tokens, (mdc, theme));
$result: map.get($color-roles, $color-role-name);
@if not $result {
Expand All @@ -105,6 +111,9 @@ $_typography-properties: (font, font-family, line-height, font-size, letter-spac
// TODO(mmalerba): implement for old style theme objects.
@error #{'get-theme-color does not support legacy theme objects.'};
}
@if not theme-has($theme, color) {
@error 'Color information is not available on this theme.';
}
$palettes: map.get($theme, $_internals, palettes);
$palette: map.get($palettes, $palette-name);
@if not $palette {
Expand All @@ -131,6 +140,9 @@ $_typography-properties: (font, font-family, line-height, font-size, letter-spac
// TODO(mmalerba): implement for old style theme objects.
@error #{'get-theme-typography does not support legacy theme objects.'};
}
@if not theme-has($theme, typography) {
@error 'Typography information is not available on this theme.';
}
@if not list.index($_m3-typescales, $typescale) {
@error #{'Valid typescales are: #{$_m3-typescales}. Got:'} $typescale;
}
Expand Down Expand Up @@ -158,9 +170,39 @@ $_typography-properties: (font, font-family, line-height, font-size, letter-spac
// TODO(mmalerba): implement for old style theme objects.
@error #{'get-theme-density does not support legacy theme objects.'};
}
@if not theme-has($theme, density) {
@error 'Density information is not available on this theme.';
}
@return map.get($theme, $_internals, density-scale);
}

/// Checks whether the theme has information about given theming system.
/// @param {Map} $theme The theme
/// @param {String} $system The system to check
/// @param {Boolean} Whether the theme has information about the system.
@function theme-has($theme, $system) {
$err: _validate-theme-object($theme);
@if $err {
// TODO(mmalerba): implement for old style theme objects.
@error #{'get-theme-density does not support legacy theme objects.'};
}
@if $system == base {
@return map.get($theme, $_internals, base-tokens) != null;
}
@if $system == color {
@return map.get($theme, $_internals, color-tokens) != null and
map.get($theme, $_internals, theme-type) != null and
map.get($theme, $_internals, palettes) != null;
}
@if $system == typography {
@return map.get($theme, $_internals, typography-tokens) != null;
}
@if $system == density {
@return map.get($theme, $_internals, density-scale) != null
}
@error 'Valid systems are: base, color, typography, density. Got:' $system;
}

/// Gets the set of tokens from the given theme, limited to those affected by the requested theming
/// systems.
/// @param {Map} $theme The theme to get tokens from.
Expand Down
Loading

0 comments on commit 9906aa3

Please sign in to comment.