iLevi
iLevi

Reputation: 936

Nested mixins or functions in SASS

Some body know how can i use nested mixins or functions in SASS? I have something like this:

@mixin A(){
    do something....
}

@mixin B($argu){
    @include A();
}

Upvotes: 29

Views: 40653

Answers (3)

Frederik Krautwald
Frederik Krautwald

Reputation: 1860

As mentioned in the other answers, you can include mixins in other mixins. In addition, you can scope your mixins.

Example

.menu {
  user-select: none;

  .theme-dark & {
    color: #fff;
    background-color: #333;

    // Scoped mixin
    // Can only be seen in current block and descendants,
    // i.e., referencing it from outside current block
    // will result in an error.
    @mixin __item() {
      height: 48px;
    }

    &__item {
      @include __item();

      &_type_inverted {
        @include __item();

        color: #333;
        background-color: #fff;
      }
    }
  }
}

Will output:

.menu {
  user-select: none;
}

.theme-dark .menu {
  color: #fff;
  background-color: #333;
}

.theme-dark .menu__item {
  height: 48px;
}

.theme-dark .menu__item_type_inverted {
  height: 48px;
  color: #333;
  background-color: #fff;
}

Scoping mixins means that you can have multiple mixins named the same in different scopes without conflicts arising.

Upvotes: 4

Ignatius Andrew
Ignatius Andrew

Reputation: 8258

You can multi nest mixins, you can also use place holders inside mixins..

@mixin a {
  color: red;
}
@mixin b {
  @include a();
  padding: white;
  font-size: 10px;
}
@mixin c{
  @include b;
  padding:5;
}
div {
  @include c();
}

which gives out CSS

div {
  color: red;
  padding: white;
  font-size: 10px;
  padding: 5;
}

Upvotes: 12

crazyrohila
crazyrohila

Reputation: 616

yeah you already doing it right. You can call first mixin in second one. check this pen http://codepen.io/crazyrohila/pen/mvqHo

Upvotes: 24

Related Questions