Is there a way to localize a SCSS mixin to apply only to a certain scope?
I have the following example
... where I have a couple basic mixins
@mixin shadow ($color) {
text-shadow: 4px 3px 0 $color, 7px 6px 0 rgba(0,0,0,.2);
}
@mixin shadow-icon ($color) {
span[class*='entypo'] {
@include shadow($color);
font-size: 156px;
}
}
I dont want to repeat the exact same code for every item
with just a different color. But this will be a one off style for this page so I dont want these to be accessible from anywhere just this file/scope/etc.
Does there exist a way to localize or scope a mixin?
SASS mixins are one of the important feature for style reusability, These are set of styles grouped under one name with dynamic arguments. Define mixin -@mixins keyword is rule is used to create a css styles Using Mixin - @includes rules is being used to include the mixin styles in the current scope
Mixins are characterized utilizing the @mixin at-rule, which is composed @ mixin <name> { … } or @mixin name (<arguments…>) { … } Following is a simple example of using mixing to define content to centre. Now just using @include to call the mixin will copy the entire style. The beauty is that it can be called n number of times inside the SCSS file.
Leveraging a clever combination of Sass variables and mixins can be useful in creating a pattern for swapping out reusable styles where needed in child themes. This pattern can be extended for other similar style mixins, such as heading styles or iconography. As design systems grow in complexity the need for flexibility also increases.
Mixin names, like all Sass identifiers, treat hyphens and underscores as identical. This means that reset-list and reset_list both refer to the same mixin. This is a historical holdover from the very early days of Sass, when it only allowed underscores in identifier names.
I figured it out. Mixins use the same rules as sass variables in that if they are nested inside a block of code its not viewable/useable outside that block.
So for this what I did was just nest the different "items" inside the item
definition; define the mixin inside the item
definition. And the result is that the mixin can only be used within that item
block.
Check it out
.item {
@mixin shadow-icon ($color) {
span[class*='entypo'] {
text-shadow: 4px 3px 0 $color, 7px 6px 0 rgba(0,0,0,.2);
font-size: 156px;
}
}
&.create {
$createColor: rgb(69, 178, 157);
background: $createColor;
@include shadow-icon($createColor);
}
&.view {
$viewColor: rgb(239, 201, 76);
background: $viewColor;
@include shadow-icon($viewColor);
}
&.report {
$reportColor: rgb(226, 122, 63);
background: $reportColor;
@include shadow-icon($reportColor);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With