sass:meta

Compatibility:
Dart Sass
since 1.23.0
LibSass
Ruby Sass

Only Dart Sass currently supports loading built-in modules with @use. Users of other implementations must call functions using their global names instead.

混入混入 permalink

meta.apply($mixin, $args...)
Compatibility:
Dart Sass
since 1.69.0
LibSass
Ruby Sass

包含带有 $args$mixin。如果传递了 @content,则将其转发给 $mixin

$mixin 必须是混入值,例如由 meta.get-mixin() 返回的值。

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;
}








meta.load-css($url, $with: null)
Compatibility:
Dart Sass
since 1.23.0
LibSass
Ruby Sass

目前只有 Dart Sass 支持这个混入。

加载 $url 处的模块,并包含其 CSS,就像它是这个混入的内容一样。$with 参数为模块提供配置;如果传递,它必须是从变量名(不带 $)到这些变量在加载的模块中使用的值的映射。

如果 $url 是相对的,则相对于包含 meta.load-css() 的文件进行解释。

@use 规则相同

  • 即使以不同方式多次加载,也只会评估给定模块一次。

  • 不能为已加载的模块提供配置,无论它是否已使用配置加载。

@use 规则不同

  • 不会使加载的模块的任何成员在当前模块中可用。

  • 可以在样式表的任何位置使用。甚至可以嵌套在样式规则中以创建嵌套样式!

  • 要加载的模块 URL 可以来自变量并包含插值

⚠️ Heads up!

$url 参数应该是一个包含类似传递给 @use 规则的 URL 的字符串。不应该是 CSS url()

SCSS Syntax

// dark-theme/_code.scss
$border-contrast: false !default;

code {
  background-color: #6b717f;
  color: #d2e1dd;
  @if $border-contrast {
    border-color: #dadbdf;
  }
}
// style.scss
@use "sass:meta";

body.dark {
  @include meta.load-css("dark-theme/code",
      $with: ("border-contrast": true));
}

Sass Syntax

// dark-theme/_code.sass
$border-contrast: false !default

code
  background-color: #6b717f
  color: #d2e1dd
  @if $border-contrast
    border-color: #dadbdf


// style.sass
@use "sass:meta"

body.dark
  $configuration: ("border-contrast": true)
  @include meta.load-css("dark-theme/code", $with: $configuration)

CSS Output

body.dark code {
  background-color: #6b717f;
  color: #d2e1dd;
  border-color: #dadbdf;
}














函数函数 permalink

meta.accepts-content($mixin) //=> boolean
Compatibility:
Dart Sass
since 1.69.0
LibSass
Ruby Sass

返回给定的混入值是否可以接受 @content

如果混入可能接受 @content 块,即使它并不总是这样做,也返回 true。

meta.calc-args($calc) //=> list
Compatibility:
Dart Sass
since 1.40.0
LibSass
Ruby Sass

返回给定计算的参数。

如果参数是数字或嵌套计算,则返回该类型。否则,返回未加引号的字符串。

Playground

SCSS Syntax

@use 'sass:meta';

@debug meta.calc-args(calc(100px + 10%)); // unquote("100px + 10%")
@debug meta.calc-args(clamp(50px, var(--width), 1000px)); // 50px, unquote("var(--width)"), 1000px
Playground

Sass Syntax

@use 'sass:meta'

@debug meta.calc-args(calc(100px + 10%))  // unquote("100px + 10%")
@debug meta.calc-args(clamp(50px, var(--width), 1000px))  // 50px, unquote("var(--width)"), 1000px
meta.calc-name($calc) //=> quoted string
Compatibility:
Dart Sass
since 1.40.0
LibSass
Ruby Sass

返回给定计算的名称。

Playground

SCSS Syntax

@use 'sass:meta';

@debug meta.calc-name(calc(100px + 10%)); // "calc"
@debug meta.calc-name(clamp(50px, var(--width), 1000px)); // "clamp"
Playground

Sass Syntax

@use 'sass:meta'

@debug meta.calc-name(calc(100px + 10%))  // "calc"
@debug meta.calc-name(clamp(50px, var(--width), 1000px))  // "clamp"
meta.call($function, $args...)
call($function, $args...)
Compatibility (Argument Type):
Dart Sass
LibSass
since 3.5.0
Ruby Sass
since 3.5.0

In older versions of LibSass and Ruby Sass, the call() function took a string representing a function’s name. This was changed to take a function value instead in preparation for a new module system where functions are no longer global and so a given name may not always refer to the same function.

Passing a string to call() still works in all implementations, but it’s deprecated and will be disallowed in future versions.

调用带有 $args$function 并返回结果。

$function 必须是函数值,例如由 meta.get-function() 返回的值。

Playground

SCSS Syntax

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

/// Return a copy of $list with all elements for which $condition returns `true`
/// removed.
@function remove-where($list, $condition) {
  $new-list: ();
  $separator: list.separator($list);
  @each $element in $list {
    @if not meta.call($condition, $element) {
      $new-list: list.append($new-list, $element, $separator: $separator);
    }
  }
  @return $new-list;
}

$fonts: Tahoma, Geneva, "Helvetica Neue", Helvetica, Arial, sans-serif;

.content {
  @function contains-helvetica($string) {
    @return string.index($string, "Helvetica");
  }
  font-family: remove-where($fonts, meta.get-function("contains-helvetica"));
}
Playground

Sass Syntax

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

/// Return a copy of $list with all elements for which $condition returns `true`
/// removed.
@function remove-where($list, $condition)
  $new-list: ()
  $separator: list.separator($list)
  @each $element in $list
    @if not meta.call($condition, $element)
      $new-list: list.append($new-list, $element, $separator: $separator)


  @return $new-list


$fonts: Tahoma, Geneva, "Helvetica Neue", Helvetica, Arial, sans-serif

.content
  @function contains-helvetica($string)
    @return string.index($string, "Helvetica")

  font-family: remove-where($fonts, meta.get-function("contains-helvetica"))

CSS Output

.content {
  font-family: Tahoma, Geneva, Arial, sans-serif;
}






















meta.content-exists()
content-exists() //=> boolean

返回当前混入是否被传递了 @content

如果在混入外部调用,则抛出错误。

Playground

SCSS Syntax

@use 'sass:meta';

@mixin debug-content-exists {
  @debug meta.content-exists();
  @content;
}

@include debug-content-exists; // false
@include debug-content-exists { // true
  // Content!
}
Playground

Sass Syntax

@use 'sass:meta'

@mixin debug-content-exists
  @debug meta.content-exists()
  @content


@include debug-content-exists  // false
@include debug-content-exists   // true
  // Content!

meta.feature-exists($feature)
feature-exists($feature) //=> boolean

返回当前 Sass 实现是否支持 $feature

$feature 必须是一个字符串。目前识别的特性有:

对于任何无法识别的 $feature,返回 false

⚠️ Heads up!

此函数已弃用,应避免使用。详情请参见重大变更页面

meta.function-exists($name, $module: null)
function-exists($name) //=> boolean

Returns whether a function named $name is defined, either as a built-in function or a user-defined function.

If $module is passed, this also checks the module named $module for the function definition. $module must be a string matching the namespace of a @use rule in the current file.

Playground

SCSS Syntax

@use "sass:meta";
@use "sass:math";

@debug meta.function-exists("div", "math"); // true
@debug meta.function-exists("scale-color"); // true
@debug meta.function-exists("add"); // false

@function add($num1, $num2) {
  @return $num1 + $num2;
}
@debug meta.function-exists("add"); // true
Playground

Sass Syntax

@use "sass:meta"
@use "sass:math"

@debug meta.function-exists("div", "math")  // true
@debug meta.function-exists("scale-color")  // true
@debug meta.function-exists("add")  // false

@function add($num1, $num2)
  @return $num1 + $num2

@debug meta.function-exists("add")  // true
meta.get-function($name, $css: false, $module: null)
get-function($name, $css: false, $module: null) //=> function

Returns the function value named $name.

If $module is null, this returns the function named $name without a namespace (including global built-in functions). Otherwise, $module must be a string matching the namespace of a @use rule in the current file, in which case this returns the function in that module named $name.

By default, this throws an error if $name doesn’t refer to Sass function. However, if $css is true, it instead returns a plain CSS function.

The returned function can be called using meta.call().

Playground

SCSS Syntax

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

/// Return a copy of $list with all elements for which $condition returns `true`
/// removed.
@function remove-where($list, $condition) {
  $new-list: ();
  $separator: list.separator($list);
  @each $element in $list {
    @if not meta.call($condition, $element) {
      $new-list: list.append($new-list, $element, $separator: $separator);
    }
  }
  @return $new-list;
}

$fonts: Tahoma, Geneva, "Helvetica Neue", Helvetica, Arial, sans-serif;

.content {
  @function contains-helvetica($string) {
    @return string.index($string, "Helvetica");
  }
  font-family: remove-where($fonts, meta.get-function("contains-helvetica"));
}
Playground

Sass Syntax

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

/// Return a copy of $list with all elements for which $condition returns `true`
/// removed.
@function remove-where($list, $condition)
  $new-list: ()
  $separator: list.separator($list)
  @each $element in $list
    @if not meta.call($condition, $element)
      $new-list: list.append($new-list, $element, $separator: $separator)


  @return $new-list


$fonts: Tahoma, Geneva, "Helvetica Neue", Helvetica, Arial, sans-serif

.content
  @function contains-helvetica($string)
    @return string.index($string, "Helvetica")

  font-family: remove-where($fonts, meta.get-function("contains-helvetica"))

CSS Output

.content {
  font-family: Tahoma, Geneva, Arial, sans-serif;
}






















meta.get-mixin($name, $module: null) //=> function
Compatibility:
Dart Sass
since 1.69.0
LibSass
Ruby Sass

Returns the mixin value named $name.

If $module is null, this returns the mixin named $name defined in the current module. Otherwise, $module must be a string matching the namespace of a @use rule in the current file, in which case this returns the mixin in that module named $name.

By default, this throws an error if $name doesn’t refer to a mixin.

The returned mixin can be included using meta.apply().

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;
}








meta.global-variable-exists($name, $module: null)
global-variable-exists($name, $module: null) //=> boolean

Returns whether a global variable named $name (without the $) exists.

If $module is null, this returns whether a variable named $name without a namespace exists. Otherwise, $module must be a string matching the namespace of a @use rule in the current file, in which case this returns whether that module has a variable named $name.

See also meta.variable-exists().

Playground

SCSS Syntax

@use "sass:meta";

@debug meta.global-variable-exists("var1"); // false

$var1: value;
@debug meta.global-variable-exists("var1"); // true

h1 {
  // $var2 is local.
  $var2: value;
  @debug meta.global-variable-exists("var2"); // false
}
Playground

Sass Syntax

@use "sass:meta"

@debug meta.global-variable-exists("var1")  // false

$var1: value
@debug meta.global-variable-exists("var1")  // true

h1
  // $var2 is local.
  $var2: value
  @debug meta.global-variable-exists("var2")  // false

meta.inspect($value)
inspect($value) //=> unquoted string

Returns a string representation of $value.

Returns a representation of any Sass value, not just those that can be represented in CSS. As such, its return value is not guaranteed to be valid CSS.

⚠️ Heads up!

This function is intended for debugging; its output format is not guaranteed to be consistent across Sass versions or implementations.

Playground

SCSS Syntax

@use "sass:meta";

@debug meta.inspect(10px 20px 30px); // unquote("10px 20px 30px")
@debug meta.inspect(("width": 200px)); // unquote('("width": 200px)')
@debug meta.inspect(null); // unquote("null")
@debug meta.inspect("Helvetica"); // unquote('"Helvetica"')
Playground

Sass Syntax

@use "sass:meta"

@debug meta.inspect(10px 20px 30px)  // unquote("10px 20px 30px")
@debug meta.inspect(("width": 200px))  // unquote('("width": 200px)')
@debug meta.inspect(null)  // unquote("null")
@debug meta.inspect("Helvetica")  // unquote('"Helvetica"')
meta.keywords($args)
keywords($args) //=> map

Returns the keywords passed to a mixin or function that takes arbitrary arguments. The $args argument must be an argument list.

The keywords are returned as a map from argument names as unquoted strings (not including $) to the values of those arguments.

Playground

SCSS Syntax

@use "sass:meta";

@mixin syntax-colors($args...) {
  @debug meta.keywords($args);
  // (string: #080, comment: #800, variable: #60b)

  @each $name, $color in meta.keywords($args) {
    pre span.stx-#{$name} {
      color: $color;
    }
  }
}

@include syntax-colors(
  $string: #080,
  $comment: #800,
  $variable: #60b,
)
Playground

Sass Syntax

@use "sass:meta"

@mixin syntax-colors($args...)
  @debug meta.keywords($args)
  // (string: #080, comment: #800, variable: #60b)

  @each $name, $color in meta.keywords($args)
    pre span.stx-#{$name}
      color: $color




@include syntax-colors($string: #080, $comment: #800, $variable: #60b)




CSS Output

pre span.stx-string {
  color: #080;
}

pre span.stx-comment {
  color: #800;
}

pre span.stx-variable {
  color: #60b;
}







meta.mixin-exists($name, $module: null)
mixin-exists($name, $module: null) //=> boolean

Returns whether a mixin named $name exists.

If $module is null, this returns whether a mixin named $name without a namespace exists. Otherwise, $module must be a string matching the namespace of a @use rule in the current file, in which case this returns whether that module has a mixin named $name.

Playground

SCSS Syntax

@use "sass:meta";

@debug meta.mixin-exists("shadow-none"); // false

@mixin shadow-none {
  box-shadow: none;
}

@debug meta.mixin-exists("shadow-none"); // true
Playground

Sass Syntax

@use "sass:meta"

@debug meta.mixin-exists("shadow-none")  // false

@mixin shadow-none
  box-shadow: none


@debug meta.mixin-exists("shadow-none")  // true
meta.module-functions($module) //=> map
Compatibility:
Dart Sass
since 1.23.0
LibSass
Ruby Sass

Only Dart Sass currently supports this function.

Returns all the functions defined in a module, as a map from function names to function values.

The $module parameter must be a string matching the namespace of a @use rule in the current file.

SCSS Syntax

// _functions.scss
@function pow($base, $exponent) {
  $result: 1;
  @for $_ from 1 through $exponent {
    $result: $result * $base;
  }
  @return $result;
}
@use "sass:map";
@use "sass:meta";

@use "functions";

@debug meta.module-functions("functions"); // ("pow": get-function("pow"))

@debug meta.call(map.get(meta.module-functions("functions"), "pow"), 3, 4); // 81

Sass Syntax

// _functions.sass
@function pow($base, $exponent)
  $result: 1
  @for $_ from 1 through $exponent
    $result: $result * $base

  @return $result

@use "sass:map"
@use "sass:meta"

@use "functions"

@debug meta.module-functions("functions") // ("pow": get-function("pow"))

@debug meta.call(map.get(meta.module-functions("functions"), "pow"), 3, 4) // 81
meta.module-mixins($module) //=> map
Compatibility:
Dart Sass
since 1.69.0
LibSass
Ruby Sass

Returns all the mixins defined in a module, as a map from mixin names to mixin values.

The $module parameter must be a string matching the namespace of a @use rule in the current file.

SCSS Syntax

// _mixins.scss
@mixin stretch() {
  align-items: stretch;
  display: flex;
  flex-direction: row;
}
@use "sass:map";
@use "sass:meta";

@use "mixins";

@debug meta.module-mixins("mixins"); // => ("stretch": get-mixin("stretch"))

.header {
  @include meta.apply(map.get(meta.module-mixins("mixins"), "stretch"));
}

Sass Syntax

// _mixins.scss
@mixin stretch()
  align-items: stretch
  display: flex
  flex-direction: row

@use "sass:map"
@use "sass:meta"

@use "mixins"

@debug meta.module-mixins("mixins") // => ("stretch": get-mixin("stretch"))

.header
  @include meta.apply(map.get(meta.module-mixins("mixins"), "stretch"))

CSS Output

.header {
  align-items: stretch;
  display: flex;
  flex-direction: row;
}













meta.module-variables($module) //=> map
Compatibility:
Dart Sass
since 1.23.0
LibSass
Ruby Sass

Only Dart Sass currently supports this function.

Returns all the variables defined in a module, as a map from variable names (without $) to the values of those variables.

The $module parameter must be a string matching the namespace of a @use rule in the current file.

SCSS Syntax

// _variables.scss
$hopbush: #c69;
$midnight-blue: #036;
$wafer: #e1d7d2;
@use "sass:meta";

@use "variables";

@debug meta.module-variables("variables");
// (
//   "hopbush": #c69,
//   "midnight-blue": #036,
//   "wafer": #e1d7d2
// )

Sass Syntax

// _variables.sass
$hopbush: #c69
$midnight-blue: #036
$wafer: #e1d7d2
@use "sass:meta"

@use "variables"

@debug meta.module-variables("variables")
// (
//   "hopbush": #c69,
//   "midnight-blue": #036,
//   "wafer": #e1d7d2
// )
meta.type-of($value)
type-of($value) //=> unquoted string

Returns the type of $value.

This can return the following values:

New possible values may be added in the future. It may return either list or map for (), depending on whether or not it was returned by a map function.

Playground

SCSS Syntax

@use 'sass:meta';

@debug meta.type-of(10px); // number
@debug meta.type-of(10px 20px 30px); // list
@debug meta.type-of(()); // list
Playground

Sass Syntax

@use 'sass:meta'

@debug meta.type-of(10px)  // number
@debug meta.type-of(10px 20px 30px)  // list
@debug meta.type-of(())  // list
meta.variable-exists($name)
variable-exists($name) //=> boolean

Returns whether a variable named $name (without the $) exists in the current scope.

See also meta.global-variable-exists().

Playground

SCSS Syntax

@use "sass:meta";

@debug meta.variable-exists("var1"); // false

$var1: value;
@debug meta.variable-exists("var1"); // true

h1 {
  // $var2 is local.
  $var2: value;
  @debug meta.variable-exists("var2"); // true
}
Playground

Sass Syntax

@use "sass:meta"

@debug meta.variable-exists("var1")  // false

$var1: value
@debug meta.variable-exists("var1")  // true

h1
  // $var2 is local.
  $var2: value
  @debug meta.variable-exists("var2")  // true