转发变量的 SASS 映射
首先我要说的是我不确定这是否可能。我仍在进一步了解 SASS 的深度。
本质上,我想做的只是转发 sass 模块的一部分。我知道我可以使用 show
转发模块的部分内容,但我想要转发的内容有很多,而且列表可能会很长。我尝试转发的模块上有一个地图,其中包含我尝试转发的所有变量,因此我尝试转发该地图,但它似乎不起作用。
具体情况是,我在元素上使用 ShadowDOM(这意味着不应用全局样式),并使用 bootstrap (v5) 作为我的样式框架。我希望引导程序文件中的特定变量可供自定义元素使用,因此我创建了一个名为 variables.scss
的模块,它尝试仅转发 $theme-colors
来自 bootstrap 的变量。 $theme-colors
是一个包含所有变量的地图,因此在我看来,我应该能够通过引用该地图来仅显示这些变量,但可惜的是,它不起作用。
以下是一些片段:
//bootstrap.scss
$primary: $blue !default;
$secondary: $gray-600 !default;
$success: $green !default;
$info: $cyan !default;
$warning: $yellow !default;
$danger: $red !default;
$light: $gray-100 !default;
$dark: $gray-900 !default;
$theme-colors: (
"primary": $primary,
"secondary": $secondary,
"success": $success,
"info": $info,
"warning": $warning,
"danger": $danger,
"light": $light,
"dark": $dark
) !default;
//variables.scss
@forward './bootstrap' show $theme-colors;
//module.scss
@use './variables';
.some-style {
color: variables.$light;
}
Sass 输出:
消息原始:未定义的变量。
颜色:变量.$light
希望这已经足够详细了。有没有办法在转发 scss 模块时使用地图仅显示地图的内容?
I'll start of by saying I'm not sure if this is possible or not. I'm still learning more about the depths that SASS goes.
Essentially, what I'm trying to do is forward just parts of a sass module. I know I can forward parts of a module with show
, but there are multiple things I'd like to forward, and the list can get lengthy. There's a map on the module I'm trying to forward that contains all the variables I'm trying to forward, and so I've tried to forward the map, but it doesn't seem to work.
Specific case, I'm using ShadowDOM on my elements (which means global styles aren't applied), and bootstrap (v5) as my style framework. There are specific variables inside the bootstrap file that I want to have available to my custom elements, so I've created a module called variables.scss
that tries to forward just the $theme-colors
variables from bootstrap. $theme-colors
is a map that contains all the variables, so in my head I should be able to show just those variables by referencing the map, but alas, it does not work.
Here are some snippets:
//bootstrap.scss
$primary: $blue !default;
$secondary: $gray-600 !default;
$success: $green !default;
$info: $cyan !default;
$warning: $yellow !default;
$danger: $red !default;
$light: $gray-100 !default;
$dark: $gray-900 !default;
$theme-colors: (
"primary": $primary,
"secondary": $secondary,
"success": $success,
"info": $info,
"warning": $warning,
"danger": $danger,
"light": $light,
"dark": $dark
) !default;
//variables.scss
@forward './bootstrap' show $theme-colors;
//module.scss
@use './variables';
.some-style {
color: variables.$light;
}
Sass output:
messageOriginal: Undefined variable.
color: variables.$light
Hopefully that's enough details. Is there a way I can use a map to show just the contents of the map when forwarding scss modules?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我将 @debug meta.module-variables('variables'); 放入
module.scss
中时,它输出以下内容(您的值会略有不同,因为我必须填写在缺少的变量空白中):这提醒我
$theme-colors
是一个 Sass 映射,在这种情况下,您将像这样检索“light”值:When I placed
@debug meta.module-variables('variables');
intomodule.scss
it outputted the following (your values will be slightly different as I had to fill in the missing variable blanks):That reminded me that
$theme-colors
was a Sass map, in which case you would retrieve the "light" value like so: