Skip to content

Commit f0272cf

Browse files
crisbetoandrewseguin
authored andcommitted
fix(material/core): throw error if hue does not exist (#23612)
Currently if an invalid hue is passed into `define-palette`, we silently emit nothing which can lead to confusion. These changes add an error to make it easier to debug. Related to #23605. (cherry picked from commit 65bc9fe)
1 parent 0ab3dce commit f0272cf

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/material/core/theming/_theming.scss

+15-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ $_emitted-color: () !default;
2020
$_emitted-typography: () !default;
2121
$_emitted-density: () !default;
2222

23+
/// Extracts a color from a palette or throws an error if it doesn't exist.
24+
/// @param {Map} $palette The palette from which to extract a color.
25+
/// @param {String | Number} $hue The hue for which to get the color.
26+
@function _get-color-from-palette($palette, $hue) {
27+
@if map.has-key($palette, $hue) {
28+
@return map.get($palette, $hue);
29+
}
30+
31+
@error 'Hue "' + $hue + '" does not exist in palette. Available hues are: ' + map.keys($palette);
32+
}
33+
2334
/// For a given hue in a palette, return the contrast color from the map of contrast palettes.
2435
/// @param {Map} $palette The palette from which to extract a color.
2536
/// @param {String | Number} $hue The hue for which to get a contrast color.
@@ -40,10 +51,10 @@ $_emitted-density: () !default;
4051
@function define-palette($base-palette, $default: 500, $lighter: 100, $darker: 700,
4152
$text: $default) {
4253
$result: map.merge($base-palette, (
43-
default: map.get($base-palette, $default),
44-
lighter: map.get($base-palette, $lighter),
45-
darker: map.get($base-palette, $darker),
46-
text: map.get($base-palette, $text),
54+
default: _get-color-from-palette($base-palette, $default),
55+
lighter: _get-color-from-palette($base-palette, $lighter),
56+
darker: _get-color-from-palette($base-palette, $darker),
57+
text: _get-color-from-palette($base-palette, $text),
4758

4859
default-contrast: get-contrast-color-from-palette($base-palette, $default),
4960
lighter-contrast: get-contrast-color-from-palette($base-palette, $lighter),

0 commit comments

Comments
 (0)