混入值

兼容性:
Dart Sass
since 1.69.0
LibSass
Ruby Sass

混入 也可以是值!你不能直接将 mixin 编写为值,但可以将 mixin 的名称传递给 meta.get-mixin() 函数 以将其作为值获取。一旦你有了 mixin 值,你就可以将它传递给 meta.apply() 混入 来调用它。这是为了让库能够以复杂而强大的方式进行扩展。

¥Mixins can also be values! You can’t directly write a mixin as a value, but you can pass a mixin’s name to the meta.get-mixin() function to get it as a value. Once you have a mixin value, you can pass it to the meta.apply() mixin to call it. This is for libraries to be extensible in complex and powerful ways.

Playground

SCSS Syntax

@use "sass:meta";
@use "sass:string";

/// Passes each element of $list to a separate invocation of $mixin.
@mixin apply-to-all($mixin, $list) {
  @each $element in $list {
    @include meta.apply($mixin, $element);
  }
}

@mixin font-class($size) {
  .font-#{$size} {
    font-size: $size;
  }
}

$sizes: [8px, 12px, 2rem];

@include apply-to-all(meta.get-mixin("font-class"), $sizes);
Playground

Sass Syntax

@use "sass:meta"
@use "sass:string"

/// Passes each element of $list to a separate invocation of $mixin.
@mixin apply-to-all($mixin, $list)
  @each $element in $list
    @include meta.apply($mixin, $element)



@mixin font-class($size)
  .font-#{$size}
    font-size: $size



$sizes: 8px, 12px 2rem

@include apply-to-all(meta.get-mixin("font-class"), $sizes)

CSS Output

.font-8px {
  font-size: 8px;
}

.font-12px {
  font-size: 12px;
}

.font-2rem {
  font-size: 2rem;
}