Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limiting variable scope without nesting into a subclass or mixin?

I would like to use a variable in a couple of classes, so i do:

$height: 100px

#foo
  height: $height

#bar
  height: $height

But this pollutes the global variable scope, so i would like to use a subscope.

When i have a common container for the elements, it's simple:

#common-container
  $height: 100px

  #foo
    height: $height

  #bar
    height: $height

But instead of polluting to global variable scope, this approach pollutes to resulting CSS: chained selectors are absolutely unnecessary. In some cases there's just no common container for the elements, so this approach is not an option at all.

I tried working around this issue by using a dummy mixin:

=local-scope
  @content

It seems to work fine:

+local-scope
  $foo: foo

@warn $foo // -> Error: Undefined variable: "$font-size".

But if a variable is declared before using the mixin, it gets overwritten! :(

$foo: foo

+local-scope
  $foo: bar

@warn $foo // -> bar

The question is: how do i limit a variable scope correctly without messing with the global namespace and without unnecessarily chaining selectors?

like image 352
Andrey Mikhaylov - lolmaus Avatar asked Jan 31 '26 00:01

Andrey Mikhaylov - lolmaus


1 Answers

While this prevents inline @imports, I think that's a dark pattern anyway, as it secretly encourages complex specificity.

@at-root {
  $this: that;  // not a global variable

  .your-original-css {
    // rules
  }
}
like image 90
Jason T Featheringham Avatar answered Feb 02 '26 17:02

Jason T Featheringham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!