Variables

Asterix uses several kinds of variables. Some are scss variables used to provide data in one side and load your own values into the system on the other side. Others are css variables set to be used client-side.

Asterix's configuration is made of two parts.

$asterix-vars injects your own design system settings into Asterix ;

$asterix-conf configures the asterix's global settings. Check Configuration section for more details.

SCSS variables

Asterix uses a map variable called $asterix-vars wich contains all required and custom values.

It is used during sass compilation to write internal rules like responsive ones, but also to write css3 variables.

You can add, update or remove groups and properties.

Default settings

// /_configuration.scss - Default settings
$asterix-vars: (
  breakpoints: (
    xs: 480px,
    s: 768px,
    m: 1024px,
    l: 1200px,
  ),
  colors: (
    default: black,
  ),
  font-families: (
    default: 'sans-serif',
  ),
  gaps: (
    1: 0.5rem,
    2: 1rem,
    3: 1.5rem,
    4: 3rem,
  ),
  shapes: (
    default: 4px,
  ),
) !default;

Minimal configuration

In order to use builtins mixins, functions and layout system, you have to include those maps:

  • breakpoints: ( key: value, key: value...)

  • gaps: (key:value, key:value...)

Other maps are totally optional.

Retrieve $asterix-vars values

You can use the --var($group[, $property]) function to retrieve a specific value or an entire group.

$group : the $asterix-vars group name to retrieve.

$property: when set, return the specific property value.

If the group doesn't exist, return an empy map ().

If the property doesn't exist, return unset.

Update $asterix-vars values

The mixin --update-vars($updatedVars) will merge the map you provide with the current settings.

Adding new groups will not write any additional css directly. You'll have to create your own specifiers or retrieve values where needed.

CSS Variables

Asterix loves css3 variables, as it uses them in its layout engine.

Translate scss variables into css variables

Manually:

Or a bunch of maps at once with the --set-css-variables($variables) mixin:

Generated css3 variables names follow a --[groupName]-[valueName] pattern.

Because we need verbose names in sass and short ones in css, you have to manually set a css group name for each scss variables group. In the example above, the var(--colors) values will be called --clr-[valueName] in css.

You can expose variables in any scope.

About colors

When translating a sass color into a css variables, its value cannot normally be used into rgba() css rules. Here's a basic example of the issue:

Core css variables

Asterix is also using a few css variables for its layout engine.

While you should never have to deal with them, it is still useful to understand how Asterix works in its core.

Internal variables

Logic

Basically, declaring an attribute will set to some properties some css variables. Values provided to the attribute will set the css variables values.

Here is a minimal example :

With this approach, the core is writing a very few code in the loops used to render columns, gap and breakpoints, and a bit more in attribute global selector. This way, code can be extended at a low byte cost.

Finally, by using css variables, the asterix's code is non-invasive as it can be manually updated in any scope level, without struggling with inheritance.

Last updated