-
Notifications
You must be signed in to change notification settings - Fork 376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can a custom element detect when it is distributed, and what its composed parent is? #941
Comments
Maybe a better title is "mechanism for observing composed connections". In my project that has cooperating custom elements, I've implemented things like If the Custom Elements API would provide something like a class MyEl extends HTMLElement {
// These fire any time the composed parent changes (f.e. distribution to a different slot, being added to a ShadowRoot, etc)
parentComposedCallback(composedParent) {
console.log('New composed parent:', composedParent)
}
parentUncomposedCallback(previousComposedParent) {
console.log('Previous composed parent:', previousComposedParent)
}
// A similar thing with composed children:
childComposedCallback(composedChild) {
console.log('child composed:', composedChild)
}
childUncomposedCallback(uncomposedChild) {
console.log('child uncomposed:', uncomposedChild)
}
} Currently, Having Related helpful new web APIs that a custom element could take advantage of could be Are there any other ways we could allow custom elements take advantage of the composed tree shape in scenarios when they have custom rendering implementations? Example: in my case, LUME, the internal Three.js tree managed by LUME custom elements match in shape with the composed tree so that the rendering outcome is as expected. |
I updated my previous code sample to delete incorrect code comments. |
This is similar to #504. That question was based around the v0 API, but the question is still valid in the v1 API: there's no easy way to do this, apart from the composed parent having to be a custom element that can cooperate and notify a distributed child, which is not possible with builtins (if a custom element is distributed via slot to a div element, that div element is not going to notify the distributed child that the div is the composed parent).
I already do this fine with cooperating elements. For example, this works:
In that example, the
inside
x-foo element can observe its slot, and wheninside
sees that an x-foo element is distributed to it (theoutside
x-foo element in this case), theinside
x-foo can notify theoutside
x-foo that theinside
x-foo is theoutside
x-foo's composed parent (in the conceptual composed tree).However, here is a use case where that isn't possible when
inside
is not a cooperating custom element. Suppose thatx-foo
is supposed to render custom graphics (with canvas for example) and the result should be based on the composed parent's size (this is something that CSS rendering already already does, CSS can render features of an element based on the element's composed parent's size, but now we're trying to implement our own rendering system).Suppose we now have this example:
In that example the
x-foo
will be distributed to the div element. However, there's no way for the x-foo to know that it was distributed, or to which node it was distributed to, therefore the x-foo can not know what its composed parent is, and can therefore not know what the composed parent size is for its canvas rendering.This is solvable if we do something hacky: we could make a loop (f.e. using
requestAnimationFrame
) in the x-foo implementation to continually check what the composed parent is, but this is obviously not ideal (it needlessly uses device battery, for example). Each time we poll, we can checkoutside.assignedSlot. ... .asignedSlot.parentElement
. There's just not way to know whenoutside.assignedSlot
has changed, therefore also no way to check foroutside.assignedSlot. ... .assignedSlot
to find the final composed parent, apart from polling or other hacks.With some imagination, another complicated way of doing this could be to monkey patch
attachShadow
, and useMutationObserver
in all trees to detect slots and notify thex-foo
of its composed parent (or just the parent size).The text was updated successfully, but these errors were encountered: