admin管理员组文章数量:1122832
I have an SCSS map for my theme colours that looks like this:
$theme-colours: (
dark: (
primary: (
light: #32303b,
base: #32303b,
dark: #32303b,
),
secondary: (
light: #201f26,
base: #201f26,
dark: #201f26,
),
),
light: (
primary: (
light: #32303b,
base: #32303b,
dark: #32303b,
),
secondary: (
light: #201f26,
base: #201f26,
dark: #201f26,
),
),
);
I have a @mixin
that I am calling in my scss file:
@mixin generateCssThemeVariables($themeName) {
@each $theme, $colourType in $theme-colours {
@if $theme == $themeName {
@each $variation, $value in $colourType {
--#{$colourType}-colour-#{$variation}: #{$value};
}
}
}
}
When I call @include generateCssThemeVariables("dark");
I want the output of the mixin to look something like this:
--primary-colour-light: #32303b;
--primary-colour-base: #32303b;
--primary-colour-dark: #32303b;
--secondary-colour-light: #201f26;
--secondary-colour-base: #201f26;
--secondary-colour-dark: #201f26;
At the moment I'm getting the following error from my mixin:
(primary: light: #32303b, base: #32303b, dark: #32303b),
secondary: (light: #201f26, base: #201f26, dark: #201f26))
isn't a valid CSS value.
╷
│ --#{$colourType}-colour-#{$variation}: #{$value};
│ ^^^^^^^^^^^
╵
As you can probably guess I'm fairly new to SASS. I would guess that the problem is with the way I am using the @mixin
and the scope of the variables in the @each
statements. Can anyone can shed some light on to what is going wrong, is there something wrong with the way I have written the mixin?
I have an SCSS map for my theme colours that looks like this:
$theme-colours: (
dark: (
primary: (
light: #32303b,
base: #32303b,
dark: #32303b,
),
secondary: (
light: #201f26,
base: #201f26,
dark: #201f26,
),
),
light: (
primary: (
light: #32303b,
base: #32303b,
dark: #32303b,
),
secondary: (
light: #201f26,
base: #201f26,
dark: #201f26,
),
),
);
I have a @mixin
that I am calling in my scss file:
@mixin generateCssThemeVariables($themeName) {
@each $theme, $colourType in $theme-colours {
@if $theme == $themeName {
@each $variation, $value in $colourType {
--#{$colourType}-colour-#{$variation}: #{$value};
}
}
}
}
When I call @include generateCssThemeVariables("dark");
I want the output of the mixin to look something like this:
--primary-colour-light: #32303b;
--primary-colour-base: #32303b;
--primary-colour-dark: #32303b;
--secondary-colour-light: #201f26;
--secondary-colour-base: #201f26;
--secondary-colour-dark: #201f26;
At the moment I'm getting the following error from my mixin:
(primary: light: #32303b, base: #32303b, dark: #32303b),
secondary: (light: #201f26, base: #201f26, dark: #201f26))
isn't a valid CSS value.
╷
│ --#{$colourType}-colour-#{$variation}: #{$value};
│ ^^^^^^^^^^^
╵
As you can probably guess I'm fairly new to SASS. I would guess that the problem is with the way I am using the @mixin
and the scope of the variables in the @each
statements. Can anyone can shed some light on to what is going wrong, is there something wrong with the way I have written the mixin?
2 Answers
Reset to default 0You are trying to iterate over your nested map structure and the misunderstanding of how variables are being used within your loops. Your $theme-colours map is a nested structure, and you need to adjust your loop to navigate this nesting correctly so, try this solution:
$theme-colours: (
dark: (
primary: (
light: #32303b,
base: #32303b,
dark: #32303b,
),
secondary: (
light: #201f26,
base: #201f26,
dark: #201f26,
),
),
light: (
primary: (
light: #32303b,
base: #32303b,
dark: #32303b,
),
secondary: (
light: #201f26,
base: #201f26,
dark: #201f26,
),
),
);
@mixin generateCssThemeVariables($themeName) {
@each $theme, $types in $theme-colours {
@if $theme == $themeName {
@each $typeName, $colours in $types {
@each $variation, $value in $colours {
--#{$typeName}-colour-#{$variation}: #{$value};
}
}
}
}
}
@include generateCssThemeVariables(dark);
Okay I have solved this now. The working @mixin
looks like this:
@mixin generateCssThemeVariables($themeName) {
@each $themeArray, $colourTypeArray in $theme-colours {
@if $themeArray == $themeName {
@each $colourType, $variationArray in $colourTypeArray {
@each $variation, $value in $variationArray {
--#{$colourType}-colour-#{$variation}: #{$value};
}
}
}
}
}
本文标签: sassSCSS mixin to generate css colour variablesStack Overflow
版权声明:本文标题:sass - SCSS @mixin to generate css colour variables - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736304107a1932118.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$colourType
is a map list and not a value? – Dandelion Commented Nov 22, 2024 at 11:51$colourType
from the css variable it throws the same error about$value
! What am I doing wrong? – Dandelion Commented Nov 22, 2024 at 12:01