-
Notifications
You must be signed in to change notification settings - Fork 12k
Using global scss variables in my components styles. #1253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
There isn't any such functionality in the CLI itself, no. There is sass compilation but that's it. |
@a5hik A temp fix for that is to make a _varaibles.scss and import where ever you use your variables. Not the cleanest way. |
What @AmarildoK suggested could actually be incorporated as a feature into angular CLI. There could be a shared scss file somewhere and new |
Yup. Certainly need this feature. |
Still nothing? |
Any update about this people? |
Any update on this? |
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
_vars.scss
When I try to reference a variable declared this way in a component's 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. |
can someone explain to me how the SASS transpiling happens?
is that correct? |
I have the same problem with global sass variables. Also the transpile is for me at the moment a blackbox |
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.
Forgot you also on the top level component need to set View Encapsulation.
|
@greenchapter yeah.. I wish there was some good documentation for it. if you find anything, do let me know. |
@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. |
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 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 @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 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 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 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. |
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.
It does not generate any CSS overhead in the components, you can leave ViewEncapsulation enabled and all is good. |
Ahh this is a frustrating problem. I want to just import colors and things one time. Not on every scss file. |
I'm so not sold on the encapsulated CSS train. I get the thinking, but honestly if you just write good reusable modular css from the get go it's not a problem at all. It's only an issue when you don't really get the cascade. BEM solves this issue pretty well IMO without any encapsulation at all. |
Yes its kind of frustrating but thats the way scss works. What i don't understand is why component-styles are always inline. If they would be concatenated to global style.scss(before running sass) instead we would have no problems with undefined variables or mixins. Beneath that there are no sourcemaps at all for the inline styles. |
I see there is plenty of people having the same issue here. I understand Angular CLI is done the way it is (take it or leave it). But not listening so many people requesting for a more pragmatic way do define the Sass inheritance + stack looks more like a whim rather than a technical issue (should I have to say what CSS means?). Is quite logic to think a developer would like to handle this in a reusable way avoiding over-importing and taking full advantage of cascading the styles. Hope Angular CLI team understand this is a constructive critic from someone, like many others, that doesn't see a good reason why don't move forward with such a highly-demanded feature. |
I'm expecting to see this feature implemented soon. It's so frustrated to write that import statement for every component style. |
How vould this be fixed? The compiler does not include your variables, it just compiles each component-scss seperately. Therefor you need to include your variables/mixins/functions. This is how sass works and writing that import statement is the correct way. |
I hope they fix this issue soon. Just in case you need import your sass, Webpack provides a better way to do it: @import “~variables.scss”; It's less frustrating than specify the full path. |
Is there a fix coming or should one use the "work around" described here ? |
There seems to be no 'fix' yet. But in any case, don't use the suggestion with |
There is a simple way of achieving this.
This assumes your scss is in a folder under src called |
@hellofornow, the above did not seem to work for me. Are there any gotchas? |
@gustavossantos no gotchas. I can provide some more details though.
Contents of
|
My two cents: I've tried CSS native variables and they works well with Angular and SCSS (also for those using Prettier or sth). https://css-tricks.com/difference-between-types-of-css-variables/ |
@hellofornow this means each file you'd import a vairables file into each component right? I don't think that's what OP wanted
|
@hellofornow what's the benefit for changing the Also, in general I see no problem implementing something. There might be some implications though, i.e. how are lazy loaded modules handled? I suggest a discussion on |
@daniel-seitz perhaps a bit of confusion -- this issue is going over many different topics. Using Adding Without adding the appropriate scss path(s) into As for automagically importing I do think if this feature was added it would need to be a new property of |
Thank you for the reply @hellofornow. I too would welcome this feature however from my current understanding its not trivial to implement this. I was not clear enough with my question for you, so one clarification: If one just imports like this BUT! if you do Keeping that in mind, what's the need to do this then? I found no documentation.
Then I also did some tests (also with lazy loaded modules, ...) and - as we all know - we would not expect this to happen: Angular CLI with Sass compilation encapsulates the styles so that they won't have effects on other parts of the system. When using import, we circumvent this and can use all variables, however when mixing variables with real css we also can cascade. PROBLEM here is that each time use import, that 'real css' gets into the resulting file inline, over and over - which again goes against cascading and worse, makes our dist bigger. The SOLUTION would be to make the variables (mixins, ...) available to the compilation so that it just knows about them and translating on the fly, nothing else. I'd like to have these variables, or at least the starting point, in src/styles.scss as it is default but likely needs to be configurable. If I find the time, I will dig deeper as I know that it's not just us who want this... |
Am I wrong or AlexHenkel's comment on Jan 23 solves the problem ? I have my _variables.scss defined
So then I can use this variable in a component
To clarify, my styles.scss looks like:
not sure if OP had same problem but works for me, cause I don't have to hard code the default color |
Sorry if this is off topic, but per my understanding in this thread, it seems that SASS process files more in isolation, then say LESS did, would that be correct? In essence, the use case is of being able to use a variables.css $color: 'green';
$theme: 'generic';
@mixin setTheme($theme: 'generic') {
@if($theme == 'a') {
$color: red;
}
@if($theme == 'b') {
$color: blue;
}
} app.component.css @import "~bootstrap/scss/bootstrap.scss";
@import "./styles/variables.scss";
@include setTheme('a'); header.component.css @import "./styles/variables.scss";
@include setTheme('a'); // only changes red if this is set manually, which is hardcoded and not ideal for a reusable component from npm
h1 {
color: $color;
} In this case, anything processed in my app component using Thanks, and hope that wasn't too confusing! Any thoughts would be greatly appreciated! |
I was able to get something working for my use case using sass-vars-loader. |
Just an FYI, it looks like using the tilde to reference 'src' from the component level ala |
I can't believe how difficult Angular makes using SASS. I'm about to switch to React/webpack |
this is ONE OF THE MOST COMMON THINGS I CAN IMAGINE. How can you get that wrong?! also: you can make this work easily via webpack and sass-loader itself. Man it would be great if ng cli offered a way to extend the webpack conf out of the box. Like Vue does. |
Guys this is how sass/scss works and has nothing todo with Angular. Just import your variables and you are good to go OR use a global stylesheets file. |
So, in conclusion, if you |
Is there a way to use sass variables globally without the need to manually import in every single component? Perhaps this would be bad practice anyway as every component then has access, even if t doesn't need it... |
Do one thing. It will work. |
What if i want separate css bundles, that i can switch at runtime, each bundle using its own _variables.scss? |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
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.
The text was updated successfully, but these errors were encountered: