Mixin Values
- Dart Sass
- since 1.69.0
- LibSass
- ✗
- Ruby Sass
- ✗
Compatibility:
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;
}