Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localizing/Scoping a sass mixin

Tags:

css

sass

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?

like image 383
Mike Fielden Avatar asked Nov 11 '13 14:11

Mike Fielden


People also ask

What is the use of mixin in Sass?

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

How to use @mixins in SCSS?

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.

How can Sass variables and mixins be used in child themes?

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.

What is the difference between mixin and reset_list in Sass?

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.


1 Answers

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);
  }
}
like image 136
Mike Fielden Avatar answered Sep 27 '22 22:09

Mike Fielden