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

Using global scss variables in my components styles.

Open a5hik opened this issue 10 years ago • 48 comments

angular-cli: 1.0.0-beta.6 node: 6.2.1 os: darwin x64

Hi I have bootstrap override varaibles.scss, which I want to use as a Global style for my app.

So, I depend on these variables on every component.scss I write. Is there a way I can add a global dependency of variables.scss.

Right now I do import the variables.scss manually in every scss file that require.

a5hik avatar Jun 30 '16 11:06 a5hik

There isn't any such functionality in the CLI itself, no. There is sass compilation but that's it.

filipesilva avatar Jul 04 '16 00:07 filipesilva

@a5hik A temp fix for that is to make a _varaibles.scss and import where ever you use your variables. Not the cleanest way.

AmarildoK avatar Jul 15 '16 17:07 AmarildoK

What @AmarildoK suggested could actually be incorporated as a feature into angular CLI. There could be a shared scss file somewhere and new .scss files in generated components could use have an import line to include the shared scss file by default.

pramttl avatar Jul 16 '16 09:07 pramttl

Yup. Certainly need this feature.

purushottamjha avatar Oct 02 '16 05:10 purushottamjha

Still nothing?

MateuszG avatar Nov 18 '16 08:11 MateuszG

Any update about this people?

mhmo91 avatar Nov 20 '16 16:11 mhmo91

Any update on this?

pquarme avatar Dec 01 '16 15:12 pquarme

I'm quite new to frontend, but I'm trying to declare some global SCSS vars for my Angular app, and I just want to be sure that it's not intended that what I'm doing should work.

I'm running angular-cli 1.0.0-beta.21. In my angular-cli.json file, in apps[0].styles, I reference my styles.scss file.

styles.scss

@import url('https://fonts.googleapis.com/icon?family=Material+Icons');
@import url('https://fonts.googleapis.com/css?family=Droid+Sans:400,700');
@import '_vars';
@import 'reset';
@import 'global';
@import 'index';

_vars.scss

/* VARIABLES  */
/* brand colors */
$color-accent: #2980b9;
$color-primary: #ecf0f1;
$color-subtle: #aaaaaa;

/* rarity */
$color-rarity-legendary: #4c139d;
$color-rarity-ascended: #fb3e8d;
$color-rarity-exotic: #ffa405;

When I try to reference a variable declared this way in a component's .scss file, the console reports that the variable hasn't been declared.

Is this failing because of something I'm doing wrong, or is this not possible under the current version of the CLI?

Thanks so much for your time and help.

jammerware avatar Dec 01 '16 16:12 jammerware

can someone explain to me how the SASS transpiling happens? I assume this happens:

  1. ng-cli transpiles global sass file to css
  2. find all component specific sass and transpile those to css
  3. concatenate all that css

is that correct?

ahmed-musallam avatar Jan 17 '17 20:01 ahmed-musallam

I have the same problem with global sass variables. Also the transpile is for me at the moment a blackbox

greenchapter avatar Jan 18 '17 22:01 greenchapter

What we do is create a folder called shared and under it a styles folder. We then create our sass there. Finally from your top level component typically "app.component.scss" just import.

@import './shared/styles/default';

Forgot you also on the top level component need to set View Encapsulation.

encapsulation: ViewEncapsulation.None

screen shot 2017-02-02 at 5 31 29 pm

screen shot 2017-02-02 at 6 22 00 pm

origin1tech avatar Feb 03 '17 01:02 origin1tech

@greenchapter yeah.. I wish there was some good documentation for it. if you find anything, do let me know.

ahmed-musallam avatar Feb 03 '17 01:02 ahmed-musallam

@ahmed-musallam see my above example I had forgotten the encapsulation flag in the root component. Just remember that the app.component.scss will not be encapsulated so any style imported there will propagate to any child component etc.

In may cases this works just fine, I see little issue with it. you can still have encapsulated styles for components.

origin1tech avatar Feb 03 '17 02:02 origin1tech

Even with the encapsulation set I'm still getting a missing var error on compile. Is there something else that needs to be setup?

askdesigners avatar Mar 13 '17 16:03 askdesigners

@askdesigners hard to say I'd need to see it. The above example I showed works just fine is it possible there's something up with your sass itself? Maybe post some screenshots or something and maybe something will stand out.

origin1tech avatar Mar 13 '17 16:03 origin1tech

@origin1tech @askdesigners you should not disable encapsulation. It's just bad when you have leaking CSS.

If you know SASS, you know that variable and mixin declaration are NOT transpiled to CSS. So there is no issue with doing what @AmarildoK suggested above. just make a variables and mixins partial and include it when you needed. Since vars and mixins do not transpile unless you use them, there is no overhead here whatsoever.

What @a5hik is asking for in the thread is for convenience only... if you use angular, you are already familiar with angular's ceremonial steps for creating components and services.. this is the same thing... Additionally, I dont think the SASS transpiler supports injecting variables on the fly (during transpiling). the only way this can be done is by writing import statement into each sass file before SASS transpiling. And this can be done by a nodejs script if you must have that convenience.

ahmed-musallam avatar Mar 13 '17 16:03 ahmed-musallam

@ahmed-musallam it's only a "leak" if you don't define it.

zero wrong with having a few styles that are global to your app for convenience. If that weren't the case cli wouldn't build with a global styles file pre-config'd nor would encapsulation option be avail. Disabling on a top level component does not negate encapsulation on sub-components.

If it's not what you prefer I understand but to emphatically suggest it's wrong is absurd. It would be unnecessary heavily lifting to have some separate build task/config just for those handful of global styles.

origin1tech avatar Mar 13 '17 17:03 origin1tech

@origin1tech of course! you should use global CSS when you need global styles. Obviously no brainer there. And I would not repeat the same css in every component instead of using a global style.

but what your example, about setting encapsulation: ViewEncapsulation.none does not work as you described it nor was it intended to be used that way. the good folks at rangle.io explain the difference in the three encapsulation methods here: https://angular-2-training-book.rangle.io/handout/advanced-components/view_encapsulation.html. also Pascal Precht has a really good article over at thoughtram.io describing that as well: https://blog.thoughtram.io/angular/2015/06/29/shadow-dom-strategies-in-angular2.html

anyway, we are not talking about "global styles" in this thread, we are talking about "sass variables" and finding a way to reuse them across components.

ahmed-musallam avatar Mar 13 '17 17:03 ahmed-musallam

What we do, is that we define a base.sass, that does not generates any CSS but only defines global variables.

This stylesheet is then imported on top of the component sass stylesheets.

@import "./src/assets/private/sass/base";

It does not generate any CSS overhead in the components, you can leave ViewEncapsulation enabled and all is good.

AlexBachmann avatar Jun 03 '17 12:06 AlexBachmann

Ahh this is a frustrating problem. I want to just import colors and things one time. Not on every scss file.

KTKate avatar Jun 21 '17 16:06 KTKate