Skip to content

Commit 1d2d033

Browse files
committed
move tag() method to DocumentComponent
1 parent b3c0004 commit 1d2d033

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

src/DocumentComponent.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,16 @@ export class DocumentComponent extends NodeComponent<DocumentFragment> {
2323
public constructor(html?: string) {
2424
super(html ? document.createRange().createContextualFragment(html) : document.createDocumentFragment());
2525
}
26+
27+
/**
28+
* Template literal tag function that accepts HTML code with components in a
29+
* string literal
30+
*/
31+
public static tag(strings: TemplateStringsArray, ...components: NodeComponent<any>[]): DocumentComponent {
32+
const idPrefix = "tag-" + crypto.randomUUID() + "-";
33+
const doc = new DocumentComponent(strings.reduce((acc, str, index) => acc += `${str}${index < components.length ? `<slot name="${idPrefix}${index + 1}"></slot>` : ""}`, ""));
34+
for (const [index, component] of components.entries())
35+
component.slot(idPrefix + index, doc.node);
36+
return doc;
37+
}
2638
}

src/ElementComponent.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,6 @@ export abstract class ElementComponent<T extends Element> extends NodeComponent<
5050
super(element);
5151
}
5252

53-
/**
54-
* Template literal tag function that accepts HTML code with components in a
55-
* string literal and returns a {@link DocumentComponent}
56-
*/
57-
public static tag(strings: TemplateStringsArray, ...components: NodeComponent<any>[]): DocumentComponent {
58-
const idPrefix = "tag-" + crypto.randomUUID() + "-";
59-
const doc = new DocumentComponent(strings.reduce((acc, str, index) => acc += `${str}${index < components.length ? `<slot name="${idPrefix}${index + 1}"></slot>` : ""}`, ""));
60-
for (const [index, component] of components.entries())
61-
component.slot(idPrefix + index, doc.node);
62-
return doc;
63-
}
64-
6553
/**
6654
* Insert component before the first child
6755
*/
@@ -153,7 +141,7 @@ export abstract class ElementComponent<T extends Element> extends NodeComponent<
153141
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals Template literals (Template strings) - MDN
154142
*/
155143
public html(strings: TemplateStringsArray, ...components: NodeComponent<any>[]): this {
156-
return this.append(ElementComponent.tag(strings, ...components));
144+
return this.append(DocumentComponent.tag(strings, ...components));
157145
}
158146

159147
/**

0 commit comments

Comments
 (0)