I have a custom element, my-el
, which in its DOM template (a shadow root) contains a slot
element. In a web page, I use my-el
by placing additional elements in between the tags <my-el>
</my-el>
as shown below.
<my-el>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</my-el>
I intend to measure the div
s' dimensions placed in my-el
as they are in the distributed tree, as soon as (i.e. the first time) they are rendered on the page. According to the spec:
To assign slotables, for a slot slot with an optional suppress signaling flag (unset unless stated otherwise), run these steps:
Let slotables be the result of finding slotables for slot.
If suppress signaling flag is unset, and slotables and slot’s assigned nodes are not identical, then run signal a slot change for slot.
...
If I have understood correctly, the first distribution of slot slottables does not fire a slotchange
event since the suppress signaling flag is set when the slot has not performed an initial distribution, further stated in
Shadow DOM v1: Self-Contained Web Components. This means that I cannot make an initial measurement of the children elements in the callback of slotchange
. Further, the following approach does not guarantee that at that point the children will be rendered:
connectedCallback() {
super.connectedCallback();
requestAnimationFrame(() => {
// Measure chilren
});
}
Question
How can I trigger my measurements upon the first time they are rendered after being distributed in the slot
in my-el
?