【问题标题】:Passing arguments from a mixin to a content block将参数从 mixin 传递到内容块
【发布时间】:2015-04-13 02:15:59
【问题描述】:

这个open issue in the Sass queue 似乎暗示将参数传递给@content 还不是一个功能,但Susy 2 似乎能够做到这一点。追踪它是如何完成的有点像兔子洞,我还没有弄清楚。也许有人可以通过一个简单的例子来阐明?我想创建一个自定义 mixin,它将使用自定义地图继承从 susy-breakpoint() 传递的布局。

示例:在全局 Sass 映射中定义 4 列布局,当在 susy-breakpoint()@content 中指定跨度 4 时,将返回 100% 的宽度。当通过$layout 参数将8 列的自定义布局直接传递给susy-breakpoint() 时,嵌套的span() mixin 会选择新的布局。但是自定义嵌套的 mixin 不会选择新的布局。为什么?

@import 'susy';

$susy: (
  columns: 4,
);

@mixin inherit-layout($layout: 4) {
  columns: $layout;
}

@include susy-breakpoint(30em) {
  // nested code uses an 4-column grid from global map
  .global-cols {
    @include span(4);
    @include inherit-layout();
  }
}

@include susy-breakpoint(48em, $layout: 8) {
  // nested code uses an 8-column grid from $layout
  .inherited-cols {
    @include span(4);
    @include inherit-layout();
  }
}

编译的 CSS:

@media (min-width: 30em) {
  .global-cols {
    width: 100%;
    float: left;
    margin-left: 0;
    margin-right: 0;
    columns: 4;
  }
}

@media (min-width: 48em) {
  .inherited-cols {
    width: 48.71795%;
    float: left;
    margin-right: 2.5641%;
    columns: 4;
  }
}

更新:

我发现,将inherit-value() 的默认变量设置为现有$susy 映射上的columns 键的值允许mixin 获取上下文。但为什么?为什么它不适用于其他地图或susy-breakpoint() 之外的地图?

请看这里:http://sassmeister.com/gist/d86e217aca3aa8337b83

【问题讨论】:

  • 我没有时间去查看Susy的来源,但你的答案可能在这里:stackoverflow.com/questions/21882528/…
  • Susy 的做法真的重要吗?为提供一个用例怎么样?
  • 我想创建一个自定义 mixin,它将使用自定义地图继承从 susy-breakpoint() 传递的布局。 (正如我在原帖中所说)。所以是的,确实如此。

标签: sass susy


【解决方案1】:

Susy 没有将任何参数传递给 @content — 相反,我们在内容块的开头更改全局变量,然后在末尾将其改回:

$example: 4;

@mixin local($local) {
  $old: $example;
  $example: $local !global;

  @content

  $example: $old !global;
}

// out here, $example == 4

@include local(12) {
  // in here, $example == 12
}

【讨论】:

  • 尽可能简单。谢谢埃里克!!
  • 太棒了!谢谢!
猜你喜欢
  • 2020-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-12
  • 2014-04-24
  • 2014-05-07
  • 2016-12-15
  • 2017-06-02
相关资源
最近更新 更多