studiometa / scss-toolkit

A small and configurable SCSS Toolkit to boost your project! 🚀
MIT License
5 stars 0 forks source link

Proposal for maps loops mixins #20

Closed titouanmathis closed 5 years ago

titouanmathis commented 5 years ago

In order to ease the use of our configuration maps, I think we could create a function/mixin to automatically generate CSS based on their values.

For example, we often use the values of breakpoints as modifiers with the following @each loop:

.my-class {
  display: block;
}

@each $breakpoint in $breakpoints {
  $key: nth($breakpoint, 1);

  @media #{md($key} {
    .my-class--#{$key} {
      display: block;
    }
  }
}

What if we could abstract this @each mechanic into a mixin or a function? I have a working, but limited, example for a mixin:

@mixin breakpoints-modifier {
  @each $breakpoint in $breakpoints {
    $key: nth($breakpoint, 1);

    @media #{md($key)} {
      &--#{$key} {
        @content;
      }
    }
  }
}

.my-class {
  display: block;

  @include breakpoints-modifier {
    display: block;
  }
}

I would be glad to have some feedback on this, @studiometa/studiometa-scss-toolkit 🙂

w-jerome commented 5 years ago

I thinks is a better pratice. We rewrite the previous function who use it ?

perruche commented 5 years ago

👍 dry approach This will be clearer to read, and easier to update in the future.

Shall we also add a way to pass a force option ?

lusimeon commented 5 years ago

It will be usefull, but it need to be more extendable:

titouanmathis commented 5 years ago
@mixin for-each-breakpoints() {
  @each $breakpoint in $breakpoints {
    $key: nth($breakpoint, 1);

    @media #{md($key)} {
      @content ($key);
    }
  }
}

.foo {
  display: block;
}

@include for-each-breakpoints using ($key) {
  .foo--#{$key} {
    display: block;
  }
}
perruche commented 5 years ago

Like @lusimeon idea to have a list of breakpoints to include, to reduce the number of generated classes. In order to make this work we need a dedicated $var for each loop ? Or I am missing something ?

titouanmathis commented 5 years ago

See #21 for a more detailed proposal.

titouanmathis commented 5 years ago

Closed by #21 🙂