Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Special Selectors with SASS

Tags:

css

sass

angular

I'm trying to add SASS instead of vanilla CSS to the project and can get it mostly working.

My project setup

  • angular-cli (1.0.0-beta.18)
  • TypeScript transpiler (1.8.10)
  • Webpack bundler (2.1.0-beta.25)

What I've done so far

  1. Installed node dependencies via npm and added them to the dependencies list in package.json
    • node-sass
    • sass-loader
    • raw-loader
  2. Changed the default style to "scss" in the angular-cli.json file
  3. Changed all the style sheet file extensions from .css to .scss
  4. Updated the styleUrls in the relevant components

The issue

There are no errors or warning messages when building the project. Almost all of the styles are applied after building; however, I use the /deep/ (>>>) special selector in a couple of components to pass a style down to the child component - I do this in order to conditionally apply a style to a specific class in a child component depending on its host/parent component. Here's an example of a style that exists in the host/parent component's stylesheet:

:host >>> .holder {
  margin-top: 0px;
  padding-top: 12px;
  border-top: 2px solid #eee;
}

This is the only style that does not seem to be applied correctly. When viewing the UI locally in any browser, it's clear that the .holder class is not defined as above.

There seems to be much discussion of how to get SASS working in Angular 2 projects on StackOverflow, but nothing relating to SASS (or any other CSS preprocessor) and Angular 2 special selectors specifically. My question is two-fold:

  • What, specifically, am I doing wrong here?
  • How, generally, do Angular 2 special selectors figure in to any CSS preprocessing steps?
like image 245
jastingo Avatar asked Nov 25 '16 15:11

jastingo


People also ask

Can I use Sass with Angular?

The Angular CLI can help you stylev12 introduced the option to use inline Sass in your Angular components. The CLI now has an option to provide a inlineStyleLanguage and compiles Sass directly from your Angular components into styling.

Why is deep deprecated?

::ng-deep , /deep/ and >>> deprecation The ::ng-deep pseudo-class selector also has a couple of aliases: >>> and /deep/ , and all three are soon to be removed. The main reason for that is that this mechanism for piercing the style isolation sandbox around a component can potentially encourage bad styling practices.

Has selector in sass?

The :has() pseudo-class takes a selector list as an argument. In the current specification :has is not marked as part of the live selector profile, which means it can not be used within stylesheets; only with functions like document. querySelector().

What is Ngdeep?

::ng-deep is what's called a shadow-piercing descendant combinator. It works because it is able to pierce the shadow DOM. Using ::ng-deep to override a child components styles will achieve the desired result but with unintended side effects.


1 Answers

Replacing :host with \:host and >>> with /deep/ worked for me.

like image 77
Mixthos Avatar answered Sep 19 '22 14:09

Mixthos