Sass Theming: The Neverending Story

Building grid systems and theming engines is a neverending Sass story. There are probably as many Sass-powered grid systems as JavaScript frameworks nowadays, and pretty much everyone has their own way to deal with color themes in their stylesheets.

In today’s article, I’d like to tackle the latter issue and have a quick round-up of a couple of ways of building color schemes with Sass. But before going any further, what exactly are we talking about?

Let’s say we have a component, such as the classic media object. Now let’s say that we have different color schemes for this component, for instance a palette per category. For a category A, our object would have a red headline and a red-ish border, and for a category B, our object would have a more blue-ish color palette.

--ADVERTISEMENT--

Finally, say you don’t have 2 themes but a dozen as well as a lot of color variations within the component. You could concede that managing this by hand would be highly impractical, and this is definitely the kind of thing you want to automate with a tool. In our case, that tool is Sass.

Okay, now we are ready. Although categories are kind of boring so let’s bring in some unicorns and dragons. Any of our approaches will rely on a map of themes. Each theme is a sub-map containing keys mapped to actual colors.

Each of our themes consists of two colors: a primary one and a secondary one. We could have named them differently like alpha and beta, it does not matter. The point is only to be able to get “the main color from a theme” for instance.

How does the theming actually work?

There are different ways of course but usually, a theme is nothing but a class to which is bound some specific styles. This class can either be added to the body element to wrap the whole pages, or to specific components to themify a small module only, as we had considered just before.

All in all, in your stylesheet you will probably expect any of those two variations:

.theme-class .component {
/* Style for the component when child of `.theme-class` */
}
.component.theme-class {
/* Style for the component when has `.theme-class` */
}

The individual mixins approach

Let’s start with a simple one, and probably my favorite: the individual mixins approach. To put it simply, you have a couple of mixins named after the property they intend to styles. For instance, theme-color to themify the color or theme-background-color to themify the background.

You have to agree this is quite elegant. Moreover, I feel like it is obvious, even without the comments.

Code

Let’s have a look at how to build this. We are not going to repeat the code logic inside each of those individual mixins, so we need a private mixin to gather it all and then be used inside each subsequent mixin.

Pros

Thanks to property-named mixins, the API is both clean and clear, even to a non-experienced developer.

Cons

This approach involves several mixins instead of one which might or might not be considered as extra complexity, even if most of them are just clones.

Because the colors are directly fetched from the color map based on their key, it is not possible to manipulate them with color functions such as lighten or mix. It might be doable with an extended version of the mixin though.

The block mixin approach

The block mixin approach uses a single mixin instead of several one and relies heavily on the usage of the @content directive. Using it would look something like:

Code

The idea is simple: exposing the two colors as variables inside the themify mixin. The problem is that we cannot actually do this in a clean way. Variables defined in a mixin are not accessible to the content passed through @content as per the documentation:

The block of content passed to a mixin are evaluated in the scope where the block is defined, not in the scope of the mixin. This means that variables local to the mixin cannot be used within the passed style block and variables will resolve to the global value.

Because of this limitation, we have to hack our way around it. The work-around is actually not that complicated: before outputting @content, we define one global variable per color in the theme, and after outputting @content we unset those variables. This way, they are only accessible in the themify call.

Pros

Contrary to the individual mixins solution, the block mixin gives the ability to manipulate colors with functions since we have direct access to the colors stored in theme variables.

I feel like the API is still quick clear with this approach, especially since we use actual CSS declarations inside the mixin call, which might be easier to understand for some people.

Cons

The use of global variables in such a way is kind of hacky I must say. No big deal, but code quality is not really the plus side here.

The big ol’ theme mixin approach

This approach is actually something I have already written about here at SitePoint, in this article from last year. The idea is that you have a big ol’ mixin you update every time you need something to be themified.

This means that this mixin is the same for all components across the project. If you want to make this new component themified in some way, you need to open the file where the themify mixin lives and add a couple of extra rules in there.

Pros

Since the mixin content is manually maintained, it gives a high flexibility with selectors. You can pretty much do whatever you want in there.

For the same reason, it also gives the ability to manipulate colors with functions like darken.

Cons

Because everything themed is stored in this mixin’s content, it is not well suited for a modular approach. It is probably okay on small to medium projects with global stylesheets though.

It might be confusing to have component styles divided into several places, i.e. the module stylesheet and the theme mixin.

The class approach

The class approach is actually a DOM-driven one. The idea is that instead of applying specific theme styles from the stylesheet, you instead add theme classes to your markup such as border-color-primary. These classes, generated with Sass, do nothing on their own but apply some styles when used in combination with our eternal theme-$theme classes.

You can read more about this system in this article from Harry Roberts.

It might looks like an awful lot of generated CSS, but this is something you can re-use several times throughout your whole project, so it actually is not that bad.

Pros

This solution has the benefit of being a DOM-based approach which turns out to be very interesting when having to manipulate themes on the fly with JavaScript. Indeed, it’s only a matter of adding/removing theme classes to/from elements; very handy.

While the output of the themify mixin might look big, it actually is a quite DRY approach since every themed color, border-color, background-color and whatelse is applied through these classes.

Cons

In some cases, a DOM-based approach might not be the correct way to proceed for instance when the markup is not hand-crafted (CMS, user-generated content…).

Final thoughts

I am sure I forgot maybe a dozen of other ways to apply theme styles with Sass, but I feel like these 4 different versions already cover a good area of the topic. If I get my choice we either go for the individual mixins approach, or the DOM-drive way with classes all the way if we want to go very modular (which is always good).