Scope of variables and content blocks in SASS
When passing content blocks to a mixin, their scope is determined by the specific location of the block, not the mixin. Therefore, we cannot use local variables of the mixin in the passed content block, which will only be able to work with global variables.
Let's consider the following example:
styles.scss$size: 14px;
@mixin sizes ($size: 20px) {
font-size: $size;
padding: $size;
@content;
}
.navbar {
@include sizes {
margin: $size;
}
}
Compilation result:
styles.css.navbar {
font-size: 20px;
padding: 20px;
margin: 14px;
}
Задание 1
Tell me what the result of compiling the following code will be:
styles.scss$color: yellow;
@mixin links ($color: red) {
background: $color;
@content;
}
Задание 2
Tell me what the result of compiling the following code will be:
styles.scss$color: yellow;
$size: 10px;
@mixin links ($color: red, $size: 12px) {
background-color: $color;
font-size: $size;
@content;
}
.navbar {
@include links {
box-shadow: $color;
padding: $size;
}
}
Задание 3
Tell me what the result of compiling the following code will be:
styles.scss$color: green;
$size: 6px;
@mixin links ($color: red, $size: 10px) {
color: $color;
font-size: $size;
@content;
}
.navbar {
@include links {
border-color: $color;
margin: $size;
}
}
#active{
@include links{
background-color: $color;
padding-top: $size;
padding-bottom: $size * 2;
}
}