且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

LESS:如何将mixin作为参数传递给另一个mixin?

更新时间:2023-11-10 08:09:28

mixin name)我想说你缺少这样的事实,在 .on-small / .on-medium 这些 small medium 的东西也只是参数,因此不应该是mixin名称的一部分。考虑到你的例子变成:

In this particular case (unlike a general case with an arbitrary mixin name) I'd say you're missing the fact that in .on-small/.on-medium these small and medium things are also nothing but parameters and thus should not be a part of the mixin names. With this in mind your example becomes:

.on(small, @rules) {
  @media (@minWidthSmall) {@rules();}
}

.on(medium, @rules) {
  @media (@minWidthMedium) {@rules();}
}

.make-col(@device, @span, @size) {
  flex: 1;
  box-sizing: border-box;
  .on(@device, {
    width: percentage(@span/@size);
    min-width: percentage(@span/@size);
  });
}

// usage:

.make-col(small, @span, @size);

.grid-col-on - * mixins相同,它们只是一个:

Same for your .grid-col-on-* mixins, they are just a single:

.grid-col-on(@device, @span: 1, @size: 1) {
  .make-col(@device, @span, @size);
}

等。

如果你真的想要一个灵活的/通用的网格 - 永远不会将硬编码设备/断点名称改成混合或变量名称(更多的理由和例子参见 https://github.com/less/less.js/issues/2702 )。

If you really want a flexible/generic grid - never hardcode device/breakpoint names into mixin or variable names (for more rationale and examples see https://github.com/less/less.js/issues/2702).