2

Consider the following two mixins:

@mixin padding($v: 8px)
  padding: $v - 3 $v $v - 3 $v

@mixin button($padding: "")
  @include padding($padding)
  @include border-radius
  border: 1px solid black

I can call @include padding without an argument, and the default 8px gets used, no problem. But how can I call the button mixin and still have the default value get used in the padding mixin? If I simply call @include button without an argument, the button mixin still seems to pass some kind of argument to padding (which padding inevitably interprets as 0, thus not adding any padding). Based on Making a Sass mixin with optional arguments, I tried modifying the button mixin to have

@include padding(unquote($padding))

but that had no effect -- padding still seemed to interpret the argument as being 0, rather than there being no argument and thus using its default value.

Community
  • 1
  • 1
maxedison
  • 17,243
  • 14
  • 67
  • 114

1 Answers1

4

This problem is typically solved by setting the default arguments for both mixins to be the same value. You may want to use a global variable for this purpose:

$default-padding: 8px

@mixin padding($v: $default-padding)
  padding: $v - 3 $v $v - 3 $v

@mixin button($padding: $default-padding)
  @include padding($padding)
  @include border-radius
  border: 1px solid black
cimmanon
  • 67,211
  • 17
  • 165
  • 171