question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature:

It would be nice to be able to use the !default modifier with mixins, much like with variable assignment… if a mixin of that name was already defined, the declaration would not replace the previous one.

For example:

@mixin foo()
{
       display: block;
}

@mixin foo() !default 
{
       display: inline;
}

.bar { @include foo; }

… would result in .bar { display: block; }

The main place I can think of that would be a great use-case is frameworks like Foundation. If all of Foundation’s mixins were defined with !default, then the application-specific settings.scss file could override not just variables, but mixins as well, making it a lot easier to customize the output of such frameworks. As it stands now, you can tweak the variables, but mixins are either “accept them as they are or don’t use them”.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
KittyGiraudelcommented, Mar 28, 2014

The only realistic option that is currently available is to check if the mixin exists before defining it:

As far as I know, it’s not even an option since mixins and functions cannot be defined in control directives.

Mixins may not be defined within control directives or other mixins. Functions may not be defined within control directives or other mixins.

Also, that would be not and not !.

Anyway, it would be cool to have the !default flag available for mixins and functions as well. 😃

1reaction
cimmanoncommented, Mar 24, 2014

I’ve been wanting a feature like this myself. I know there’s been some discussion about namespacing, but I don’t think it would solve this problem. If I like a particular library’s boilerplate except for minor things here and there that are controlled by mixins, being able to have defaultable mixins seems like the most straightforward solution.

@mixin base-block-styles {
    margin: 0;
    padding: 0;
}

p, div, ul, ol, etc {
    @include base-block-styles;
}

What happens if I want to use this mixin instead?

@mixin base-block-styles {
    margin: 1em 0;
}

If the file I was importing didn’t include a base-block-styles mixin (maybe it is defined elsewhere in the library), then the user would be forced to create the mixin themselves or do an extra import. I don’t feel this is good for the user.

The only realistic option that is currently available is to check if the mixin exists before defining it:

@if !mixin-exists(base-block-styles) {
    @mixin base-block-styles {
        margin: 0;
        padding: 0;
    }
}

This seems a lot less elegant than simply making use of a !default flag, which users are already familiar with.

Read more comments on GitHub >

github_iconTop Results From Across the Web

mixin and @include - Sass
Normally, every argument a mixin declares must be passed when that mixin is included. However, you can make an argument optional by defining...
Read more >
Default values for mixins | Sass in the Real World: book 1 of 4
Set default values for mixins. When using mixins that require arguments, it is a best practice to always specify a default value for...
Read more >
Simplifying SASS Mixins with Default Values | by Seth Poulin
SASS mixins are blocks of code we can bundle into packages that get unpacked when we call them with the @include function.
Read more >
A Practical Tip For Using Sass Default Parameters - CSS-Tricks
You can use Sass default parameters, that is, parameters that have a value even if you don't provide them when the function or...
Read more >
Making a Sass mixin with optional arguments - Stack Overflow
Super simple way. Just add a default value of none to $inset - so @ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found