Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@import in @if statement in Sass

I want to load only the css needed for the login page for performance. On my other pages I want a grouped css file that will be cached on every page which contain all my css.

I have the following files:

minifiedcssforloginpage.scss
grouped-pages.scss

In minifiedcssforloginpage.scss I declare $load-complete-css:false. Afterwards I import myproject.scss which contains all the imports of my modules, layouts, core... In myproject.scss i want to do something like

@if $load-complete-css {
     @import module1;
     @import module2;
     @import module3;
}

So minifiedcssforloginpage.scss would generate minifiedcssforloginpage.css with less css then grouped-pages.css (that has a var $load-complete-css set to true).

But I get an error that this is not possible "Import directives may not be used within control directives or mixins".

like image 513
woutvdd Avatar asked Dec 14 '12 12:12

woutvdd


People also ask

What is the use of the @import function in Sass?

The SASS @import function helps us to import multiple SASS or CSS stylesheets together such that they can be used together. Importing a SASS file using the @import rule allows access to mixins, variables, and functions to the other file in which the other file is imported.

Is @import deprecated in Sass?

As of October 2021 @import will be deprecated, along with global built-in functions, and one year after that (no later than October 2022) @import support will be dropped. Make sure you go check out the newly refreshed Sass documentation for the latest updates.

How do I write an if statement in Sass?

The @if rule is written @if <expression> { ... } , and it controls whether or not its block gets evaluated (including emitting any styles as CSS). The expression usually returns either true or false —if the expression returns true , the block is evaluated, and if the expression returns false it's not.

Can we use if condition in SCSS?

If the condition proves true, the compiler will execute the if statement's code block. If the condition proves false, the compiler will move on to code outside and below the if statement's code block. An if statement is written with the @if rule, followed by the condition we want to evaluate and a code block.


3 Answers

It's one of those things that's just not allowed. The only thing you can do is turn those imports into mixins (import the file outside the @if and call the mixin where appropriate).

Clarification:

_partial.scss

@mixin partial {
    .test { color: red }
    // other styles here
}

styles.scss

@import "partial";
@if $someval == true {
    @include partial;
}
like image 119
cimmanon Avatar answered Oct 08 '22 11:10

cimmanon


The core dev team is reluctant to implement this feature, although they are considering the implementation of a brand new dependency system.


See the following Github issues :

  • Allow @import within @if (#451)
  • Using @import statements within control directives or mixins (#779)
  • Allow optional @imports (#779)
  • Dynamic Dependencies (#739)
like image 13
John Slegers Avatar answered Oct 08 '22 11:10

John Slegers


Put your styles into various partial files in a way that makes sense to you. Then, you can have create a separate SASS file for your login page that imports only the files with the relevant styles.

To quote from my answer to another question:

It is currently not possible to use SASS to include files dynamically. @import cannot be used within control directives (e.g. @if) or mixins, using a variable in an import directive is erroneous syntax, and there is no directive for ending file execution early (which effectively would allow conditional imports). However, you can solve your issue by changing how you structure your style rules.

... If you have styles that [should be] conditionally included [they] should be encapsulated in mixins, in 'module' or 'library' files. ... The main idea is that importing one such file will not output any css. This way, you can import these files redundantly so you can use the mixins wherever you need them.

like image 7
Stephen M. Harris Avatar answered Oct 08 '22 09:10

Stephen M. Harris