Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid Sass variable name repeat in _variables.scss?

I have _variables.scss which is structured like this following BEM naming conventions.

$slot-width: 100px;
$slot-height: 10px;
$slot-title-width: 80px;
$slot-title-height: 10px;

I'm repeating variable names $slot-. Is there a way to do something like:

$slot {
  &-width: 100px;
  &-height: 10px;
  &-title {
    &-width: 80px;
    &-height: 10px;
  }
}

And it compiles to the above?

like image 256
thinkvantagedu Avatar asked Nov 22 '25 05:11

thinkvantagedu


2 Answers

You could use Sass Maps:

$slot: (
  "width": 100px,
  "height": 10px,
  "title": (
    "width": 80px,
    "height": 10px
  )
);

Additionally, here's a good article on getting values out of deep Sass maps.

However, it may be easier to continue using regular variables with hyphens, as you already have.

like image 82
Sean Avatar answered Nov 23 '25 22:11

Sean


You could use a map :

$slot: (
    "width": 100px,
    "height": 10px
);

.some_bem__selector { width: map-get($slot, width); }
like image 29
net.uk.sweet Avatar answered Nov 23 '25 22:11

net.uk.sweet