-
-
Notifications
You must be signed in to change notification settings - Fork 405
Description
Components have an element property that gives you the DOM element of the component itself. Getting its parent is pretty easy, using element.parentElement or this.$().parent(). But these approaches fail when dealing with tagless components, which have no element and no this.$() either. Obviously, they still have a parent though!
So far I have used this utility function:
import Ember from 'ember';
const { get, $ } = Ember;
export default function getParent(view) {
if (get(view, 'tagName') === '') {
// Beware: use of private API! :(
if (Ember.ViewUtils && Ember.ViewUtils.getViewBounds) {
return $(Ember.ViewUtils.getViewBounds(view).parentElement);
} else {
return $(view._renderNode.contextualElement);
}
} else {
return view.$().parent();
}
}This seems to work for all Ember versions (pre and post Glimmer2), but is obviously pretty hacky as it is using private APIs. And there does not seem to be any way to get the parent element for a tagless component using just public APIs, unless I am overlooking something!?
So my suggestion would be to introduce a parentElement property, in analogy to the element property.
An example use case: I want to make a tooltip component, that should be used like this:
<button>
Create
{{bs-tooltip title="Just create something"}}
</button>The tooltip component must have access to its parent element, to attach event listeners to it, but should not (until the tooltip is actually shown) add anything to the DOM. Thus a tagless component.
Any thoughts?