angular: Trying to use :host-context and :host together in the same rule doesn't work.
I’m submitting a … (check one with “x”)
[x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior
Writing a CSS rule in a component style sheet that uses both :host-context and and :host doesn’t work:
:host-context(.parentClass) :host(.hostClass) .someClass { // not applied }
Expected behavior
The above rule should work.
Minimal reproduction of the problem with instructions
http://embed.plnkr.co/puGWYrVlpu1Tim1b2xuA/
What is the motivation / use case for changing the behavior?
Trying to write a rule that uses both host-context and host.
Please tell us about your environment:
OS X, blah blah blah, pretty sure this doesn’t matter.
- Angular version: 2.0.X
2.4.5
- Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]
Chrome, Safari
-
Language: [all | TypeScript X.X | ES6/7 | ES5]
-
Node (for AoT issues):
node --version
=
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 11
- Comments: 19 (4 by maintainers)
Commits related to this issue
- fix(compiler): handle `:host-context` and `:host` in the same selector In `ViewEncapsulation.Emulated` mode the compiler converts `:host` and `:host-context` pseudo classes into new CSS selectors. P... — committed to petebacondarwin/angular by petebacondarwin 3 years ago
- fix(compiler): handle `:host-context` and `:host` in the same selector In `ViewEncapsulation.Emulated` mode the compiler converts `:host` and `:host-context` pseudo classes into new CSS selectors. P... — committed to petebacondarwin/angular by petebacondarwin 3 years ago
- fix(compiler): handle `:host-context` and `:host` in the same selector In `ViewEncapsulation.Emulated` mode the compiler converts `:host` and `:host-context` pseudo classes into new CSS selectors. P... — committed to petebacondarwin/angular by petebacondarwin 3 years ago
- fix(compiler): handle `:host-context` and `:host` in the same selector (#40494) In `ViewEncapsulation.Emulated` mode the compiler converts `:host` and `:host-context` pseudo classes into new CSS sele... — committed to angular/angular by petebacondarwin 3 years ago
Change the order:
https://plnkr.co/edit/mH467v5YODo2bIvAHtKk?p=preview
The issue is that
translates to
instead of
The same problem here. We would like to “scale” ui with body css class and host elements like: :host-context(.small) :host{ height: 50px; }
:host-context(.medium) :host{ height: 70px; }
:host-context(.big) :host{ height: 90px; }
do not work-
I came here looking for help, I really needed it for my project, I was almost hopeless reading the comments, but I found a solution! What I found out is that I had the wrong concept about host-context, I will explain here so that nobody falls into the trap of the name (yes the name is quite confusing)!
According developer.mozilla.org, despite being called host-context it sees the context but the final selector is the host itself, so there is no point in declaring it as a relative of the :host like
:host-context(.context) :host(.class-inside-host)
(or reverse), as it selects the host itself under those context conditions and you will say to select host inside same host, or declare it two times in same element selection like:host(.class-inside-host):host-context(.context)
.So, since it selects the element host itself, I can use it like this:
I hope this helps 😃
Adding a request for this feature as well. We are using
:host-context([dir])
to support RTL language support and automatic direction flipping of all styles, and in some cases need to use it with:host
which is completely broken in all current releases of Angular.I can provide more background if needed or if others are interested, but it is not necessary for the testing and completion of this issue report.
I’m not sure the severity of inconvenient is accurate. It full out breaks anything that uses both together and there is no reasonable workaround that works in all cases.
@DzmitryShylovich
Why would we need to change the order?
Based on my understanding, if I want to apply a style to the element which has a class named
someClass
under the same circumstance,This seems to make sense but apparently, it doesn’t work and I had to change the order as you said. What am I missing?
Thanks! This should really be documented, or fixed to support the other way around, as that seems more intuitive for CSS rules.
@KevynTD wow that works perfectly!
Also stumbled over this. I observed that it especially does not work, when the preceding
:host
selector has a class.:host :host-context(.blue-theme)
works, it applies the rules to the selected:host
:host(.big) :host-context(.blue-theme)
works always, even if the:host
element does not even have the.big
class.https://stackblitz.com/edit/angular-adt195