Accoutrement 2.1.1

Colors » CSS Variables

$color-var-prefix (string)

scss
$color-var-prefix: 'color-' !default;

Since CSS variables are often defined in a global space, it can be useful to prefix different variable types. Accoutrement maps help solve that problem in Sass, so we wanted to carry that over into our CSS variable support. Set a prefix for all color-tokens, and we’ll apply it when setting or calling css variables based on your color maps. Set to null or '' (empty string) for no prefix.

Since 2.0.0:

  • NEW: Initial release

Example

scss
$colors: ('my-color': pink);
$color-var-prefix: 'prefix_';
:root { @include color--('my-color') }
css compiled
:root {
  --prefix_my-color: pink;
}

Used By

@function var-contrast()

@mixin colors--()

@mixin color--()

@function var-color()

@mixin colors--()

Convert any color-map into CSS variables, using the global $color-var-prefix.

  • Color names that start with _ or - are considered “private” and will not be output as variables
  • Colors that contain a simple alias with no adjustments will be output with a var() value, keeping the alias relationship intact

Since 2.0.0:

  • NEW: Initial release

Parameters & Output

$source: $colors (map)

Optionally use a custom map of colors to set as css variables

{CSS output} (code block)

Custom properties for all public colors in the map

Example

scss
$colors: (
  '_brand': hsl(120, 50%, 50%),
  'text': '#_brand' ('shade': 50%),
  'border': '#text',
);
:root {
  @include colors--;
}
css compiled
:root {
  --color-text: #206020;
  --color-border: var(--color-text, #206020);
}

Requires

@mixin tokens--()

$_a_COLORS (map) [private]

@mixin color--()

Set a single custom property based on a map-color, with optional alias and fallback

Since 2.0.0:

  • NEW: Initial release

Parameters

$color: (*)

Color name available in the $source map, or alias to use in naming the CSS variable

$value: null (string | null)

Optional value for the variable, if different from the given $color

$fallback: true (*)

The optional fallback value for a var() function:

  • true will generate a fallback based on the token value
  • A token name will fallback to the value of that css-variable and then to the token’s calculated value
  • Any other fallback will be passed through unchanged

$source: $colors (map)

Optional map of colors to use as a palette source

Example

scss
$colors: (
  '_brand': hsl(120, 50%, 50%),
  'text': '#_brand' ('shade': 50%),
  'border': '#text',
);
.example {
  @include color--('border');
  @include color--('outline', 'border', red);
}
css compiled
.example {
  --color-border: var(--color-text, #206020);
  --color-outline: var(--color-border, red);
}

Requires

@mixin token--()

$_a_COLORS (map) [private]

@function var-color()

Access the CSS variable associated with a given token, along with a fallback value based on the token itself

Since 2.0.0:

  • NEW: Initial release

Parameters & Return

$color: (*)

Color name available in the $source map

$fallback: true (*)

The optional fallback value for a var() function:

  • true will generate a fallback based on the token value
  • A token name will fallback to the value of that css-variable and then to the token’s calculated value
  • Any other fallback will be passed through unchanged

$source: (map)

Optional Accoutrement map of colors to use as source

@return (string)

CSS variable call, in the format: var(--<token>, <fallback>)

Example

scss
$colors: (
  '_brand': hsl(120, 50%, 50%),
  'text': '#_brand' ('shade': 50%),
  'border': '#text',
);
.example {
  border-color: var-color('border');
  color: var-color('text', black);
}
css compiled
.example {
  border-color: var(--color-border, #206020);
  color: var(--color-text, black);
}

Requires

@function var-token()

$_a_COLORS (map) [private]

Used By