So a while back I found this post and particularity the new way mentioned here: Initialize Sass variables from Hugo templates
So I have a very similar setup as mentioned in that thread, in my html I have the following bit to render the SCSS with dartsass:
{{ $options := (dict "targetPath" "css/style.css"
"vars" site.Params.style
"transpiler" "dartsass"
"outputStyle" "compressed"
"enableSourceMap" (not hugo.IsProduction)
"includePaths" (slice "assets/scss/lib")) }}
{{ $style := resources.Get "scss/main.scss" | resources.ToCSS $options | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $style.Permalink }}" integrity="{{ $style.Data.Integrity }}" media="screen" />
Then in my scss file:
@mixin my-light-theme() {
--background-color: #{h.$light-background-color};
--foreground-color: #{h.$light-foreground-color};
}
And this works very well and good with the following config:
params:
style:
light-background-color: '#ffffff'
light-foreground-color: '#000000'
Here comes the first twist on what I’m trying to do
Here comes the twist, sometimes I want to run a function within the css on the variable that I create.
So instead of the following:
@mixin my-light-theme() {
--background-color: darken(#{h.$light-background-color}, 5%);
--foreground-color: #{h.$light-foreground-color};
}
I just do this instead:
params:
style:
light-background-color: 'darken(#ffffff, 5%)'
light-foreground-color: '#000000'
However, it doesn’t work with custom functions
In my main.scss
file I have defined the line
@import "functions";
That imports the following file that contains a function called contrast-color
that does what it says, it automatically calculates contrast colors for the foreground to be readable against the background: https://github.com/etu/etu.github.io/blob/main/src/scss/functions.scss#L53-L77
However, it seems like my contrast-color
function isn’t evaluated when passing it through from a variable. My only guess is that it’s something that happens when hugo sends off the variables from yaml to scss so it evaluates functions it knows that are built in but not functions that are included.
Is there some way to accomplish this or do I have to find some other way?