Skip to content

Commit

Permalink
refactor(material/theming): add inspection compatibility flag
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalerba committed Aug 24, 2023
1 parent 685b585 commit eeb0cf2
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 158 deletions.
2 changes: 1 addition & 1 deletion src/material/_index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@forward './core/theming/theming' show define-light-theme, define-dark-theme,
define-palette, get-contrast-color-from-palette, get-color-from-palette,
get-color-config, get-typography-config, get-density-config,
$theme-ignore-duplication-warnings;
$theme-ignore-duplication-warnings, $theme-legacy-inspection-api-compatibility;
@forward './core/theming/theming' as private-* show private-clamp-density;
@forward './core/theming/palette' show $red-palette, $pink-palette, $indigo-palette,
$purple-palette, $deep-purple-palette, $blue-palette, $light-blue-palette, $cyan-palette,
Expand Down
15 changes: 15 additions & 0 deletions src/material/core/theming/_m2-inspection.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@

$_typography-properties: (font, font-family, line-height, font-size, letter-spacing, font-weight);

/// Gets the m2-config from the theme.
@function _get-m2-config($theme) {
$internal: map.get($theme, _mat-theming-internals-do-not-access, m2-config);
@return $internal or $theme;
}

/// Gets the type of theme represented by a theme object (light or dark).
/// @param {Map} $theme The theme
/// @return {String} The type of theme (either `light` or `dark`).
@function get-theme-type($theme) {
$theme: _get-m2-config($theme);
@if not theme-has($theme, color) {
@error 'Color information is not available on this theme.';
}
Expand All @@ -26,6 +33,7 @@ $_typography-properties: (font, font-family, line-height, font-size, letter-spac
/// interpreted as a palette name).
/// @return {Color} The requested theme color.
@function get-theme-color($theme, $palette-name, $args...) {
$theme: _get-m2-config($theme);
@if not theme-has($theme, color) {
@error 'Color information is not available on this theme.';
}
Expand All @@ -44,6 +52,7 @@ $_typography-properties: (font, font-family, line-height, font-size, letter-spac
/// (font, font-family, font-size, font-weight, line-height, or letter-spacing).
/// @return {*} The value of the requested font property.
@function get-theme-typography($theme, $typescale, $property) {
$theme: _get-m2-config($theme);
@if not theme-has($theme, typography) {
@error 'Typography information is not available on this theme.';
}
Expand Down Expand Up @@ -79,14 +88,20 @@ $_typography-properties: (font, font-family, line-height, font-size, letter-spac
/// @param {Map} $theme The theme
/// @return {Number} The density scale.
@function get-theme-density($theme) {
$theme: _get-m2-config($theme);
@if not theme-has($theme, density) {
@error 'Density information is not available on this theme.';
}
$scale: theming.get-density-config($theme);
@return theming.clamp-density($scale, -5);
}

/// 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) {
$theme: _get-m2-config($theme);
$theme: theming.private-legacy-get-theme($theme);
@if $system == base {
@return true;
Expand Down
11 changes: 10 additions & 1 deletion src/material/core/theming/_theming.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
@use 'palette';
@use '../density/private/compatibility';

$theme-legacy-inspection-api-compatibility: true !default;

// Whether duplication warnings should be disabled. Warnings enabled by default.
$theme-ignore-duplication-warnings: false !default;

Expand Down Expand Up @@ -544,7 +546,14 @@ $_internals: _mat-theming-internals-do-not-access;
@return $theme;
}
$color: map.get($theme, color);
@return map.merge($theme, $color);
$m2-config: map.merge($theme, $color);
$theme: (
_mat-theming-internals-do-not-access: (
theme-version: 0,
m2-config: $m2-config,
)
);
@return map.merge(if($theme-legacy-inspection-api-compatibility, $m2-config, ()), $theme);
}

// Gets the theme from the given value that is either already a theme, or a color configuration.
Expand Down
Loading

0 comments on commit eeb0cf2

Please sign in to comment.