Skip to content

Commit 8a02f5b

Browse files
author
infodusha
committed
feat: add appendStyle method
1 parent ca9b396 commit 8a02f5b

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

example/app-footer.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<template data-shadow>
1+
<template>
22
<footer>Built by infodusha. Inspired by Vika.</footer>
33
</template>
44

src/create-component.ts

+22-16
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function createComponent(
2323
"<template> is required"
2424
);
2525

26-
const styles = definedElement.querySelectorAll("style");
26+
const styles = [...definedElement.querySelectorAll("style"), ...globalStyles];
2727
const scripts = definedElement.querySelectorAll("script");
2828

2929
const usedAttributes = getUsedAttributes(template, ["data-attr", "data-if"]);
@@ -38,6 +38,12 @@ export function createComponent(
3838

3939
readonly #cleanupFns = new Set<CleanupFn>();
4040

41+
readonly #styleGroup = document.createElement("style");
42+
43+
get shadowRoot(): ShadowRoot {
44+
return returnIfDefined(super.shadowRoot);
45+
}
46+
4147
constructor() {
4248
super();
4349
const content = cloneNode(template.content);
@@ -78,9 +84,12 @@ export function createComponent(
7884
}
7985

8086
#attach(content: DocumentFragment): void {
81-
const shadowRoot = this.attachShadow({ mode: "open" });
82-
shadowRoot.appendChild(content);
83-
this.#setShadowStyles(shadowRoot);
87+
this.attachShadow({ mode: "open" });
88+
this.shadowRoot.appendChild(content);
89+
90+
for (const style of styles) {
91+
this.appendStyle(style);
92+
}
8493
}
8594

8695
#applyAttr(name: string, value: string | null): void {
@@ -107,16 +116,6 @@ export function createComponent(
107116
}
108117
}
109118

110-
#setShadowStyles(shadowRoot: ShadowRoot) {
111-
const group = document.createElement("style");
112-
group.setAttribute("data-define-html", "");
113-
for (const style of styles) {
114-
group.append(cloneNode(style));
115-
}
116-
group.append(...globalStyles.map((el) => cloneNode(el)));
117-
shadowRoot.append(group);
118-
}
119-
120119
#execScripts(): void {
121120
for (const script of scripts) {
122121
executeScript(script, href, this)
@@ -153,12 +152,19 @@ export function createComponent(
153152
}
154153
}
155154

155+
appendStyle(style: HTMLLinkElement | HTMLStyleElement) {
156+
if (this.#styleGroup.childElementCount === 0) {
157+
this.shadowRoot.append(this.#styleGroup);
158+
}
159+
this.#styleGroup.append(cloneNode(style));
160+
}
161+
156162
querySelector(selector: string): Element | null {
157-
return returnIfDefined(this.shadowRoot).querySelector(selector);
163+
return this.shadowRoot.querySelector(selector);
158164
}
159165

160166
querySelectorAll(selector: string): NodeListOf<Element> {
161-
return returnIfDefined(this.shadowRoot).querySelectorAll(selector);
167+
return this.shadowRoot.querySelectorAll(selector);
162168
}
163169
}
164170

0 commit comments

Comments
 (0)