1

Let's say I have an existing code base using sass variables i.e.

$sw-primary-1: #ccc;

and these classes are used all over the place i.e.

.lifecycle {
    &.new {
      background-color: $sw-primary-1:
    }
  }
}

Is there anything I can do a large existing code base like this where I know want to change this colour palette variables depending on a different class on the body.

i.e. body class="redesign"

where I basically want to go

.redesign {
  $sw-primary-1: blue;
}

But the above doesn't work it still sticks with the original sass variable color.

StevieB
  • 6,263
  • 38
  • 108
  • 193

2 Answers2

5

It works.
Note that you should use the !global flag as well.

All variable assignments not at the top level of the document are now local by default. If there's a global variable with the same name, it won't be overwritten unless the !global flag is used. For example, $var: value !global will assign to $var globally. This behavior can be detected using feature-exists(global-variable-shadowing).

Source

$primary-color: blue;

.foo {
  color: $primary-color;
}

.bar {
  $primary-color: red !global;
  color: $primary-color;
}
Quentin Veron
  • 3,079
  • 1
  • 14
  • 32
0

Another way to accomplish this is using mixins.

@mixin bkcolor($color) {
    background-color:$color;
}

.lifecycle {
    &.new {
      @include bkcolor('blue');
    }
  }
}
Patricio Vargas
  • 5,236
  • 11
  • 49
  • 100