转发变量的 SASS 映射

发布于 2025-01-11 14:51:03 字数 1440 浏览 0 评论 0原文

首先我要说的是我不确定这是否可能。我仍在进一步了解 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淑女气质 2025-01-18 14:51:03

当我将 @debug meta.module-variables('variables'); 放入 module.scss 中时,它输出以下内容(您的值会略有不同,因为我必须填写在缺少的变量空白中):

module.scss:5 Debug: ("theme-colors": ("primary": "blue", "secondary": "gray", "success": "green", "info": "cyan", "warning": "yellow", "danger": "red", "light": "gray", "dark": "gray"))

这提醒我 $theme-colors 是一个 Sass 映射,在这种情况下,您将像这样检索“light”值:

.some-style {
   color: map.get(variables.$theme-colors, 'light');
}

When I placed @debug meta.module-variables('variables'); into module.scss it outputted the following (your values will be slightly different as I had to fill in the missing variable blanks):

module.scss:5 Debug: ("theme-colors": ("primary": "blue", "secondary": "gray", "success": "green", "info": "cyan", "warning": "yellow", "danger": "red", "light": "gray", "dark": "gray"))

That reminded me that $theme-colors was a Sass map, in which case you would retrieve the "light" value like so:

.some-style {
   color: map.get(variables.$theme-colors, 'light');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文