Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to combine [ngStyle] and ::ng-deep?

I try to build an application that can be themed completely at runtime. Therefore i want to set global settings like font-size, color, background-color etc. on my root app.component. For now i do it with predefined CSS classes:

     // CSS
    .font-size-16::ng-deep { font-size: 16px; }

    // TS
    fontSizeClass = 'font-size-16'

    // HTML
    <div [ngClass]="fontSizeClass"></div>

Changing the fontSizeClass string to another class works for deep styling my application. But this solution is not dynamic at all. What i actually want is to set the font-size via [ngStyle] but keep the ng::deep functionality, too.

Is that possible?

And are there reasons to not implement theming completely with JavaScript and Redux?

Thanks in advance!

like image 564
vachee Avatar asked Mar 23 '18 15:03

vachee


People also ask

Can we use NgStyle and NgClass together?

Combining NgStyle and NgClass Directives with our knowledge of Angular Template Syntax, we can marry conditional styling with Angular's Property & Event Binding.

What is :: ng-deep in SCSS?

The ::ng-deep pseudo-class selector So this style will be applied to all h2 elements inside app-root , but not outside of it as expected. This combination of selectors is useful for example for applying styles to elements that were passed to the template using ng-content , have a look at this post for more details.

Is NG :: deep deprecated?

The documentation at https://angular.io/guide/component-styles states this about :ng-deep : The shadow-piercing descendant combinator is deprecated and support is being removed from major browsers and tools.

What is the difference between NgClass and NgStyle?

The NgClass and NgStyle directives are used to apply style to the HTML element conditionally. The NgClass allows you to apply whole class based on condition whereas NgStyle gives you more flexibility to set individual properties.


1 Answers

Try this using angular material radio button if you want to make the border of the radio buttons => transparent

  1. in HTML:

    [ngClass]="{'**transparentBorder**': "--Here yours condition---"}"
    
  2. in css:

a. you add the transparentBorder (that we used in HTML) to: b. add ::ng-deep at the beginning of the CSS c. in the Chrome Dev Tools finds all the classes that the Angular material used in this case, there are 2 option: 1. if the radio button is checked, 2. is unchecked

result:

this is the classes when the radio button is checked we need to add our class transparent-border to angular material classes.

::ng-deep .mat-radio-button.**transparentBorder**.mat-primary.mat-radio-checked .mat-radio-outer-circle {
  border-color: transparent;
}

this is the classes when the radio button is unchecked

::ng-deep .mat-radio-button.**transparentBorder**.mat-primary .mat-radio-outer-circle {   border-color: transparent; }

good luck

like image 138
Yoni Ayalon Avatar answered Sep 22 '22 08:09

Yoni Ayalon