Negating the selection of :host-context() locally

Viewed 120

How to properly negate in an Angular component (locally) with :host-context() and :not()?

For example, say there is a light/dark theme, differing only by body.dark (added to "dark" theme) and we need red color only in "light" theme with no changes to color in "dark" theme locally (meaning the code should work in the local Angular component SCSS):

In foo-bar.component.scss:

:host-context(:not(.dark)) { 
    color: red; // red color needed only in "light" theme, no changes to color for "dark" theme
}
1 Answers

Use encapsulation: ViewEncapsulation.None on your component, which will open your component's style for the entire project. And you can control that by using a class on your component and adding your component-specific style under that class.

Shown in a working demo here, with your requirements.

This way, you can add your component-specific (for the theme and general styles) in the component itself, without adding them in global styles.css file.

Related