Skip to content

Commit e055771

Browse files
committed
add docs for new sass functions
1 parent 821a9ee commit e055771

File tree

2 files changed

+48
-22
lines changed

2 files changed

+48
-22
lines changed

src/material/core/theming/_m2-inspection.scss

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
@use '../typography/typography-utils';
66
@use '../typography/typography';
77

8+
/// Key used to access the Angular Material theme internals.
89
$_internals: _mat-theming-internals-do-not-access;
910

11+
/// Keys that indicate a config object is a color config.
1012
$_color-keys: (
1113
primary,
1214
accent,
@@ -17,6 +19,7 @@ $_color-keys: (
1719
color, /* included for themes that incorrectly nest the color config: (color: (color: (....))) */
1820
);
1921

22+
/// Keys that indicate a config object is a typography config.
2023
$_typography-keys: (
2124
headline-1,
2225
headline-2,
@@ -34,9 +37,23 @@ $_typography-keys: (
3437
overline,
3538
);
3639

40+
/// The CSS typography properties supported by the inspection API.
3741
$_typography-properties: (font, font-family, line-height, font-size, letter-spacing, font-weight);
3842

39-
/// Checks whether the given theme contains error objects.
43+
/// Gets the m2-config from the theme.
44+
@function _get-m2-config($theme) {
45+
// It is possible for a user to pass a "density theme" that is just a number.
46+
@if meta.type-of($theme) != 'map' {
47+
@return $theme;
48+
}
49+
$internal: map.get($theme, $_internals, m2-config);
50+
$theme: map.remove($theme, $_internals);
51+
@return if(_is-error-theme($theme), $internal, $theme);
52+
}
53+
54+
/// Checks whether the given theme contains error values.
55+
/// @param {*} $theme The theme to check.
56+
/// @return {Boolean} true if the theme contains error values, else false.
4057
@function _is-error-theme($theme) {
4158
@if meta.type-of($theme) != 'map' {
4259
@return false;
@@ -47,15 +64,24 @@ $_typography-properties: (font, font-family, line-height, font-size, letter-spac
4764
@return _is-error-theme(list.nth(map.values($theme), 1));
4865
}
4966

50-
/// Gets the m2-config from the theme.
51-
@function _get-m2-config($theme) {
52-
// It is possible for a user to pass a "density theme" that is just a number.
53-
@if meta.type-of($theme) != 'map' {
54-
@return $theme;
67+
/// Checks whether the given theme object has any of the given keys.
68+
/// @param {Map} $theme The theme to check
69+
/// @param {List} $keys The keys to check for
70+
/// @return {Boolean} Whether the theme has any of the given keys.
71+
@function _has-any-key($theme, $keys) {
72+
@each $key in $keys {
73+
@if map.has-key($theme, $key) {
74+
@return true;
75+
}
5576
}
56-
$internal: map.get($theme, $_internals, m2-config);
57-
$theme: map.remove($theme, $_internals);
58-
@return if(_is-error-theme($theme), $internal, $theme);
77+
@return false;
78+
}
79+
80+
/// Checks whether the given theme is a density scale value.
81+
/// @param {*} $theme The theme to check.
82+
/// @return {Boolean} true if the theme is a density scale value, else false.
83+
@function _is-density-value($theme) {
84+
@return $theme == minimum or $theme == maximum or meta.type-of($theme) == 'number';
5985
}
6086

6187
/// Gets the type of theme represented by a theme object (light or dark).
@@ -177,16 +203,3 @@ $_typography-properties: (font, font-family, line-height, font-size, letter-spac
177203
}
178204
@error 'Valid systems are: base, color, typography, density. Got:' $system;
179205
}
180-
181-
@function _has-any-key($theme, $keys) {
182-
@each $key in $keys {
183-
@if map.has-key($theme, $key) {
184-
@return true;
185-
}
186-
}
187-
@return false;
188-
}
189-
190-
@function _is-density-value($theme) {
191-
@return $theme == minimum or $theme == maximum or meta.type-of($theme) == 'number';
192-
}

src/material/core/theming/_theming.scss

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,10 @@ $_internals: _mat-theming-internals-do-not-access;
601601
@return $density-scale;
602602
}
603603

604+
/// Copies the given theme object and nests it within itself under a secret key and replaces the
605+
/// original map keys with error values. This allows the inspection API which is aware of the secret
606+
/// key to access the real values, but attempts to directly access the map will result in errors.
607+
/// @param {Map} $theme The theme map.
604608
@function _internalize-theme($theme) {
605609
@if map.has-key($theme, $_internals) {
606610
@return $theme;
@@ -619,6 +623,15 @@ $_internals: _mat-theming-internals-do-not-access;
619623
@return map.merge($error-theme, $internalized-theme);
620624
}
621625

626+
/// Replaces concrete CSS values with errors in a theme object.
627+
/// Errors are represented as a map `(ERROR: <message>)`. Because maps are not valid CSS values,
628+
/// the Sass will not compile if the user tries to use any of the error theme values in their CSS.
629+
/// Users will see a message about `(ERROR: <message>)` not being a valid CSS value. Using the
630+
/// message, that winds up getting shown, we can help explain to users why they're getting the
631+
/// error.
632+
/// @param {*} $value The theme value to replace with errors.
633+
/// @param {String} $message The error message to sow users.
634+
/// @return {Map} A version of $value where concrete CSS values have been replaced with errors
622635
@function _replace-values-with-errors($value, $message) {
623636
$value-type: meta.type-of($value);
624637
@if $value-type == 'map' {

0 commit comments

Comments
 (0)