Skip to content

Commit f738a67

Browse files
committed
Add method to render component in the place of a slot
1 parent 1a81ec8 commit f738a67

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/NodeComponent.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,17 @@ export abstract class NodeComponent<T extends Node> {
7373
this.node.addEventListener(type, e => listener(e, this), c);
7474
return this;
7575
}
76+
77+
/**
78+
* Render this component in the place of a `<slot name="…"></slot>`.
79+
* If multiple slots with the same name are found, they will all be used.
80+
* @param slot The slot name
81+
* @param [parent] The parent element within to search for slots. Defaults to `document`
82+
*/
83+
public slot(slot: string, parent: ParentNode = document) {
84+
const slotNodes = parent.querySelectorAll(`slot[name="${slot}"]`);
85+
for (const slotNode of slotNodes)
86+
slotNode.replaceWith(this.node);
87+
return this;
88+
}
7689
}

0 commit comments

Comments
 (0)