1
0
mirror of https://github.com/jaandrle/deka-dom-el synced 2025-07-01 12:22:15 +02:00

💥 simulateSlots

This commit is contained in:
2023-11-24 16:16:08 +01:00
parent 6a293d75a6
commit fb02635d24
20 changed files with 1263 additions and 1299 deletions

View File

@ -66,6 +66,37 @@ export function createElement(tag, attributes, ...addons){
scoped= 2;
return el;
}
/** @param {HTMLElement} element */
export function simulateSlots(element){
const _default= Symbol.for("default");
const slots= Array.from(element.querySelectorAll("slot"))
.reduce((out, curr)=> Reflect.set(out, curr.name || _default, curr) && out, {});
const has_d= Reflect.has(slots, _default);
element.append= new Proxy(element.append, {
apply(_1, _2, els){
if(!els.length) return element;
const d= document.createDocumentFragment();
for(const el of els){
if(!el || !el.slot){ if(has_d) d.appendChild(el); continue; }
const name= el.slot;
const slot= slots[name];
elementAttribute(el, "remove", "slot");
if(!slot) continue;
slot.replaceWith(el);
Reflect.deleteProperty(slots, name);
}
if(has_d){
slots[_default].replaceWith(d);
Reflect.deleteProperty(slots, _default);
}
Object.values(slots)
.forEach(slot=> slot.replaceWith(createElement().append(...Array.from(slot.childNodes))));
return element;
}
});
return element;
}
/**
* @param { { type: "component", name: string, host: "this" | "parentElement" } | { type: "reactive" | "later" } } attrs
* @param {boolean} [is_open=false]