Sass combine #forward and #use - sass

I'm trying to migrate to the new modular sass, and have two files I want to export from a common utils module:
utils/_functions.scss
#function color($key) {
#return map-get($colors, $key);
}
utils/_variables.scss
$colors: (
primary: 'red',
secondary: 'blue',
);
I'm trying to export it like this:
utils/_index.scss
#forward 'variables';
#forward 'functions';
But I get a compilation error SassError: Undefined variable, because $colors is not defined in function.
I would like to have a way to combine #forward and #use like this:
utils/_functions.scss
#use '../variables'
#function color($key) {
#return map-get(variables.$colors, $key);
}
Is that possible?

Indeed you have to #use the variables file in every file you need it. In this case you needd utils/variables in utils/functions but you do not #use it there.
Additional hint:
There are two additional issuses in your code, - but they do not affect the error in your question:
You should setup variables in modules as !defaults so they can be changed/overwritten when you #use them.
You have an error in your map. You setup the colors as string (= primary: 'red'). The result is that the value will be handeld as string and not as color which leads to a wrong css code.
Following code should work:
//###> file: utils/variables
$colors: (
primary: red,
secondary: blue,
) !default;
//##> file: utils/functions
#use 'variables' as *;
#function color($key) {
#return map-get($colors, $key);
}
//##> file: utils/index
#forward 'variables';
#forward 'functions';
//##> file main
#use 'utils' as *;
.class {
color: color( primary );
}
//---> compiles to css
.class {
color: red;
}

Related

SCSS font-stack mixin error: "argument `$map` of `map-get($map, $key)` must be a map"

I have a problem with this mixin in my project where I'm using gulp-sass:
$base-font-stack: (
sfprodisplay: (
regular: (
family: (SFProDisplay, sans-serif),
weight: 400,
style: normal
),
bold: (
family: (SFProDisplay, sans-serif),
weight: 700,
style: normal
),
),
ubuntu: (
light: (
family: (Ubuntu, sans-serif),
weight: 300,
style: normal
),
regular: (
family: (Ubuntu, sans-serif),
weight: 400,
style: normal
),
)
);
#mixin font($group, $variant: regular, $properties: family weight style, $font-stack: $base-font-stack) {
$font-properties: map-deep-get($font-stack, $group, $variant);
#if $font-properties {
#each $property, $values in $font-properties {
#if contains($properties, $property) {
font-#{$property}: map-get($font-properties, $property);
}
}
}
}
It's not a duplicate question, the problem is:
I checked this in sassmeister and got errors on this lines:
$font-properties: map-deep-get($font-stack, $group, $variant);
font-#{$property}: map-get($font-properties, $property);
and gulp-sass throw error when trying to compile this last line.
I tried your code with Node-Sass and Gulp, as you described.
You seem to be missing the map-deep-get function, so the value of $font-properties is an interpolated string instead of a map.
You need to add the map-deep-get function to your Sass to make it work with node-sass:
#function map-deep-get($map, $keys...) {
#each $key in $keys {
$map: map-get($map, $key);
}
#return $map;
}
I copied the map-deep-get function from this CSSTricks article: https://css-tricks.com/snippets/sass/deep-getset-maps/.
Interestingly, I also tried your code with dart-sass (sass) and the "new" Sass Module System, where you specifically have to import the built-in map module using #use "sass:map". The great thing about the new map module, is that map-get handles deeply nested maps out of the box (https://sass-lang.com/documentation/modules/map), so if you use dart-sass, you can just use the built in map-get, without having to define your own map-deep-get function:
#use "sass:map";
...
$font-properties: map-get($font-stack, $group, $variant);

Why am I getting "Expected digit" when trying to import a Sass variable from Material using Rollup and PostCSS?

I have the following code...
#use "#material/top-app-bar/_variables.import" as mdc;
.color{
background-color: lightblue;
height: mdc.$row-height;
}
But when I run I get...
Error: Expected digit.
src/components/header/index.style.scss 4:17 root stylesheet
I am guessing I am importing the Sass file incorrectly. I have tried...
#use "#material/top-app-bar/variables";
.color{
background-color: lightblue;
height: variables.$row-height;
}
#use "#material/top-app-bar/mdc-top-app-bar";
.color{
background-color: lightblue;
height: $mdc-top-app-bar-row-height;
}
Etc... but nothing I try is working. How do I import the variable using sass?
Update
I noticed this further up
[!] (plugin postcss) Error: Expected digit.
src/components/header/index.style.scss 9:29 root stylesheet
Could this be a Rollup plugin issue?
Update 2
Looks like part of the problem was installing the node-sass package. Now I get...
[!] (plugin postcss) Error: Invalid CSS after "...ight: variables": expected expression (e.g. 1px, bold), was ".$row-height;"
When I run the following...
#use "#material/top-app-bar/variables" as variables;
.color{
background-color: lightblue;
height: variables.$row-height;
}
I also tried the simpler
// ./_variables.scss
$row-height: 64px !default;
#use "./variables";
.color{
background-color: lightblue;
height: variables.$row-height;
}
And I still get the same thing.
I had a similar issue using the sass color module.
I had to add the following to my rollup.config.js
import sassPlugin from 'rollup-plugin-sass'
import sass from 'sass'
sassPlugin({
runtime: sass,
// Other properties omitted for brevity.
})

Foundation 6 custom color palette doesn't work

i'm trying to change the color palette ($foundation-palette) just like the guide from the Global Styles on Zurb Foundation, placing it on base/settings.scss file, but it doesn't seems to be working. here is the code:
// settings.scss
$foundation-palette: (
primary: #FF7212,
secondary: #000000,
success: #3adb76,
warning: #ffae00,
alert: #cc4b37,
);
// app.scss
#import 'base/mixins';
#import 'base/variables';
#import 'base/settings';
#import 'base/color';
#import 'base/reset';
#import 'base/global';
#import 'base/text';
Any help, please, thanks...
All the variables from this Sass map are already defined and are/can be used by calling on a CSS property like:
.thing {
color: $success-color;
}
You can add your own map key value pairs like here I added a tertiary key:
$foundation-palette: (
primary: #5C27AF,
secondary: #3adb76,
tertiary: #009CBF,
success: #3adb76,
warning: #ffae00,
error: #cc4b37,
alert: #E3162F,
);
To call this new map key and get the value:
.thing {
color: get-color(tertiary);
}

gulp not compiling scss

why he does not see the bootstrap class?
If I remove all the #extend works normally
Error: ".navbar" failed to #extend "navbar-light".
The selector "navbar-light" was not found.
Use "#extend navbar-light !optional" if the extend should be able to fail.
on line 11 of app/styles/app.scss
>> #extend navbar-light;
--^
app.scss
does not see #extend bootstrap class, but why? (If I remove all the #extend, bootstrap styles compiling normally)
#import 'bootstrap/scss/bootstrap';
#import 'fotorama/fotorama';
.navbar {
color: red;
#extend navbar-light;
#extend bg-faded;
}
My gulp task
var sassPaths = [
'node_modules'
];
gulp.task('styles', () => (
gulp.src(['app/styles/app.scss'])
.pipe(sass({
includePaths: sassPaths,
errLogToConsole: true,
indentedSyntax: false
}))
.pipe(rename({suffix: '.min', basename: 'app'}))
.pipe(gulp.dest('dist/assets/styles'))
));

sass script variables in a #each directive

I have this snippet of code in my scss file:
$ttk-bg-green: #99FF00;
$ttk-bg-orange: #FFBE00;
$ttk-bg-purple: #CD66FF;
$ttk-bg-blue: #55BBFF;
$ttk-icon-green: #80D500;
$ttk-icon-orange: #FFAF37;
$ttk-icon-purple: #CD66FF;
$ttk-icon-blue: #62C0FF;
$color-types: "bg" "icon";
$color-styles: "green" "orange" "purple" "blue";
#each $color-type in $color-types {
#each $color-style in $color-styles {
.ttk-#{$color-type}-#{$color-style} { color: $ttk-#{$color-type}-#{$color-style}; }
}
}
it's supposed to generate something along the lines of:
.ttk-bg-green: #99FF00;
The compass compiler fails to compile the code, saying $ttk variable does not exist.
it just fails to recognize it as a whole variable.
ideas? solutions?

Resources