Technology News

A Practical Tip For Using Sass Default Parameters

Sass offers functions and mixins that accept parameters. You can use Sass default parameters, that is, parameters that have a value even if you don’t provide them when the function or mixin is called.

Let’s focus on mixins here. Here’s the syntax of a mixin:

@mixin foo($a, $b, $c) { // I can use $a, $b, and $c in here, but there is a risk they are null
} .el { @include foo(1, 2, 3); // if I tried to do `@include foo;` // ... which is valid syntax... // I'd get `Error: Missing argument $a.` from Sass
}

It’s safer and more useful to set up default parameters in this Sass mixin:

@mixin foo($a: 1, $b: 2, $c: 3) {
} .el { // Now this is fine @include foo; // AND I can send in params as well @include foo("three", "little", "pigs");
}

But what if I wanted to send in $b and $c, but leave $a as the Sass default parameter? The trick is that you send in named parameters:

@mixin foo($a: 1, $b: 2, $c: 3) {
} .el { // Only sending in the second two params, $a will be the default. @include foo($b: 2, $c: 3);
}

A real-life example using Sass default parameters

Here’s a quick-y mixin that outputs what you need for very basic styled scrollbars (Kitty has one as well):

@mixin scrollbars( $size: 10px, $foreground-color: #eee, $background-color: #333
) { // For Google Chrome &::-webkit-scrollbar { width: $size; height: $size; } &::-webkit-scrollbar-thumb { background: $foreground-color; } &::-webkit-scrollbar-track { background: $background-color; } // Standard version (Firefox only for now) scrollbar-color: $foreground-color $background-color;
}

Now I can call it like this:

.scrollable { @include scrollbars;
} .thick-but-otherwise-default-scrollable { // I can skip $b and $c because they are second and third @include scrollbars(30px);
} .custom-colors-scrollable { // I can skip the first param if all the others are named. @include scrollbars($foreground-color: orange, $background-color: black);
} .totally-custom-scrollable { @include scrollbars(20px, red, black);
}

I’m just noting this as I had to search around a bit to figure this out. I was trying stuff like sending empty strings or null as the first parameter in order to “skip” it, but that doesn’t work. Gotta do the named parameter approach.

The true cost of Amazon’s low prices
8 Helpful Accessibility Links for January 2022

Related Articles

7 Best Web Safe HTML & CSS Fonts

7 Best Web Safe HTML & CSS Fonts
In today’s world, having a website is not enough – you need to make it look good, and design it in a way that will allow your visitors to have…

Process For Online Registration For GST

process-for-online-registration-for-gst
Online-Registration-For-GST- The introduction of GST in India has been widely acclaimed as a landmark reform in the nation’s tax system. It was introduced as a step to eliminate the need for double…

Best Ads That Ensure Customers Come Back For More

best-ads-that-ensure-customers-come-back-for-more
Advertising isn’t just for getting extra attention. The ideal advertisement should encourage consumers to come back for more. Not only should it increase brand awareness, but it should also encourage…

The Possibilities of Syndication

the-possibilities-of-syndication
That’s the one word that isn’t an adjective in the acronym RSS. Really Simple Syndication. RSS isn’t just about RSS readers. Even though, gosh if I don’t love RSS readers.…