Insights Code

C
o
n
t
r
o
l
l
i
n
g
c
o
l
o
r
o
r
g
a
n
i
z
a
t
i
o
n
i
n
y
o
u
r
C
S
S

Joining
the
forces
of
CSS
variables
and
SCSS
to
maintain
strict
color
structure
and
organization.

Colors are one of the hardest parts of CSS for me. I find that I back myself into a corner often with either having too many colors dispersed between many different files, or the system I've designed is too inflexible to handle all of the color options I want.

The importance of simplicity

The best design system is usually the one that is simple. As designers & developers, it tends to be in our nature to make things that are detailed, with lots of possibilities, while we overlook the peace simplicity brings.

Merging CSS variables & SCSS

Creating a map

$colors: (
    primary: #F1F2F3,
    secondary: blue,
    darkmode: (
        grey-1: #222,
        grey-2: #333,
        grey-3: #444
    )
);
scss

Staging the colors map

html {
    @each $name, $color in $colors {
        @if type-of($color) == "map" {
            @each $subname, $subcolor in $color {  
                --color-#{$name}-#{$subname}: #{$subcolor};
            }
        } @else {
            --color-#{$name}: #{$color};
        }
    }
}
scss

Utility functions to inject our colors

@function c($color-name) {
    @return var(--color-#{$color-name});
}
scss

Using the variables throughout our CSS

body {
    background: c('primary');

    // For nested maps, you will have to call the nested variable, followed by the name
    color: c('darkmode-grey-1');
}
scss