I'd like to, without using HTML, create a slot and slot an element into it. I tried the following and a few variations but no luck. What am I doing wrong? Why is slot.assignedNodes() returning an empty array after I assigned the element?
const element = document.createElement('div');
const slot = document.createElement('slot');
const slotted = document.createElement('span');
document.body.append(element);
element.attachShadow({ mode: 'open', slotAssignment: 'manual'});
element.shadowRoot.append(slot);
element.shadowRoot.append(slotted);
slot.assign(slotted);
console.log(slot.assignedNodes());
Your problem is here:
You are assigning
slottedto the same shadowDOM the<slot>is in.It should go to lightDOM with:
element.append(slotted)I rewrote your code so (I hope) its clearer
Without
slotAssignment:"manual"you do not needSLOT.assign(SPAN)because lightDOM content will automagically be slotted to the default unnamed<slot>With standard slot behavior