How to insert a selector inside a Mixin? - sass

I'm trying to use in this case a placeholder inside a mixin, however I get the following error:
"message": "You may not #extend an outer selector from within #media.\nYou may only #extend selectors within the same directive.\nFrom \"#extend %shadow-sm\" on line 7 of build/scss/components/cards/_form-register.scss\n",
"formatted": "Error: You may not #extend an outer selector from within #media.\n You may only #extend selectors within the same directive.\n From \"#extend %shadow-sm\" on line 7 of build/scss/components/cards/_form-register.scss\n on line 2 of build/scss/abstracts/placeholders/_shadows.scss\n>> %shadow-sm { box-shadow: 0 .125rem .25rem rgba(0, 0, 0, .075) !important; \n ^\n"
}
Where my Mixin is:
// BOOTSTRAP GRID
#mixin media-breakpoint-xs {
#media (min-width: 320px) {
#content;
}
}
#mixin media-breakpoint-sm {
#media (min-width: 576px) {
#content;
}
}
#mixin media-breakpoint-md {
#media (min-width: 768px) {
#content;
}
}
#mixin media-breakpoint-lg {
#media (min-width: 992px) {
#content;
}
}
#mixin media-breakpoint-xl {
#media (min-width: 1200px) {
#content;
}
}
Where my Placeholder is:
%shadow-none { box-shadow: none !important; }
%shadow-sm { box-shadow: 0 .125rem .25rem rgba(0, 0, 0, .075) !important; }
MY CODE:
#include media-breakpoint-lg {
#extend %shadow-sm;
width: 25rem;
}
Is it possible to do insert a placeholder and or maybe even a mixin inside another mixin?
If not, what would be the best way to use good practices?

Before I had a color %placeholder, now I changed and created a #mixin for the same generation .. Which looked like this:
$default-color-property-type-list: border-color, background-color, color;
$default-color-name-map: (
'cream-1': #FAFAFA,
'cream-2': #EFEFEF,
'correct': #02C22B,
'dark-blue-1': #3897F0,
'dark-grey-2': #999999,
'incorrect': #EE2D3E,
'transparent': transparent,
'white': white,
);
#mixin getColor($color-name, $color-property-type) {
#if not map-has-key($default-color-name-map, $color-name) {
#error 'The color #{$color-name} does not exist!';
} #else if not index($default-color-property-type-list, $color-property-type) {
#error 'The color type #{color-type} does not exist!';
} #else {
$color-value: map-get($default-color-name-map, $color-name);
#{$color-property-type}: #{$color-value} !important;
}
}
Soon to insert the way I was wanting (inside another #mixin), I just need to give an #include in my #mixin of colors this way:
#include media-breakpoint-NAME {
#include getColor($color-name, $color-property-type);
}

Related

SASS mapped property doesn't enter in IF else statement

I'm just pulling my hair right know.
I have a mixing that receives a simple object. Depending on the value of one of the properties should enter inside de IF statement or the ELSE one.
However it's always evaluating as TRUE and can't figure why.
Code is compiled using node-sass (libsass v3.5.4 ) and the gist can be seen here:
https://www.sassmeister.com/gist/40eb28fd55173d7534a7162de3c1b960
Both times calculateHeight mixin is invoked evaluates the if to TRUE and creates a CSS with twice color: red, instead of color: red and color: blue
// ----
// libsass (v3.5.4)
// ----
$headerHeight: 150px;
$headerHeightM: 115px;
$headerHeightS: 100px;
#mixin calculateHeight($obj:()){
#if #{map-get($obj, value)} {
color: red;
#media (min-width: 480px) {
#{map-get($obj, property)}: $headerHeightS;
}
#media (min-width: 768px) {
#{map-get($obj, property)}: $headerHeightM;
}
#media (min-width: 1280px) {
#{map-get($obj, property)}: $headerHeight;
}
}
#else {
color: blue;
#media (min-width: 480px) {
#{map-get($obj, property)}: #{map-get($obj, value)} 233px;
}
}
}
.header{
#include calculateHeight((property: 'height', value: false));
#include calculateHeight((property: "background", value: "url('../img/red_header_wave_1.png') no-repeat center top/100% "));
}
Not sure why, but, if I assign the value of the object property to a variable it does the trick and works. Again, not sure why but it's working now:
// ----
// libsass (v3.5.4)
// ----
$headerHeight: 150px;
$headerHeightM: 115px;
$headerHeightS: 100px;
#mixin calculateHeight($obj:()){
// Assign the mapped property to a variable prior to #if
$var: #{map-get($obj, value)};
#if $var == 'false' {
color: red;
#media (min-width: 480px) {
#{map-get($obj, property)}: $headerHeightS;
}
#media (min-width: 768px) {
#{map-get($obj, property)}: $headerHeightM;
}
#media (min-width: 1280px) {
#{map-get($obj, property)}: $headerHeight;
}
}
#else {
color: blue;
#media (min-width: 480px) {
#{map-get($obj, property)}: #{map-get($obj, value)} 233px;
}
}
}
.header{
#include calculateHeight((property: 'height', value: false));
#include calculateHeight((property: "background", value: "url('../img/red_header_wave_1.png') no-repeat center top/100% "));
}
You are interpolating the value from your map into a string, so you are actually checking the string "false", which is truthy (as you already found out). Get rid of the interpolation #{...} and it should work as expected.
- #if #{map-get($obj, value)} {
+ #if map-get($obj, value) {

SASS: Get value of variables after returning a string from list

Is there a way I can get the value of nth($list , 1)
$xs: 600px;
$sm: 960px;
$md: 1280px;
$lg: 1920px;
$list: xs, sm, md, lg;
#mixin respond($media) {
#for $i from 1 through length($list) {
#if $media==nth($list, 1) {
#media only screen and (max-width: nth($list, 1) - 1px) {
#content;
}
}
}
}
I'm getting this now
#media only screen and (max-width: xs-1px) {}
I want the value of the variable $xs = 600px;
#media only screen and (max-width: 600px - 1px) {}
I tried:
$nth($scree-size , 1)
$#{nth($scree-size , 1)}
The short answer is: no, as what you are looking for are dynamic variable names and they are not well supported. However, I suggest to use a map instead to have a somewhat cleaner code without the need to repeat the variable names and to simplify your mixin in general:
$breakpoints: (
xs: 600px,
sm: 960px,
md: 1280px,
lg: 1920px
);
#mixin respond($media) {
#if map-has-key($breakpoints, $media) {
#media only screen and (max-width: map-get($breakpoints, $media) - 1px) {
#content;
}
}
}
#include respond("md") {
.class {
color: red;
}
}
outputs:
#media only screen and (max-width: 1279px) {
.class {
color: red;
}
}

Conditionally negating a media query in Sass

I have the following mixin (source):
#mixin media($queries) {
#if length($queries) == 0 {
#content;
} #else {
$first-key: nth(map-keys($queries), 1);
#media ($first-key: map-get($queries, $first-key)) {
$queries: map-remove($queries, $first-key);
#include media($queries) {
#content;
}
}
}
}
I would like to use a condition to be able to negate the media query, like so:
#media not screen and ($first-key: map-get($queries, $first-key)) {
What is the correct syntax to add it dynamically? I tried the following without success:
$invert: true;
$foo: if($invert, 'not screen and', null);
#media #{$foo} ($first-key: map-get($queries, $first-key)) {
Error:
Invalid CSS after "...#media #{$foo} ": expected "{", was "($first-key: ma..."
The iterated queries could look like this:
tablet: (
respond-to: (min-width: 421px, max-width: 992px)
)
Which would result in the following css when used:
#media (min-width: 421px) and (max-width: 992px) { }
I don't have an explanation as to why what you have doesn't work (this issue claims that interpolation is done before the media query is parsed).
It looks like you'll need to move the and outside of the variable and into the media query itself:
#mixin media($queries, $invert: false) {
#if length($queries) == 0 {
#content;
} #else {
$first-key: nth(map-keys($queries), 1);
$foo: if($invert, 'not', '') screen;
#media #{$foo} and ($first-key: map-get($queries, $first-key)) {
$queries: map-remove($queries, $first-key);
#include media($queries, $invert) {
#content;
}
}
}
}
Output:
#media not screen and (min-width: 30em) and (max-width: 50em) {
.foo {
color: red;
}
}
#media (min-width: 30em) {
.foo {
color: green;
}
}
And yes, you need to apply the not each time you nest it otherwise Sass won't merge the media query (you can't merge a not screen with a (min-width: 30em) because they're exclusive from each other).

Undefined operation: "500px gte medium-screens"

I'm trying to combine Jake Archibald's mobile-friendly IE sass mixins... http://jakearchibald.github.io/sass-ie ...with the responsive mixins illustrated by Dan Cederholm in his book 'Sass for Web Designers'. I'm using CodeKit to compile the files (detailed below).
all.scss compiles successfully, but all-old-ie.scss fails. CodeKit says:
Error: Undefined operation: "500px gte medium-screens".
on line 8 of /Applications/MAMP/htdocs/sass-ie-test/sass/_utils.scss, in `responsive'
from line 4 of /Applications/MAMP/htdocs/sass-ie-test/sass/_layout.scss
from line 2 of /Applications/MAMP/htdocs/sass-ie-test/sass/all.scss
from line 3 of /Applications/MAMP/htdocs/sass-ie-test/sass/all-old-ie.scss
Use --trace for backtrace.
Not sure what's going on here. My files are:
_utils.scss
$medium: 600px;
$large: 950px;
$fix-mqs: false !default;
#mixin responsive($width) {
#if $fix-mqs {
#if $fix-mqs >= $width {
#content;
}
}
#else if $width == medium-screens {
#media only screen and (min-width: $medium) {
#content;
}
}
#else if $width == large-screens {
#media only screen and (min-width: $large) {
#content;
}
}
}
$old-ie: false !default;
#mixin old-ie {
#if $old-ie {
#content;
}
}
_layout.scss
article {
padding: 30px;
#include responsive(medium-screens) {
padding: 50px;
}
#include responsive(large-screens) {
padding: 70px;
}
#include old-ie {
//stuff for ie
}
}
all.scss
#import 'utils';
#import 'layout';
all-old-ie.scss
$old-ie: true;
$fix-mqs: 500px;
#import 'all';
The error is quite clear here. It explicitly states that it cannot possibly determine whether or not the number 500px is greater than or equal (gte) to the string medium-screens.
You're trying to overcomplicate things for no reason: just use variables.
$medium: 600px;
$large: 950px;
#mixin responsive($width) {
#if $fix-mqs {
#if $fix-mqs >= $width {
#content;
}
} #else {
#media only screen and (min-width: $width) {
#content;
}
}
}
article {
padding: 30px;
#include responsive($medium) {
padding: 50px;
}
#include responsive($large) {
padding: 70px;
}
#include old-ie {
//stuff for ie
}
}

Sass: Storing #content into list for use later in another mixin?

I am trying to store the #content directive from one mixin into a list so that I can use it later in another mixin. I am getting errors when I try this. Anyone know if this is possible?
Example is a simplified version of what I am doing, but basically I want to create custom breakpoint classes but without having all of the extra markup from the media query on every class.
Heres what I've got:
$breakpoints: ((sm, 320px), (md, 780px), (lg, 960px));
$all: ();
#mixin push($name) {
$content: (#content);
$all: append($all, ($name, $content));
}
#mixin printAll() {
#each $breakpoint in $breakpoints {
$breakpointName: nth($breakpoint, 1);
$breakpointSize: nth($breakpoint, 2);
#media only screen and (min-width: $breakpointSize) {
#each $item in $all {
$className: nth($item, 1);
$content: nth($item, 2);
#{$className}-#{$breakpointName} {
#{$content};
}
}
}
}
}
#include push(color-red){ color: red; }
#include push(color-green){ color: green; }
#include push(color-blue){ color: blue; }
#include push(totally-crazy){
color: red;
background-color: green;
border-top: solid 5px brown;
border-left: solid 5px gray;
border-right: solid 5px blue;
border-bottom: solid 5px yellow;
}
#include printAll();
So basically I want to output something like:
#media only screen and (min-width: 320px) {
//all custom classes with -sm suffix here
}
#media only screen and (min-width: 728px) {
//all custom classes with -md suffix here
}
#media only screen and (min-width: 960px) {
//all custom classes with -lg suffix here
}
INSTEAD of:
#media only screen and (min-width: 320px) {
//first pushed class with -sm suffix here
}
#media only screen and (min-width: 728px) {
//first pushed class with -md suffix here
}
#media only screen and (min-width: 960px) {
//first pushed class with -lg suffix here
}
#media only screen and (min-width: 320px) {
//second pushed class with -sm suffix here
}
#media only screen and (min-width: 728px) {
//second pushed class with -md suffix here
}
#media only screen and (min-width: 960px) {
//second pushed class with -lg suffix here
}
etc..
Example here:
http://sassmeister.com/gist/10332360

Resources