-
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
[idea] slottedCallback for Custom Elements #527
Comments
This callback for Custom Elements could be a possible way to explain number 3 of #184 (comment), if that route is taken: the iframe element simply detects that it has been distributed using |
The
Any other cases? |
Another possibility could be to add a customeElements.define('motor-scene',
class extends HTMLElement {
constructor() {
this.addEventListener('assigned', e => this.doSomething())
}
doSomething() {}
}
) but, in that case we might as well move the customElements.define('motor-scene',
class extends HTMLElement {
createdCallback() {
this.addEventListener('connected', e => this.doSomethingElse())
this.addEventListener('disconnected', e => this.cleanUp())
this.addEventListener('assigned', e => this.doSomething())
}
doSomething() {...}
doSomethingElse() {...}
cleanUp() {...}
}
) I think just allowing a method to be defined is the simplest way for class-definition time ergonomics: class MotorScene extends HTMLElement {
constructor() {...}
connectedCallback() {...}
disconnectedCallback() {...}
assignedCallback() {...}
}
customElements.define('motor-scene', MotorScene) But including the document.querySelector('#some-el').addEventListener('assigned', e => {...}) |
cc @domenic thoughts? |
Speaking of events, it seems that a framework can asynchronously map the "flat tree" if the end user of the framework places custom elements from that framework at the root of the app and at a leaf positions, and finally triggering events, at which point I wanted to point out that I think it's a good idea. The reason I need this (or something similar) is to guarantee synchronous discovery (or lack of discovery, which is what I really want) of the flat tree between two shadow layers when my library's custom elements are used on both sides of the shadow boundary. With strictly events, as it currently stands, this can only be achieved with event polling (we must poll because there's no way for an element to know it has been distributed because there's nothing like Suppose we do write an event polling mechanism. Then, But, if something like Quick question, which code can listen to |
@rniwa Shoot, even if The main problem I'm trying to solve is so that my
|
^ That last paragraph is the most important. That's my real problem, and I simply don't see a well-defined way of achieving this. @domenic @hayatoito @rniwa @treshugart |
My opinion did not change from: #504 (comment) BTW, can we merge this issue to #504? It looks the similar discussion is happening on both sides. |
Let me close this in favor of #504. |
To align more with v1 terminology, I renamed from |
There's currently no way for an HTMLElement to know when it gets distributed into a content or slot elements. In v1 API, there is a new
slotchange
event, but this is only useful to the parent of the slot element. In a closed tree, children can get references to the slot they are distributed into.So how can an element know when it is distributed?
Maybe there can be a
distributedCallback
that Custom Element classes can define. ThedistributedCallback
can receive a single argument:slot
. Theslot
argument is a reference to the slot where the element got distributed into. If the tree that the element was distributed into is closed, then the value of the slot argument can be null. Additionally,slotchange
handlers on the associated slot element should be executed beforedistributedCallback
s.For example:
If it is a guarantee that
slotchange
event handlers on the receiving slot element fire beforedistributedCallback
of the elements being distributed, then we can do some nifty things. For example, it would solve the problem I have in #504 (comment).In my specific case (and I can see this also applying to any other libraries made of Custom Elements like http://aframe.io), I need to detect when element of type A is distributed into another element of type A, and I want to initiate an error state whenever an element of type A is distributed into an element that is not of type A.
My specific problem: if parent element A contains a slot element, then A can watch for
slotchange
events in order to detect distributed child elements and can check that those elements are also of type A. The parent A element can then establish a connection with the child A element. However, I need to detect the case where a child element A is distributed into a slot whose parent is not of type A, and I want to throw an error. This is currently not possible because the child element A does not know that it has been distributed, and even if it did, it cannot see the parent of the slot element if the shadow tree is closed.If we can guarantee that
slotchange
fires before childdistributeCallbacks
, then in thedistributedCallback
of a child of type A that was distributed into any other element that is not of type A, the child A element will notice that a connection has not been made (parent would make the connection in aslotchange
handler), and the distributed child can then make the safe assumption that it has not been distributed into another element of type A, and an error can be thrown.The text was updated successfully, but these errors were encountered: