Re: [w3c/webcomponents] How can a custom element detect when it is transcluded into a shadow tree (slotchange)? (#504)

In my case, it's just that if a `motor-node` element gets attached or distributed into some other element that is not also a `motor-node`, then I'd like to throw an error or warning about this to the user. The problem I'm having is specifically when a `motor-node` is distributed into anything other than a `motor-node` I can't catch that case (because as you said, the child `motor-node` can not break encapsulation by reading the parent of the slot if the shadow tree is closed).

I'd really like to be able to inform the user about this case. AngularJS has nice errors that point to online documentation. I'd like to do this in that specific case, but I don't see how yet.

I'm thinking that the best solution (so far, in my mind) would be for Custom Elements to have a `distributedCallback` or `slottedCallback` (or similar) that is called when the custom element gets distributed into a slot (fired in the same tick as the slot parent's `slotchange` handlers). The single argument to the callback would be the `slot` element, and if the slot's shadow tree is closed then access to `slot.parentNode`/`slot.parentElement` would fail for encapsulation purposes. Additionally, it may be possibly arguable that the custom element who's `distributedCallback` is passed the slot cannot read the slot's children. But, I can probably imagine cases where a child would need info about it's sibling (even within a slot).

 I technically now *don't need for a distributed element to be able to view a slot's parents in order to make my flat-tree mirror, but now I need a distributed `motor-node` element to be able to know it was distributed and to then be able to throw an error in a deferred tick (because in the previous tick a parent `motor-node` element will have established a connection with the `motor-node` child that is inside the slot.

Does that make it a little more clear what my issue is?

---
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/w3c/webcomponents/issues/504#issuecomment-224118530

Received on Monday, 6 June 2016 23:24:07 UTC