Home Sass Meta meta.get-mixin() Definition
Definition and Usage meta.get-mixin($name, $module: null) returns a mixin value for the named mixin. Include it with @include meta.apply() . Load it with @use "sass:meta" on Dart Sass 1.69+ .
Official sample idea. Build an apply-to-all helper that receives meta.get-mixin("font-class") and calls @include meta.apply($mixin, $element) for each list item.
$module for namespaces.Pass a string matching a @use namespace, or a module value, to resolve mixins from that module.
Missing names throw. Official docs: if $name is not a mixin, Sass errors. Guard optional names with meta.mixin-exists first.
Syntax
Syntax @use "sass:meta";
meta.get-mixin($name, $module: null); //=> mixin
@mixin font-class($size) {
.font-#{$size} { font-size: $size; }
}
@include meta.apply(meta.get-mixin("font-class"), 12px);
@include meta.apply(meta.get-mixin("wrap")) {
// forwarded @content
}
// Module API — prefer meta.get-mixin after @use "sass:meta" Pattern: resolve a mixin value → @include meta.apply with args → optionally forward @content. Check meta.accepts-content when content is optional.
Code
SCSS Examples Examples target Dart Sass 1.69+ with @use "sass:meta". Open View Compiled CSS for verified output (matches official Sass docs samples where shown).
1. Basic 2. apply-to-all 3. Forward @content 4. With accepts-content 5. With mixin-exists Example 1 — Resolve and Apply Once Get a mixin value, then include it with one argument.
@use "sass:meta";
@mixin font-class($size) {
.font-#{$size} {
font-size: $size;
}
}
@include meta.apply(meta.get-mixin("font-class"), 12px); .font-12px {
font-size: 12px;
} How It Works get-mixin returns a mixin value.meta.apply includes it with 12px.CSS emits from the target mixin.Same idea as @include font-class(12px), but dynamic.Example 2 — Official Docs apply-to-all Pass a mixin value into a helper that applies it to each list item.
@use "sass:meta";
/// 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); .font-8px {
font-size: 8px;
}
.font-12px {
font-size: 12px;
}
.font-2rem {
font-size: 2rem;
} How It Works get-mixin captures font-class.Higher-order mixin receives that value + list.Each size is applied separately.Matches the official Sass docs CSS output.Example 3 — Forward a @content Block Content passed to meta.apply is forwarded into the resolved mixin.
@use "sass:meta";
@mixin with-content {
.inner {
@content;
}
}
.wrap {
@include meta.apply(meta.get-mixin("with-content")) {
color: red;
}
} .wrap .inner {
color: red;
} How It Works get-mixin resolves the content-capable mixin.Caller passes a content block to meta.apply.Sass forwards that block into the mixin.Result nests color: red under .inner.Example 4 — Guard Content with accepts-content Forward a content block only when the resolved mixin can take one.
@use "sass:meta";
@mixin plain {
color: blue;
}
@mixin with-content {
.inner {
@content;
}
}
@mixin apply-if-content($name) {
$mixin: meta.get-mixin($name);
@if meta.accepts-content($mixin) {
@include meta.apply($mixin) {
outline: 1px solid red;
}
} @else {
@include meta.apply($mixin);
}
}
.a {
@include apply-if-content("plain");
}
.b {
@include apply-if-content("with-content");
} .a {
color: blue;
}
.b .inner {
outline: 1px solid red;
} How It Works get-mixin resolves by string name.accepts-content checks capability.Plain mixins get a content-free apply.Content mixins receive the outline block.Example 5 — Guard with mixin-exists Avoid a throw when the mixin name might be missing.
@use "sass:meta";
@mixin badge {
.badge {
display: inline-block;
}
}
.exists {
@if meta.mixin-exists("badge") {
@include meta.apply(meta.get-mixin("badge"));
}
} .exists .badge {
display: inline-block;
} How It Works mixin-exists answers yes/no safely.get-mixin only runs on the true path.apply emits the badge rule.Safer for optional plugin-style mixins. Edge Cases & Pitfalls Most get-mixin bugs are missing names, forgetting apply, or confusing functions with mixins.
include Resolve ≠ include You still need @include meta.apply($mixin, …) to emit CSS.
module Namespaced mixins need $module Match the @use namespace string, or pass a module value.
vs Functions use get-function Dynamic functions return values via meta.get-function + meta.call.
Toolchain
Sass Support Prefer the module form with Dart Sass 1.69+.
API Status meta.get-mixin()Dart Sass 1.69+ via @use "sass:meta" Return type Mixin value Include with @include meta.apply($mixin, $args...)LibSass / Ruby Sass Not available
Compile with the Dart Sass CLI .
Remember
Key Takeaways Returns a mixin value for a name (and optional module).
Include with meta.apply ; content blocks are forwarded.
Pair with mixin-exists / accepts-content for safe dynamic includes.
Dart Sass 1.69+ via @use "sass:meta".
One line: when you need a mixin value to pass around or include dynamically, use meta.get-mixin($name, $module: null).
Practice
Compile Notes Practical checklist:
1. @use "sass:meta"
2. meta.get-mixin("name") or with $module
3. @include meta.apply($mixin, $args...)
4. Optionally forward @content (guard with accepts-content)
5. Prefer mixin-exists before optional lookups Frequently Asked Questions What does Sass meta.get-mixin() do? meta.get-mixin($name, $module: null) returns a mixin value for the named mixin. You include that value with @include meta.apply($mixin, $args...).
When do I pass $module? When the mixin lives under a @use namespace. $module must match that namespace string in the current file, or you can pass a module value.
How is it different from meta.get-function? get-mixin resolves mixins (for meta.apply / CSS). get-function resolves functions (for meta.call / returned values).
What if the name is missing? Official docs: get-mixin throws if $name does not refer to a mixin. Guard optional names with meta.mixin-exists first.
Does it support @content? get-mixin only resolves the mixin. When you @include meta.apply($mixin) { … }, that content block is forwarded into the applied mixin.
Which compilers support meta.get-mixin? Dart Sass 1.69+. LibSass and Ruby Sass do not provide this API. Prefer current Dart Sass.
CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion