Search code examples
sassmixinscompass-sass

Sass: calling my own mixin within +background() results in "doesn't support keyword arguments"


I'm pretty new to Sass. This is my mixin:

= foo($param1, $param2: 0, $param3: 123)
  something: $param1
  otherthing: $param2
  yetanotherthing: $param3

When I call it like this:

div.foo
  +foo(99, $param3: 444)

...it produces:

div.foo {
  something: 99;
  otherthing: 0;
  yetanotherthing: 444;
}

When I call it like this:

div.foo
  +background(+foo(99, $param3: 444))

...it produces an ereror "Function foo doesn't support keyword arguments".

When I call it like this:

div.foo
  +background(foo(99))

...it produces:

div.foo {
  background: foo(99);
}

This seems weird to me. Can't I pass the result of my own mixin to another mixin? And why does something like +background-image(linear-gradient(bottom, #ababab, #dadada)) work?

Thanks a lot for help.


Solution

  • It looks like you're getting a not very useful error message. Functons do support keyword arguments. Problem is, you don't have a function. You have a mixin. There's a big difference between the two.

    @function foo($a: true, $b: true) {
        @return $b;
    }
    
    @debug foo($b: false);
    
    .foo {
        @if foo {
            color: red;
        } @else {
            color: green;
        }
    }
    

    Linear-gradient() is a function. When you invoke it, it returns a value that can be printed, echoed via @debug, or passed to other functions or mixins. There's some other magic involved here with the Compass background* mixins, but you'll have to dig into the source yourself.

    Mixins do not have a return value and cannot be passed around in any way. They can only be used to emit code.