Skip to content

Commit

Permalink
fix(material/core): throw error if hue does not exist (#23612)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
crisbeto authored and andrewseguin committed Jan 13, 2022
1 parent 0ab3dce commit f0272cf
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/material/core/theming/_theming.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ $_emitted-color: () !default;
$_emitted-typography: () !default;
$_emitted-density: () !default;

/// Extracts a color from a palette or throws an error if it doesn't exist.
/// @param {Map} $palette The palette from which to extract a color.
/// @param {String | Number} $hue The hue for which to get the color.
@function _get-color-from-palette($palette, $hue) {
@if map.has-key($palette, $hue) {
@return map.get($palette, $hue);
}

@error 'Hue "' + $hue + '" does not exist in palette. Available hues are: ' + map.keys($palette);
}

/// For a given hue in a palette, return the contrast color from the map of contrast palettes.
/// @param {Map} $palette The palette from which to extract a color.
/// @param {String | Number} $hue The hue for which to get a contrast color.
Expand All @@ -40,10 +51,10 @@ $_emitted-density: () !default;
@function define-palette($base-palette, $default: 500, $lighter: 100, $darker: 700,
$text: $default) {
$result: map.merge($base-palette, (
default: map.get($base-palette, $default),
lighter: map.get($base-palette, $lighter),
darker: map.get($base-palette, $darker),
text: map.get($base-palette, $text),
default: _get-color-from-palette($base-palette, $default),
lighter: _get-color-from-palette($base-palette, $lighter),
darker: _get-color-from-palette($base-palette, $darker),
text: _get-color-from-palette($base-palette, $text),

default-contrast: get-contrast-color-from-palette($base-palette, $default),
lighter-contrast: get-contrast-color-from-palette($base-palette, $lighter),
Expand Down

0 comments on commit f0272cf

Please sign in to comment.