I'm looking for a generic solution to find the scrollable container of an element (I'd like to listen to the scroll event). The normal approach would be to traverse up in the DOM tree in check the parent elements until one of them is scrollable - here are some solutions like this. However this will fail if shadow DOM is also in the picture.
Here's an example structure:
<app-home>
<ion-content>
#shadow-root
<div class="inner-scroll">
<slot>
-> <my-element> (reveal)
</slot>
</div>
<my-element>...</my-element>
</ion-content>
</app-home>
In the above example the scrollable element is .inner-scroll
, which is inside the shadow dom of ion-content
. Since my-element
gets into the container through the slot
, traversing up the tree will never reach the scroll container (my-element
-> ion-content
-> app-home
).
I found that ion-content
has a getScrollElement method, which can be used in this specific case, however I'd like to know if there's a generic DOM based solution, since I'd like this to work regardless of the context.
UPDATE:
To be more clear, I'd like my-element
to be a standalone, reusable component, which can find it's scrolling container, no matter where it's placed.
What I found, that the built in scrollIntoView
method works on the DOM element, and it scrolls the correct container, meaning that the browser somehow figures it out, but does not expose a method which returns the scroll container.
Another idea was to listen to the scroll event on a higher level, without knowing the exact element, but this is not working either, because the scroll event does not propagate.
Any ideas?