skip to Main Content

Sass announced there will be some upcoming breaking changes to the syntax in mixed declarations. As of Sass 1.77.7 I’m getting the following warnings when compiling. –

Sass’s behavior for declarations that appear after nested
rules will be changing to match the behavior specified by CSS in an upcoming
version. To keep the existing behavior, move the declaration above the nested
rule. To opt into the new behavior, wrap the declaration in & {}.

Below is a basic example of the issue. If we define a mixin and then add it to a style class at the top of the class then declare other rules we will receive the above warning in the console.

@mixin border($color) {
    border: 1px solid $color;
}


.style-with-mixin {
    @include border(red);
    padding: 20px;
    // ...Any other styles
}

To remove this error we either have to wrap styles like the following –

.style-with-mixin {
    @include border(red);

    &{
        padding: 20px;
        // ...Any other styles
    }
}

Or shift the mixin to be the last style declaration –

.style-with-mixin {
    padding: 20px;
    // ...Any other styles
    @include border(red);
}

However, both of these solutions involve a lot of rewriting of styles for whole projects which isn’t going to be realistic for me. Other than locking the sass version has anyone been able to find a work around that can be implemented efficiently in exisiting projects?

2

Answers


  1. there is no need to add &{} everywhere
    just reorder your lines
    make sure props at beginning then the nesting selectors

    example this

    .main{
       .content{
         color: green;
       }
       font-size: 1em;
    }
    

    should be

    .main{
       font-size: 1em;
       .content{
         color: green;
       }
    }
    
    Login or Signup to reply.
  2. The problem is even worse when you have mixins that also use nested rules inside. I have no clue how to fix the deprecations then without having multiple selector duplicates in my output css.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search