🐞 bug report
Is this a regression?
No
Description
When using custom accordion component and put another accordion component inside the accordion component, Angular encapsulating does not work. I have svg arrow there and it has same property (f.e. _ngcontent-how-c51) in inner accordion than outer accordion. (here is some blog post about properties: https://blog.angular-university.io/angular-host-context/)
The main problem is that the inner accordion’s arrow does not rotate.
🔬 Minimal Reproduction
https://stackblitz.com/edit/angular-fb9s2c?file=src%%2Fapp%%2Fapp.component.html You may see, that in the demo, the inner accordion arrow does not rotate. I believe it is something to do with encapsulating.
🌍 Your Environment
I created new project with ng new
.
Anything else relevant?
Main issue here is how to manipulate the svg arrow to rotate in situation described in stackblitz linked above. There are two nested custom components and it seems it is impossible to use css (and :host-context) to manipulate the svg arrow so it would rotate in inner accordion and outer accordion. With different scss it is possible to rotate the inner accordion’s arrow, but then the outer accordion arrow does not rotate.
This rotates inner accordion’s arrow:
:host-context(.accordion-div .is-expanded) img {
transform: rotate(180deg);
}
This rotates outer accordion’s arrow:
:host-context(.is-expanded) img {
transform: rotate(180deg);
}
If one puts both there still both arrows does not rotate.
So I think the problem you have is that
:host-context
does not care about encapsulation. For example:This rule says that if there is “any” element that is an ancestor to the current component that has the
is-expanded
class, then rotate theimg
element.So in HTML that looks like:
Both
img1
andimg2
will be rotated since evenimg2
will see theis-expanded
div two levels above.I was going to post something similar, but @petebacondarwin beat me to it😁
Since
:host-context()
matches any of the root’s ancestors (all the way up to the document element), a selector that looks like:host-context(.foo) img
will match an<img>
tag if any of its ancestors has a.foo
class.So, you need a selector that will be able to tell whether the owning accordion-pane (and not any of its ancestors) is opened or not. One way to achieve this would be to add the
.is-expanded
class to the immediate parent of the<img>
tag based on the parentAccordionPaneComponent
‘sisOpened
property.You can change accordion-pane-header.component.ts like this:
And then change accordion.component.scss like this:
I am going to close the issue (since there doesn’t seem to be anything to fix in Angular), but feel free to continue the discussion below.