Reputation: 936
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
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
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
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