简体   繁体   English

使用 SASS/SCSS 生成 CSS 变量

[英]Using SASS/SCSS to generate CSS variables

I'm currently using SCSS to attempt to recreate a CSS block.我目前正在使用 SCSS 尝试重新创建 CSS 块。 The CSS block is as follows: CSS 块如下:

:root {
    --Franklin-Blue: #1d1c4d;
    --Light-Blue: #4e5d94;
    --Pale-Blue: #7289da;
    --Pure-White: #ffffff;
    --VLight-Grey: #99aab5;
    --Middle-Grey: #2c2f33;
    --Dark-Grey: #23272a;
    --body-color: #ffffff; 
    --text-color: #000000; 
    --bold-text: #000000; 
    --White-Blue: #ffffff; 
}

[data-theme="dark"] {
    --Franklin-Blue: #000000;
    --Light-Blue: #0d0d0d;
    --Pale-Blue: #2c2f33;
    --Pure-White: #ffffff;
    --VLight-Grey: #99aab5;
    --Middle-Grey: #2c2f33;
    --Dark-Grey: #23272a;
    --body-color: #000000;
    --text-color: #ffffff;
    --bold-text: #7289da;
    --White-Blue: #7289da;
}

So, I've started off by creating this SCSS map:所以,我从创建这个 SCSS map 开始:

$themes: (
    dark: (
        'Franklin-Blue': #000000, 
        'Light-Blue': #0d0d0d,
        'Pale-Blue': #2c2f33,
        'Pure-White': #ffffff,
        'VLight-Grey': #99aab5,
        'Middle-Grey': #2c2f33,
        'Dark-Grey': #23272a, 
        'body-color': #000000,
        'text-color': #ffffff,
        'bold-text': #7289da,
        'White-Blue': #7289da
    ), 
    default:( 
        'Franklin-Blue': #1d1c4d, 
        'Light-Blue': #4e5d94,
        'Pale-Blue': #7289da, 
        'Pure-White': #ffffff,
        'VLight-Grey': #99aab5,
        'Middle-Grey': #2c2f33,
        'Dark-Grey': #23272a,
        'body-color': #ffffff,
        'text-color': #000000,
        'bold-text': #000000,
        'White-Blue': #ffffff 
    )
);

I've then created this mixin to cycle through that map and create the CSS variables from it:然后我创建了这个 mixin 来循环遍历 map 并从中创建 CSS 变量:

@mixin theme() {
    @each $theme, $map in $themes {
        @if $theme == "default" {
            :root {
                @each $key, $value in $map {
                    #{--$key}: $value;
                }
            }
        }
        @else {
            [data-theme="#{$theme}" ] {
                @each $key, $value in $map {
                    #{--$key}: $value;
                }
            }
        }
    }
}

@include theme();

I can tell I'm along the right lines, but the CSS it creates is ever so slightly off - I end up with this:我可以说我的思路是正确的,但是它创建的 CSS 有点偏离 - 我最终得到了这个:

[data-theme=dark] {
  -- Franklin-Blue: #000000;
  -- Light-Blue: #0d0d0d;
  -- Pale-Blue: #2c2f33;
  -- Pure-White: #ffffff;
  -- VLight-Grey: #99aab5;
  -- Middle-Grey: #2c2f33;
  -- Dark-Grey: #23272a;
  -- body-color: #000000;
  -- text-color: #ffffff;
  -- bold-text: #7289da;
  -- White-Blue: #7289da;
}

:root {
  -- Franklin-Blue: #1d1c4d;
  -- Light-Blue: #4e5d94;
  -- Pale-Blue: #7289da;
  -- Pure-White: #ffffff;
  -- VLight-Grey: #99aab5;
  -- Middle-Grey: #2c2f33;
  -- Dark-Grey: #23272a;
  -- body-color: #ffffff;
  -- text-color: #000000;
  -- bold-text: #000000;
  -- White-Blue: #ffffff;
}

How do I make sure the quotes appear in that attribute selector, as well as removing the whitespace from the CSS variables?如何确保引号出现在该属性选择器中,以及从 CSS 变量中删除空格?

I know I need to move the default above the other theme to make the :root appear first, but I'm not sure how to fine tune the formatting.我知道我需要将默认设置移到其他主题上方以使:root首先出现,但我不确定如何微调格式。

To give a general overview, the syntax #{…} is called interpolation and it is the only way to inject dynamic values into a CSS variable (custom property).概括地说,语法#{…}称为插值,它是将动态值注入 CSS 变量(自定义属性)的唯一方法。 Here is a quote from the doc :这是来自文档的引用:

CSS custom properties, also known as CSS variables, have an unusual declaration syntax: they allow almost any text at all in their declaration values. CSS 自定义属性,也称为 CSS 变量,具有不寻常的声明语法:它们允许在其声明值中包含几乎任何文本。 What's more, those values are accessible to JavaScript, so any value might potentially be relevant to the user.此外,JavaScript 可以访问这些值,因此任何值都可能与用户相关。 This includes values that would normally be parsed as SassScript.这包括通常会被解析为 SassScript 的值。

Because of this, Sass parses custom property declarations differently than other property declarations.因此,Sass 解析自定义属性声明的方式与其他属性声明不同。 All tokens, including those that look like SassScript, are passed through to CSS as-is.所有令牌,包括那些看起来像 SassScript 的令牌,都按原样传递给 CSS。 The only exception is interpolation , which is the only way to inject dynamic values into a custom property.唯一的例外是interpolation ,这是将动态值注入自定义属性的唯一方法。

$primary: red;
:root {
  --primary: #{$primary}; // this one works
  --primary: $primary;    // this does not
}

The error you made is to put the -- inside the interpolation curly brackets ( {} ).您犯的错误是将--放在插值大括号( {} )内。 This would work:这会起作用:

@mixin theme() {
  @each $theme, $map in $themes {
    @if $theme == "default" {
      :root {
        @each $key, $value in $map {
          --#{$key}: #{$value};
        }
      }
    } @else {
      [data-theme="#{$theme}"] {
        @each $key, $value in $map {
          --#{$key}: #{$value};
        }
      }
    }
  }
}

@include theme();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM