3

I was hoping to use Angular Elements with an external library (Kendo UI). I managed to create a component and add it to my app. The problem is that Kendo's CSS messed up with all my app styling.

Any idea on how could I encapsulate the CSS to only apply to the component and not "leak" to the rest of the parent app?

Thanks

UPDATE

See the following stackblitz for the code: https://stackblitz.com/edit/angular-kpmnjg

If I build the code I get 4 .js files and styles.css file (containing Kendo UI's styles). Adding the files to a clean html page works fine. But as soon as I add them to my app, the styles.css bleed out to my app, messing with the styles (buttons/inputs/etc).

4
  • Where Are you adding the kendo, if it is as I suspect globaly, try add it only in the components you use, in styleUrls Commented Aug 3, 2018 at 16:14
  • Can you please reproduce it in a stackblitz or post code? Commented Aug 3, 2018 at 16:14
  • @TemkitSidali how would I add it directly through styleUrls? I tried but I always get an error that it can't find the file. Commented Aug 3, 2018 at 19:21
  • @HobojoeBr , check the path, something like this worked fine, styleUrls: ['./elements-test.component.css','./../../_kendo.scss'] , you can see here stackblitz.com/edit/angular-kpmnjg Commented Aug 3, 2018 at 19:31

3 Answers 3

1

Without having knowledge of Kendo UI and having only read about angular elements, in general how I'd encapsulate the CSS is use SASS and do

.angular-element-class {
  @import 'path/to/kendo.css';
}

or hopefully if Kendo UI has SASS api

@import 'path/to/kendo.scss';

.angular-element-class {
  @include kendo-mixin-needed-for-angular-element;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi I tried your approach but I get some errors like: ".angular-element-class .k-button[disabled]" failed to @extend "%disabled". The selector "%disabled" was not found. Use "@extend %disabled !optional" if the extend should be able to fail.
Since kendo is not really scoping their styles i am afraid that will result in the same issue.
1

create a _kendo.scss and import all necessary stylesheets:

@import '~@progress/kendo-theme-bootstrap/scss/dropdownlist';
@import '~@progress/kendo-theme-bootstrap/scss/grid';
@import '~@progress/kendo-theme-bootstrap/scss/input';
@import '~@progress/kendo-theme-bootstrap/scss/popup';
@import '~@progress/kendo-theme-bootstrap/scss/tabstrip';
....

You need to have the kendo npm modules installed for this to work.

then in your styles.scss

@import './kendo';

UPDATE:

My guess is that they are bleeding through because they are globally imported. So lets say you have

<kendo-grid>
...
    <table>
      <th>

The styles from the grid will bleed through to your table because kendo does css as such:

.kendo-grid th { ...

So really there is not a way around it except for creating counter-rules which is more than terrible, we had to do it in our project too. We are getting rid of kendo, it is not well fit for angular.

maybe you can get around it by only importing

@import '~@progress/kendo-theme-bootstrap/scss/grid';

in the component itself an not globally. Although I have not tested that. Also it will replicate the SCSS in every component that imports it and blow up bundle size.

5 Comments

thanks for the reply. I tried implementing your approach (stackblitz.com/edit/angular-kpmnjg) and after building, i get some js files and a styles.css. It works on a blank html. But if I add them to an app, the styles.css file bleed into my app styles as it has a lot of styles for html tags. I don't see how it would solve my problem. Have I done something wrong??
Can you name/show any example of the leaking styles? Per default the view encapsulation keeps them intact
There is no view encapsulation on styles.scss
No but in every other component. are you styling globally?
I think I know why @WilliamLohan. I updated the answer
0

It worked for me just to remove the .css file extension.

So instead of having your import statement as

@import 'path/to/file.css'

you could try to do something like

 @import 'path/to/file'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.