mixin is not found in different scss file - css

I am learning Angular and scss as well. So in my test Angular v.5 Project currently facing problems with #mixin. Note that default.styleExt also set to 'scss'. #mixin works fine if I use #mixin and #include in the same *.scss file. But When I try to use #include of that #mixin in a different *.scss file. It shows errors.
For example, in my styles.scss has this mixin,
#mixin breakpoint($screen){
#if $screen == window_xLarge {
#media (min-width: 1440px){
#content;
}
}
#else if $screen == window_large {
#media (min-width: 1280px){
#content;
}
}
#else if $screen == window_medium {
#media (min-width: 840px){
#content;
}
}
#else if $screen == window_small {
#media (min-width: 600px){
#content;
}
}
#else if $screen == window_xSmall {
#media (min-width: 480px){
#content;
}
}
}
Now, when I try to use this #mixin in one of my other components' '*.scss', for example,
in comp01.scss try to add this code
app-comp02{
width: 100%;
#include breakpoint(window_small){
width: 50%
}
#include breakpoint(window_medium){
width: 50%;
}
#include breakpoint(window_large){
width: 33%;
}
height: auto;
margin: 0 5px;
}
It shows error like this
Module build failed:
#include breakpoint(window_small){
^
No mixin named breakpoint
Does anyone know, why this is happening? I definitely do not want to put all codes in the same scss file. Thanks in Advance.

Keep your mixins in a _helpers.scss file and then include that file in both your different scss like this
#import "/helpers"; // Path of scss
Or you can simply import "styles.scss" in the "comp01.scss" using the same syntax as mentioned above

You have to correct your typing you have an error because you should write the media query like that and this is a good site:sassmeister to test your sass/scss you can paste your code and you will see the errors
#mixin breakpoint($screen){
#if $screen == window_xLarge {
#media screen and (min-width: 1440px){
#content;
}
}
#else if $screen == window_large {
#media screen and (min-width: 1280px){
#content;
}
}
#else if $screen == window_medium {
#media screen and (min-width: 840px){
#content;
}
}
#else if $screen == window_small {
#media screen and (min-width: 600px){
#content;
}
}
#else if $screen == window_xSmall {
#media screen and (min-width: 480px){
#content;
}
}
}
app-comp02{
width: 100%;
#include breakpoint(window_small){
width: 50%
}
#include breakpoint(window_medium){
width: 50%;
}
#include breakpoint(window_large){
width: 33%;
}
height: auto;
margin: 0 5px;
}
and you wrote it like that
#media (min-width: 1280px){
#content;
}

Related

\# symbol on media queries and how is it interpreted

I'm currently learning SASS and I've come across the '\#' symbol on a utility map example as shown below:
#each $name, $breakpoint in $breakpoints {
#media(min-width: $breakpoint) {
**.text-size-#{$size}\##{$name}** {
font-size: #{$size}px;
}
}}
which creates font-sizes for various breakpoints and compiles to something like this:
...
#media (min-width: 412px) {
.text-size-12\#small {
font-size: 12px;
}
}
#media (min-width: 768px) {
.text-size-12\#medium {
font-size: 12px;
}
}
#media (min-width: 1024px) {
.text-size-12\#large {
font-size: 12px;
}
}
...
What does the '\#' symbol translate to in this case? Am I correct in assuming these classes will be invoked as class="text-size-12 small"?

PostCSS compiling SASS media queries out of order?

I'm using a simple webpack build with PostCSS. If I don't declare a specific media query mixin in the first section of the page's SCSS, but use that specific media query mixin in a following section, that media query outputs after the other breakpoints (even though I have them defined in a specific order in my mixins file). Here's my code:
_variables.scss
$sm-screen: 576px;
$md-screen: 768px;
$lg-screen: 992px;
$xl-screen: 1200px;
_mixins.scss
#mixin sm-screen {
#media screen and (min-width: #{$sm-screen}) {
#content;
}
}
#mixin md-screen {
#media screen and (min-width: #{$md-screen}) {
#content;
}
}
#mixin lg-screen {
#media screen and (min-width: #{$lg-screen}) {
#content;
}
}
#mixin xl-screen {
#media screen and (min-width: #{$xl-screen}) {
#content;
}
}
#mixin screen-size($screen) {
#media screen and (min-width: $screen) {
#content;
}
}
_page.scss
// ————————————————————————————————————————————————————————————
// HERO SECTION
// ————————————————————————————————————————————————————————————
#hero-section {
.hero-heading {
font-size: 2rem;
}
#include sm-screen {
.hero-heading {
font-size: 5rem;
}
}
#include lg-screen {
.hero-heading {
font-size: 6.5rem;
}
}
#include xl-screen {
.hero-heading {
font-size: 6.5rem;
}
}
}
// ————————————————————————————————————————————————————————————
// INTRO SECTION
// ————————————————————————————————————————————————————————————
#intro-section {
.icon-group {
background: red;
}
#include md-screen {
.icon-group {
background: yellow;
}
}
#include xl-screen {
.icon-group {
background: blue;
}
}
}
main.scss (importing files)
#import "/base/variables";
#import "/base/mixins";
#import "page";
As you can see in _page.scss, I like to declare breakpoints right within each section (or row) as I go down the page. The problem is since I didn't declare the #include md-screen breakpoint on the first section, HERO SECTION, it comes after the #include xl-screen breakpoint in the output CSS and overrides the styles of the largest breakpoint:
output CSS:
body.home #intro-section .icon-group {
background: red;
}
#media screen and (min-width:1200px) {
body.home #intro-section .icon-group {
background: blue;
}
}
#media screen and (min-width:768px) {
body.home #intro-section .icon-group {
background: yellow;
}
}
Does this mean that my practice of declaring the breakpoints on each section is not the best way? Should I declare all breakpoints at the end of each file, that way if nothing changes in one section at a specific breakpoint, I don't need to declare that breakpoint at all to get it to compile in the correct order? Is there a way to get media queries to output in the order they're declared in the mixins file, regardless of when the mixins are called in the CSS?
My intent is to get the breakpoints to output from smallest to largest since I'm using the mobile-first, min-width approach. I would love to apply the media queries like I'm doing, but if there's a better way (such as at the end of files), so be it.

My Media queries are not changing as wanted

Currently I'm trying to change the padding of my landing page text, so it pushes my smooth scroll button to the bottom of the page. However the queries work up until (min-width: 600px), and they dont change higher.
Mixins Partial
#mixin for-phone-only {
#media (max-width: 599px) {
#content;
}
}
#mixin for-tablet-portrait {
#media (min-width: 600px) {
#content;
}
}
#mixin for-tablet-landscape {
#media (min-width: 899px) {
#content;
}
}
#mixin for-desktop-up {
#media (min-width: 1200px) {
#content;
}
}
Landing Page Partial
.welcomeTitle {
font-family: $landing-font;
font-size: $landing-header-size-l;
text-shadow: 1px 1px 15px #000;
padding-top: 20%;
#include for-phone-only {
font-size: $landing-header-size-sm;
}
}
.welcomeText {
#include for-desktop-up {
padding: 0 20% 21.5% 20%;
font-size: $landing-text-size-l;
}
#include for-tablet-landscape {
padding: 0 20% 20% 20%;
}
#include for-tablet-portrait {
padding: 0 20% 60% 20%;
}
#include for-phone-only {
padding: 0 20% 50% 20%;
}
}
So it applies the tablet portrait query, but when the width goes above 899px, it dose not apply the tablet landscape, or when it goes above 1200px, it dosent apply the desktop query.
Thanks :D

How to create Sass mixin aliases with support for content blocks?

How can I define multiple names for the same mixin, with support for content blocks?
Definition
#mixin desktop-breakpoint {
#media only screen and (min-width: 769px) {
#content;
}
}
#mixin large-breakpoint {
#include desktop-breakpoint;
}
Usage
.my-class {
font-size: small;
#include desktop-breakpoint {
font-size: big;
}
}
.my-other-class {
color: red;
#include large-breakpoint {
color: blue;
}
}
Error message
Mixin "large-breakpoint" does not accept a content block.
You're not passing any #content when using #include desktop-breakpoint in your large-breakpoint mixin. Doing this will fix your compilation error:
#mixin large-breakpoint {
// Remember to pass content received by mixin to #include
#include desktop-breakpoint {
#content;
}
}
Then your CSS will be compiled properly, as intended:
.my-class {
font-size: small;
}
#media only screen and (min-width: 769px) {
.my-class {
font-size: big;
}
}
.my-other-class {
color: red;
}
#media only screen and (min-width: 769px) {
.my-other-class {
color: blue;
}
}
See proof-of-concept example based on your modified code: https://www.sassmeister.com/gist/3109af060293eed0b89a22c27fa20527

SASS media queries best practise?

Is it ok to nest media queries inside an element? If I want to use min-width: 480px in another places there will be huge repetition. Please look at my code example. Or just use the old way? Any idea?
SASS
.navbar {
height: 500px;
width: 500px;
#media screen and (min-width: 480px) {
background-color: lightgreen;
}
}
.items {
padding: 15px;
color: red;
#media screen and (min-width: 480px) {
border: 1px solid black;
}
}
CSS
#media screen and (min-width: 480px) {
.navbar {
background-color: lightgreen;
}
.items {
border: 1px solid black;
}
}
$pc: 1024px; // PC screen size.
$tablet: 720px; // Tablet screen size.
$phone: 320px; // Phone screen size.
#mixin responsive($media) {
#if $media= phone {
#media only screen and (max-width: $tablet - 1) {
#content;
}
}
#else if $media= tablet {
#media only screen and (min-width: $tablet - 1) and (max-width: $pc) {
#content;
}
}
#else if $media= pc {
#media only screen and (min-width: $pc + 1) and (min-width: $pc) {
#content;
}
}
#else if $media= pc_tablet {
#media only screen and (min-width: $tablet - 1) {
#content;
}
}
}
Examples
body {
#include responsive(pc) {
background: red;
}
#include responsive(tablet) {
background: yellow;
}
#include responsive(phone) {
background: green;
}
}

Resources