angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Can't access Sass Global Variables in components scss

Open paramsingh88 opened this issue 9 years ago • 29 comments

I have a habit of importing my _variables.scss and _mixins.scss into my root src/style.scss file

@import 'styles/base/_variables.scss';
@import 'styles/base/_mixins.scss';
@import 'styles/helpers/flexbox.scss';
* {
    padding: 0;
    margin: 0;
}

body{
	padding: 4%;
	font-family: sans-serif;
}

But when I'm trying to access the variables in my components.scss, it's giving me an error color: $primary-color;

Isn't there a way I can achieve this simply? Please Help.

paramsingh88 avatar Dec 22 '16 09:12 paramsingh88

You need to include styles/base/_variables.scss in each and every file where you need to use variables. Same goes for mixins. The actual content of those files will not be included in the final build and wont impact the build size in any way.

grizzm0 avatar Dec 22 '16 12:12 grizzm0

For now the solution is as @grizzm0 says.

Dupe-ish of https://github.com/angular/angular-cli/issues/1791, work in progress in https://github.com/angular/angular-cli/pull/2747.

filipesilva avatar Dec 30 '16 16:12 filipesilva

Seriously there is no better way for this?

JustasKuizinas avatar Apr 26 '17 20:04 JustasKuizinas

This is how SCSS has always worked. It really has nothing to do with the Angular CLI. The component's CSS is being compiled by itself - the other files aren't there. There's no reason for them to be there.

This is not only how it works, this is how you should WANT it to work. You don't want coupling with monolithic styles. You WANT each component to be a COMPONENT. They are isolated, encapsulated pieces of logic.

ciel avatar May 09 '17 03:05 ciel

Add this to .angular-cli.sjon (change includePath to your path):

"stylePreprocessorOptions": {
        "includePaths": [
          "../node_modules/bootstrap/scss/"
        ]
      },

rossanmol avatar May 29 '17 13:05 rossanmol

@JustasKuizinas if you want you can:

  • Ditch SASS and use css vars.

  • write a service that create css variables in style element in the head.

  • Get variables for the head style service from a js config

  • reuse same ts config in your template / ts code

  • When building your project with webpack use pure css with autoprefixer or

  • use the same approach as mentioned above with no variables for color / sizes configured if you van the benifits of the pre processor

More on this here: https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_variables

As mentioned above this is not an angular issue but more of a seed (project base that use angular) issue (with all the build processes and pre-processing pre-dictated by the author).

dewwwald avatar Jun 12 '17 11:06 dewwwald

Looks like we just need to duplicate in every component.

@import "~style/variables";

AlexanderKozhevin avatar Sep 26 '17 11:09 AlexanderKozhevin

@AlexanderKozhevin Exactly. Just like you need to import anything you use in typescript you also need to import anything you use in SCSS.

grizzm0 avatar Sep 26 '17 11:09 grizzm0

I can make another recommendation on this.

I am buzzy building a framework that will have no classes, only placeholders, mixins and variables. You can import this in the tag and write your sass as per usual but for the component.

My afore mentioned solution has some problems, for example css has not loops and ifs. Therefore it is better to just use sass.

You can then import base styles like import grid or responsive-grid in you app component (root component).

dewwwald avatar Sep 26 '17 12:09 dewwwald

Well, there is this loader that could resolve all our problems - https://github.com/shakacode/sass-resources-loader I've been using it in one of the ejected projects, but will be nice if we could do this directly from CLI.

GuskiS avatar Dec 19 '17 12:12 GuskiS

@GuskiS Yep, sass resources loader is great, but out of the box ng cli karma config will fail with it. SASS vars will not get picked up, but during a build and development it will work.

...Currently trying to figure out to get my tests to pass with it 🤔

jsonberry avatar Jan 17 '18 16:01 jsonberry

@GuskiS that looks like the best option, combining that with importing sass modules using angular's styleUrls: [] has turned out to be my solution to the problem.

Again, as always, setup usually depends on your project.

dewwwald avatar Jan 22 '18 17:01 dewwwald

My current solution is to turn off ViewEncapsulation for the component, but I dont like this at all. I wish there was a clean solution for this.

@Component({ selector: 'app-admin', templateUrl: './admin.component.html', encapsulation: ViewEncapsulation.None, styleUrls: ['./admin.component.scss'] })

aoakeson avatar Feb 13 '18 23:02 aoakeson

@aoakeson, form my understanding, that is based on experience and not the actual algorithm, view encapsulation only applies when styling is applied to a class specific to that component. I haven't had to turn on viewEncapsulation for any of my component. The global stylesheets just seem to apply.

I am curious to know why this is different for you.

dewwwald avatar Feb 14 '18 17:02 dewwwald

@dewwwald I think I figured out my problem. It turns out the elements that I was trying to access using these global variables were outside the scope of the main angular app. As my app is comprised of multiple modules, and apps it was not able to access these. I moved things around and am now able to have my components keep ViewEncapsulation on.

aoakeson avatar Feb 14 '18 18:02 aoakeson

@grizzm0:

You need to include styles/base/_variables.scss in each and every file where you need to use variables.

@AlexanderKozhevin

Looks like we just need to duplicate in every component.

You should never do this because https://github.com/angular/angular-cli/issues/8431 :( Thus component.scss is totally unusable in real projects now...

LastDragon-ru avatar Jun 28 '18 12:06 LastDragon-ru

@LastDragon-ru As explained in that issue you should never import files that actually render CSS more than once. You can import files that contain variables and mixins as many times as you want as they do not affect the output.

grizzm0 avatar Jun 28 '18 12:06 grizzm0

@grizzm0, For example with bootstrap sometimes more simply and better to use something like &.disabled {@extend .form-control:disabled;} or @extend .is-valid than redefine(copy) this styles. because after copying we will need to check it after each bootstrap update :( So this feature/issue can be useful.

LastDragon-ru avatar Jun 28 '18 12:06 LastDragon-ru

I prefer creating index.scss in the components folder (lets say you have a folder Search that lives under components). Then, import all the .scss files used in this folder into your newly created index.scss. Then, include @import "../components/Search/index"; in App.scss or wherever all your global partials are stored. Now all the .scss files inside components/Search will have access to global variables.

kkuzmina avatar Nov 23 '18 20:11 kkuzmina

@kkuzmina I know that it`s not right thing to do, but helped me as well 👍

yuriirud1 avatar Feb 13 '19 23:02 yuriirud1