Reputation:
I'm trying to dynamically generate my @include and also insert the @content dynamically so I do not have to keep repeating the code ..
However I am getting the following error that is below, and I would like to know what I am doing wrong and if it is possible to do this with includes or I must enter all variable names manually.
$ node-sass scss/_sixbase-grid.scss ../app/src/public/css/sixbase-grid.min.css --output-style expanded
{
"status": 1,
"file": "C:/Users/THIAGOSAAD/Documents/DEVELOPMENT/SIXBASE/PERSONAL PROJECTS/githubcompare/build/scss/_sixbase-grid.scss",
"line": 40,
"column": 12,
"message": "no mixin named media-",
"formatted": "Error: no mixin named media-\n on line 40 of scss/_sixbase-grid.scss\n>> @include media-#{$media-key} {\n\n -----------^\n"
}
error Command failed with exit code 1.
SIXBASE-GRID.SCSS
/*!
* Sixbase Flexbox v1.0.0 (https://sixbase.tech/)
* Copyright 2019 Sixbase.
* Licensed under GNU General Public License v3.0 (https://github.com/sixbase-tech/githubcompare/blob/master/LICENSE)
*/
@import './mixins/media-queries';
$container-map: (
flex: ( display: flex ),
inline: ( display: inline )
);
$flex-direction-map: (
row: ( flex-direction: row ),
row-reverse: ( flex-direction: row-reverse ),
column: ( flex-direction: column ),
column-reverse: ( flex-direction: column-reverse )
);
$media-map: (
smartphone-xs: ( type: 'xs' ),
smartphone-sm: ( type: 'sm' ),
tablet-md: ( type: 'md' ),
tablet-lg: ( type: 'lg' ),
desktop: ( type: 'xl')
);
* {
&::before,
&::after {
margin: 0;
padding: 0;
box-sizing: border-box;
}
}
@each $media-key in $media-map {
@include media-#{$media-key} {
@each $display-key, $display-type in $container-map {
.container-#{map-get($map: $media-key, $key: type )}-#{$display-key} {
display: map-get($map: $display-type, $key: display );
}
}
}
}
MEDIA_QUERIES.SCSS
$xs-width: 320px;
$sm-width: 576px;
$md-width: 768px;
$lg-width: 992px;
$xl-width: 1200px;
@mixin media-smartphone-xs {
@media only screen and (min-width: $xs-width) {
@content;
}
}
@mixin media-smartphone-sm {
@media only screen and (min-width: $sm-width) {
@content;
}
}
@mixin media-tablet-md {
@media only screen and (min-width: $md-width) {
@content;
}
}
@mixin media-tablet-lg {
@media only screen and (min-width: $lg-width) {
@content;
}
}
@mixin media-desktop {
@media only screen and (min-width: $xl-width) {
@content;
}
}
Upvotes: 1
Views: 376
Reputation: 5060
The problem is here: @include media-#{$media-key} {...}
'cause you can't use interpolation in @mixin
. See this post, it is very clear about this issue: How to define a dynamic mixin or function name in SASS?
So, we have to use another way. A solution could be create a general mixin and work with its arguments. Something like this:
@mixin general-media($width){
@media only screen and (min-width: $width) {
@content;
}
}
After that, I choose to add your width values in $media-map
map and use them with that mixin (I tried to use your code):
$media-map: (
smartphone-xs: ( type: 'xs', width: $xs-width ),
smartphone-sm: ( type: 'sm', width: $sm-width ),
tablet-md: ( type: 'md', width: $md-width ),
tablet-lg: ( type: 'lg', width: $lg-width ),
desktop: ( type: 'xl', width: $xl-width )
);
And this is your loop with some changes:
@each $media-key, $media-type in $media-map {
@include general-media(map-get($media-type, 'width')) {
@each $display-key, $display-type in $container-map {
.container-#{map-get($media-type, 'type' )}-#{$display-key} {
display: map-get($map: $display-type, $key: display );
}
}
}
}
This is all code in action:
/* MEDIA_QUERIES.SCSS */
$xs-width: 320px;
$sm-width: 576px;
$md-width: 768px;
$lg-width: 992px;
$xl-width: 1200px;
@mixin general-media($width){
@media only screen and (min-width: $width) {
@content;
}
}
/* SIXBASE-GRID.SCSS */
$container-map: (
flex: ( display: flex ),
inline: ( display: inline )
);
$flex-direction-map: (
row: ( flex-direction: row ),
row-reverse: ( flex-direction: row-reverse ),
column: ( flex-direction: column ),
column-reverse: ( flex-direction: column-reverse )
);
$media-map: (
smartphone-xs: ( type: 'xs', width: $xs-width ),
smartphone-sm: ( type: 'sm', width: $sm-width ),
tablet-md: ( type: 'md', width: $md-width ),
tablet-lg: ( type: 'lg', width: $lg-width ),
desktop: ( type: 'xl', width: $xl-width )
);
* {
&::before,
&::after {
margin: 0;
padding: 0;
box-sizing: border-box;
}
}
@each $media-key, $media-type in $media-map {
@include general-media(map-get($media-type, 'width')) {
@each $display-key, $display-type in $container-map {
.container-#{map-get($media-type, 'type' )}-#{$display-key} {
display: map-get($map: $display-type, $key: display );
}
}
}
}
The output:
*::before, *::after {
margin: 0;
padding: 0;
box-sizing: border-box;
}
@media only screen and (min-width: 320px) {
.container-xs-flex {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}
.container-xs-inline {
display: inline;
}
}
@media only screen and (min-width: 576px) {
.container-sm-flex {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}
.container-sm-inline {
display: inline;
}
}
@media only screen and (min-width: 768px) {
.container-md-flex {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}
.container-md-inline {
display: inline;
}
}
@media only screen and (min-width: 992px) {
.container-lg-flex {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}
.container-lg-inline {
display: inline;
}
}
@media only screen and (min-width: 1200px) {
.container-xl-flex {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}
.container-xl-inline {
display: inline;
}
}
Upvotes: 1