Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beta sass modules - fix module loop in function color() #4484

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
94 changes: 49 additions & 45 deletions src/stylesheets/core/functions/utilities/color.scss
Expand Up @@ -5,24 +5,37 @@ color()
Derive a color from a color shortcode
----------------------------------------
*/

@use "sass:list";
@use "sass:map";
@use "sass:meta";
@use "sass:string";
@use "../../settings";
@use "../../functions/general";
@use "../../settings" as *;
@use "../../tokens/color/shortcodes-color-system" as *;
@use "../../tokens/color/shortcodes-color-project" as *;
@use "../../tokens/color/assignments-theme-color" as *;

@function color($value, $flags...) {
// Create lists of single items
$value: general.unpack($value);

// Non-token colors may be passed with specific flags
@if meta.type-of($value) == color {
// override or set-theme will allow any color
@if index($flags, override) or index($flags, set-theme) {
// If theme color is found, prepare that value to be parsed by $system-color-shortcodes
@if map.has-key($assignments-theme-color, $value) {
$value: map.get($assignments-theme-color, $value);
}

// If theme or system color is found, return that value
@if map.has-key($system-color-shortcodes, $value) {
$quote: general.smart-quote($value);
$value: map.get($system-color-shortcodes, $value);
@return $value;
}

// If not a theme or system color, check if there is an acceptable value that can be passed into the system via flags
// Acceptable values: False, non-token css color values
@if meta.type-of($value) == color or $value == false {
@if list.index($flags, 'override') or list.index($flags, 'set-theme') {
// override + no-warn will skip warnings
@if index($flags, no-warn) {
@if list.index($flags, no-warn) {
@return $value;
}

Expand All @@ -34,46 +47,37 @@ Derive a color from a color shortcode
}
}

// False values may be passed through when setting theme colors
@if $value == false {
@if index($flags, set-theme) {
@return $value;
}
}

// Now, any value should be evaluated as a token

$value: general.smart-quote($value);

@if map.has-key($system-color-shortcodes, $value) {
$our-color: map.get($system-color-shortcodes, $value);
@if $our-color == false {
@error '`#{$value}` is a color that does not exist '
+ 'or is set to false.';
}
@return $our-color;
}

// If we're using the theme flag, $project-color-shortcodes has not yet been set
@if not index($flags, set-theme) {
@if map.has-key($project-color-shortcodes, $value) {
$our-color: (map.get($project-color-shortcodes, $value));
@if $our-color == false {
@error '`#{$value}` is a color that does not exist '
+ 'or is set to false.';
}
@return $our-color;
}
}

// If no acceptable value found, return an error
@return general.error-not-token($value, "color");
}

// @debug color("orange-80v");
@debug color('orange-80v');
// @return #352313;
// @debug color("primary-dark");
@debug color('primary-dark');
// @return #1a4480;
// @debug color("primary-lightest");
@debug color($theme-color-accent-warm-dark);
// @return #c05600;
@debug color('info');
// @return #00bde3
@debug color(orange, set-theme, no-warn);
// @return orange
@debug color(#ff0, set-theme, no-warn);
// @return #ff0
@debug color(rgba(0,0,0,1), set-theme, no-warn);
// @return black
@debug color(green, set-theme);
// @return green with Warning: Override: `green` is not a USWDS color token.
@debug color(green, override);
// @return green with Warning: Override: `green` is not a USWDS color token.
@debug color('primary-lightest', set-theme);
// @return false

// these should result in errors
// @debug color(green);
// error: '"green" is not a valid USWDS color token. See designsystem.digital.gov/design-tokens/color for more information.'
//@debug color(#f00);
// @return error: '"#f00" is not a valid USWDS color token. See designsystem.digital.gov/design-tokens/color for more information.'
// @debug color(hamburgers);
// error: '"hamburgers" is not a valid USWDS color token. See designsystem.digital.gov/design-tokens/color for more information.'
@debug color('primary-lightest');
// @return error: set to false;
// @debug color(#f00);
// @return error: not a valid token;
Expand Up @@ -62,4 +62,6 @@ $assignments-theme-color: (
"disabled-light": settings.$theme-color-disabled-light,
"disabled": settings.$theme-color-disabled,
"disabled-dark": settings.$theme-color-disabled-dark,
"emergency": settings.$theme-color-emergency,
"emergency-dark": settings.$theme-color-emergency-dark,
);
Expand Up @@ -38,4 +38,4 @@ $tokens-color-state: (
color(settings.$theme-color-emergency-dark, set-theme, no-warn),
);

@debug $tokens-color-state;
// @debug $tokens-color-state;