Angular 6 alterante way to global css - css

I'm using Handsontable in one of my component, and it forces me to include it's css globally in my angular.json file
"styles": [
"./src/styles.scss",
"./node_modules/handsontable/dist/handsontable.full.css"
],
and it affects my calendar component all over my application.
How can i include this css only for that handsontable component and disable it for all other components.
writing import in handsontable component scss file doesn't work.
Thanks in Advance.

This works as below. Pay attention to encapsulation: ViewEncapsulation.None. This will disable default Emulated DOM, so that styles of this component, will be applicable to children as well. Other wise, by default Emulate DOM, protects scopes of each component (that is why it was not working in your case).
Refer to documentation also.
However, that won't fix your problem actually. Because, as per documentation:
[ViewEncapsulation] None means that Angular does no view encapsulation. Angular adds the CSS to the global styles. The scoping rules, isolations, and protections discussed earlier don't apply. This is essentially the same as pasting the component's styles into the HTML.
What effectively means, that the output will be in global scope any way.
So probably it is easier just add once in your main styles.scss:
#import '~/handsontable/dist/handsontable.full.css';
Working with ViewEncapsulation.None encapsulation.
// component.ts
#Component({
selector: 'app-table',
template: `<hot-table ....></hot-table>`,
styleUrls: ['./table.component.scss'],
encapsulation: ViewEncapsulation.None
})
export class TableComponent {
...
}
// component.scss
#import '~/handsontable/dist/handsontable.full.css';

You can try the following
#Component({
selector: 'app-home-component',
template: '<div></div>',
styleUrls: [
'path/handsontable.full.css',
'./home.component.css'
],
})
export class HomeComponent{}

Related

Prevent CSS overwrites from web components in parent page

I created a web component using Angular Elements (6.0.3). When I use the web component in another website, the webcomponent overwrites the styles of the parent page since it also uses Bootstrap. I know that there is the view encapsulation principle, but as far as I know the "Native" encapsulation is not yet fully supported. So for now I am using the default "Emulated".
I found out that if I add :host ::ng-deep in front of my styles, they are only applied to the web component itself which is great. However, all css and scss files that I load in angular.json seem to be overwriting the parent page too. Is there any way I can prevent this behaviour from imported style-files?
My style-imports in angular.json:
"styles": [
"node_modules/intl-tel-input/build/css/intlTelInput.css",
"node_modules/bootstrap/dist/css/bootstrap.min.css",
"node_modules/angular-calendar/css/angular-calendar.css",
"src/styles.scss",
"node_modules/font-awesome/css/font-awesome.min.css",
"src/assets/css/wizard.css",
"src/assets/css/calendar.css",
"src/assets/css/ng2-select.css"
]
The 'styles' imports in the angular.json are for styles you wish to define globally to the whole app. To use ViewEncapsulation you'll need to link their stylesheets inside of their components like so:
#Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css'],
})

CSS classes not picked up during Angular 5 compilation

I'm using ag-grid in my angular 5 project. I'm trying to add css classes to the cells. Having looked the extensive documentation on ag-grid cell style I have tried to use cellClass and cellClassRules.
The styles are defined in the scss file, as an example:
.readonly-cell{ background-color: #cccccc; }
The scss file is then included in the component:
#Component({
selector: 'app-volume',
templateUrl: './volume.component.html',
styleUrls: ['./volume.component.scss']
})
I then apply the class to the column:
{headerName:'Power', field:'power', width:150, cellClass:'readonly-cell'}
The grid itself is working fine. The issue is that the the power cells do not change colour. When I check the rendered HTML in Firefox, I can see that the cell have indeed have the class readonly-cell applied to them. But the style details are not listed on the rules panel.
This makes me think that the classes are not being picked up during the compilation. I don't think it's an issue with ag-grid, but the way the style classes are being picked up.
Is there any way to troubleshoot why the classes and the definitions are not being included in the compilation?
You get that behavior because element you are trying to target by a CSS rule is generated outside your Angular component and Angular adds special attributes so that component CSS applies only to that component (and not to its child components, any DOM nodes added outside Angular etc.). You should either build all the HTML you need to style using Angular and in exactly the same component as your styles, or disable that feature. You can disable it either by using ViewEncapsulation.None:
#Component({
selector: 'app-volume',
templateUrl: './volume.component.html',
styleUrls: ['./volume.component.scss'],
encapsulation: ViewEncapsulation.None
})
or by using /deep/ in your stylesheet, as described here:
https://angular.io/guide/component-styles#deprecated-deep--and-ng-deep

Isolate bootstrap CSS to one angular component?

Is it possible to isolate Bootstrap's css file to one Angular component?
When including the file globally in styles.css or in .angular-cli.json, the global styles are causing issues with other components that should not be using bootstrap. I have tried to do this by importing it from the component's CSS file but the styles do not seem to render properly...
I am using Angular 5 and Angular Cli 1.7.2.
you can explicitly set the encapsulation strategy using the encapsulation property.Set it to ViewEncapsulation.Native for your case
#Component({
// ...
encapsulation: ViewEncapsulation.Native,
styles: [
// ...
]
})
export class HelloComponent {
// ...
}
Native - styles from main HTML do not propagate to the component. Styles defined in this component's #Component decorator are scoped to this component only.
Refer this View Encapsulation.

Overwriting styles from Bootstrap in an angular2 component-stylesheet

I have an angular2 application build with angular-cli containing several components. Each component has a referenced stylsheed (scss).
Unique styles from those stylesheets are correctly applied to the component-template.
What I cannot do is overwrite styles from external css's which are included in the angular-cli from those component-stylesheets.
As soon as I move the style to the central styles.scss it works.
My angular-cli.json looks like this:
"styles": [
"../node_modules/bootstrap/dist/css/bootstrap.css",
"../node_modules/bootstrap-datepicker/dist/css/bootstrap-datepicker.css",
"../node_modules/bootstrap-timepicker/css/bootstrap-timepicker.css",
"styles.scss"
],
Any Ideas how I can overwrite the bootstrap-css in the component.scss?
I've also posted this question as a possible bug to the angular-cli project and got the hint I needed:
Emulated view encapsulation (the default) emulates the behavior of Shadow DOM by preprocessing (and renaming) the CSS code to effectively scope the CSS to the component's view. If those classes got renamed for this behaviour, it cannot work and I cannot override styles from ie. bootstrap.
If I now set the encapsulation in the component to none, it works and I can override the styles in the component.scss.
#Component({
selector: 'app-generic-view',
templateUrl: './generic-view.component.html',
styleUrls: ['./generic-view.component.scss'],
encapsulation: ViewEncapsulation.None
})

AngularJS 2 styleUrls: What's up with concatenation?

My AngularJS 2 application has several style files which I concatenate with a gulp task. That's all fine, since they'll end up in a big file I send to the browser in production. My question is about the Angular 2 #Component and its styleUrls attribute.
#Component({
selector: 'hero',
templateUrl: 'hero/hero.template.html',
styleUrls: ['hero/hero.component.css'],
inputs: ['hero']
})
Thanks to shadow DOM emulation in default mode (emulated) the styles defined in the hero/hero.component.css are applied only to the component just like I want. My question is, what happens with concatenation? I cannot bundle all the CSS files specified in the multiple styleUrls since we'd be defeating the purpose of encapsulation: the styles for a component would leak to the whole document. However, I don't want either to make a call in production for every CSS file a component needs. How can I concatenate those styles (and possibly minify them) so the client gets them all in a single call, and still preserve encapsulation?
The template and css files can be bundled along the js files using a system js plugin.
import { Component } from '#angular/core';
import html from './hero/hero.template.html!text';
import css from './hero/hero.component.css!text';
#Component({
selector: 'hero',
template: html,
styles: [css],
})
export class HeroComponent implements {
}

Resources