|unrecognize|未被认识的,未被承认的, 被忽略的|

|recognized|承认,认出,认可|

|invoke|调用,激活,请求,提及|

|contrast|对比,对照,反差|

|relative|相对的,相关的,比较的|

sass:meta

mixins

  • meta.load-css($url, $width: null): loads the module at $url and includes its css, if passed $with, it must be a map(key without $), and $url can be relative
  • $url should be a string but not be a css url()
  • like the @use rule:
    • only evaluate the given module once
    • cannot provide configuration to already loaded module
  • unlike the @use rule:
    • in loaded module, any members don’t avaliable in the current module
    • can used to anywhere in stylesheet even can nested
    • url can come from variable and include interpolation
  1. // dark-theme/_code.scss
  2. $border-contrast: false !default;
  3. code {
  4. background-color: #6b717f;
  5. color: #d2e1dd;
  6. @if $border-contrast {
  7. border-color: #dadbdf;
  8. }
  9. }
  10. // style.scss
  11. @use "sass:meta";
  12. body.dark {
  13. @include meta.load-css("dark-theme/code", $with: ("border-contrast": true));
  14. }

functions

  • meta.call($function, $args…) or call($function, $args…):
    • invoke $function with $args and return result
    • $function should be a function returned by meta.get-function()
  1. @use "sass:list";
  2. @use "sass:meta";
  3. @use "sass:string";
  4. // return a copy of $list with all elements for with $condition returns `true` removed
  5. @function remove-where($list, $condition) {
  6. $new-list: ();
  7. $separator: list.separactor($list);
  8. @each $element in $list {
  9. @if not meta.call($condition, $element) {
  10. $new-list: list.append($new-list, $element, $separactor: $separactor);
  11. }
  12. }
  13. @return $new-list;
  14. }
  15. $fonts: Tahoma, Geneva, "Helvetica Neue", Helvetica, Arial, sans-serif;
  16. content {
  17. @function contains-helvetica($string) {
  18. @return string.index ($string, "Helvetica");
  19. }
  20. font-family: remove-where($fonts, meta.get-function("contains-helvetica"));
  21. }
  • meta.content-exists() or content-exists()
    • return boolean whether the current mixin was passed a @content block
    • throws an error if called outside of a mixin
  1. @mixin debug-content-exists {
  2. @debug meta.content-exists();
  3. @content;
  4. }
  5. @include debug-content-exists; // false
  6. @include debug-content-exists { // true
  7. // content!
  8. }
  • meta.feature-exists($feature) or feature-exists($feature):
    • return boolean whether the current sass-implementation support $feature
    • $feature must be a string:
      • global-variable-shadowing: means local variable will shadow a global variable unless it’s !global flag
      • extend-selector-psedoclass: means @extend rule will affect selector nested in pseudo-class like :not()
      • units-level3: means unit arithmetic support units defined in css values and units level 3
      • at-error: means support @error rule
      • custom-property: means custom property declaration values only support interpolation, doesn’t support other expression
      • any unrecognize $feature return false
  1. @debug meta.feature-exists("at-error"); // true
  2. @debug meta.feature-exists("unrecognize"); // false
  • meta.function-exists($name) or function-exists($name): reurn boolean whether a function is defined
  1. @debug meta.function-exists("scale-color"); // true
  2. @debug meta.function-exists("add"); // false
  3. @function add($num1, $num2) {
  4. @return $num1 + $num2;
  5. }
  6. @debug meta.function-exists("add"); // true
  • meta.get-function($name, $css: false) or get-function($name, $css: false):
    • return named $name function
    • if named $name function not existing, will throws an error
    • if $css is true, it instead returns a plain css function
    • returned function can be called using meta.call()
  1. @use "sass:list";
  2. @use "sass:meta";
  3. @use "sass:string";
  4. // return a copy of $list with all elements for which $condition returns `true` removed
  5. @function remove-where($list, $condition) {
  6. $new-list: ();
  7. $separactor: list.separactor($list);
  8. @each $element in $list {
  9. @if not meta.call($condition, $element) {
  10. $new-list: list.append($new-list, $element, $separator: $srparator);
  11. }
  12. }
  13. @return $new-list;
  14. }
  15. content {
  16. @function contains-helvetica($string) {
  17. @return string.index($string, "Helvetica");
  18. }
  19. font-family: remove-where($fonts, meta.get-function("contains-helvetica"));
  20. }
  • meta.global-variable-exists($name) or global-variable-exists($name): return boolean whether exist a named $name global variable
  1. @debug meta.global-variable-exists("var1"); // false
  2. $var1: value;
  3. @debug meta.global-variable-exists("var1); // true
  4. h1 {
  5. // $var2 is local
  6. $var2: value;
  7. @debug meta.global-variable-exists("var2"); // false
  8. }
  • meta.inspect($value) or inspect($value): return a unquoted string of any sass value not only css value, the function is intended for debugging
  1. @debug meta.inspect(10px 20px 30px); // unquote("10px 20px 30px")
  2. @debug meta.inspect(("width": 200px)); // unquote('("width": 200px)')
  3. @debug meta.inspect(null); // unquote("null")
  4. @debug meta.inspect("Helvetica"); // unquote('"Helvetica"')
  • meta.keywords($args) or keywords($args): return keyword-map(key without $) passed to mixin or function takes arbitrary arguments, $args must be an arguments list
  1. @use "sass:meta";
  2. @mixin syntax-colors($args...) {
  3. @debug meta.keywords($args);
  4. // (string: #080, comment: #800, variable: #60b)
  5. @each $name, $color in meta.keywords($args) {
  6. pre span.stx-#{$name} {
  7. color: $color;
  8. }
  9. }
  10. }
  11. @include syntax-colors(
  12. $string: #080,
  13. $comment: #800,
  14. $variable: #60b,
  15. )
  • meta.mixin-exists($name) or mixin-exists($name) return boolean whether named $name mixin exists
  1. @debug meta.mixin-exists("shadow-none"); // false
  2. @mixin shadow-none {
  3. box-shadow: none;
  4. }
  5. @debug meta.mixin-exists("shadow-none"); // true
  • meta.module-functions($module) return have all function-key-value map in module, the $module must refer using @use rule
  1. // _functions.scss
  2. @function pow($base, $exponent) {
  3. $result: 1;
  4. @for $_ from 1 through $exponent {
  5. $result: $result + $base;
  6. }
  7. @return $result;
  8. }
  9. @use "sass:map";
  10. @use "sass:meta";
  11. @use "functions";
  12. @debug meta.module-functions("functions"); // ("pow": get-function("pow"))
  13. @debug meta.call(map.get(meta.module-variables("functions"), "pow"), 3, 4); // 16
  • meta.module-variables($module) return all variables-key-value-map(without $) defined in a module, and need using @use rule
  1. // _variables.scss
  2. $hopbush: #c69;
  3. $midnight-blue: #036;
  4. $wafer: #e1d7d2;
  5. @use "sass:meta";
  6. @use "variables";
  7. @debug meta.module-variales("variables");
  8. // (
  9. // "hopbush": #c69,
  10. // "midnight-blue": #036,
  11. // "wafer": #e1d7d2,
  12. // )
  • meta.type-of($value) or type-of($value) return the type of $value like these: number, string, color, list, mpa, bool, null, function, arglist, and () may return list or map, depending whether returned by map function
  1. @debug meta.type-of(10px); // number
  2. @debug meta.type-of(10px 20px 30px); // list
  3. @debug meta.type-of(()); // list
  • meta.variale-exists($name) or variable-exists($name) return boolean whether named $name variable existing in current scope
  1. @debug meta.variable-exists("var1); // false
  2. $var1: value;
  3. @debug meta.variable-exists("val1"); // true
  4. h1 {
  5. // $var2 is local
  6. $var2: value;
  7. @debug meta.variable-exists("var2"); // true
  8. }