0

I have a util scss file for my breakpoints to reuse them through my Angular project:

$breakpoints: (
    sm: 768px,
    md: 1024px,
    lg: 1280px,
    xl: 1400px
);

$bp-sm: get-bp('sm');
$bp-md: get-bp('md');
$bp-lg: get-bp('lg');
$bp-xl: get-bp('xl');

@function get-bp($bp) {
    @if $bp {
        $bp: map-get($breakpoints, $bp);
    } @else {
        @error "Parameter #{$bp} is unknown or empty.";
    }

    @return $bp;
}

I have a list of breakpoints. I need the function for another util file, where I loop through the list and render media queries for each breakpoint. The scss variables are used for hardcoded media queries in an Angular component styling. So I try to set the value of the variable by also calling the function, so I avoid redundancy for the values and use the same list.

.logo {
    width: 250px;

    @media (min-width: $bp-md) {
        width: 650px;
    }
}

.button {
    padding: 20px;

    @media (min-width: $bp-md) {
        padding: 50px;
    }
}

My problem is, when checking my compiled css for the logo the media query looks like this: @media (min-width: 1024px) {...} (so it has the expected value, but for my button it looks like this: @media (min-width: get-bp('md')) {...} (so it does not map the value it shows the function call as value). Both are independent Angular components importing the breakpoint util.

So my question, is it possible to assign values to scss variables by calling a function? And why does it work for one component but on the other it doesn't work? I know I could do it with map-get(), but I do this already in the function so it would be also somehow redundant.

1
  • try to find a place where .button can be overriden Commented Apr 14, 2021 at 11:35

3 Answers 3

2

In SASS the function needs to be declared BEFORE it is used. As in your example it is not $bp-sm: get-bp('sm') doesn't know that you want to call a function and advices a string to the variable.

Solution: just write variables AFTER the function declaration. That code should work:

// ### SASS: 

$breakpoints: (
    sm: 768px,
    md: 1024px,
    lg: 1280px,
    xl: 1400px
);


// function-declaration FIRST:
@function get-bp($bp) {
    @if $bp {
        $bp: map-get($breakpoints, $bp);
    } @else {
        @error "Parameter #{$bp} is unknown or empty.";
    }

    @return $bp;
}

// variables settings with function call 
// AFTER FUNCTION DECLARATION
$bp-sm: get-bp('sm');
$bp-md: get-bp('md');
$bp-lg: get-bp('lg');
$bp-xl: get-bp('xl');



.logo {
    width: 250px;

    @media (min-width: $bp-md) {
        width: 650px;
    }
}

.button {
    padding: 20px;

    @media (min-width: $bp-md) {
        padding: 50px;
    }
}
  

// ### compiles to CSS

.logo {
  width: 250px;
}
@media (min-width: 1024px) {
  .logo {
    width: 650px;
  }
}

.button {
  padding: 20px;
}
@media (min-width: 1024px) {
  .button {
    padding: 50px;
  }
}


Sign up to request clarification or add additional context in comments.

2 Comments

Damn, this was also what I thought I could be and I'm sure I tried it out but it didn't work. Maybe it was a caching problem, since changing it like you said and full-reload in browser it works now! Nice to know thanks mate!
Yeah ... I think we all know such situations ... I personal for sure know exactly that ... everytime again trying to resist biting the board ;-)
1

the order of css lines are important. you should place your functions in global scope and use that in the following lines. tell me if this solution worked.

Comments

1

It is too big for comment, however I would rewrite query like this:

@media (min-width: $bp-md) {
    .button {
            padding: 50px;
    }
    
    .logo {
        width: 650px;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.