aboutsummaryrefslogtreecommitdiff
path: root/src/client-component.ffi.mjs
diff options
context:
space:
mode:
authorHayleigh Thompson <me@hayleigh.dev>2024-06-13 23:53:23 +0100
committerHayleigh Thompson <me@hayleigh.dev>2024-06-13 23:53:23 +0100
commit4081a0941fcf55ce60866d4dcd60fc6fc99e7e8b (patch)
tree7a8178431430f22d8d57e2ad49403aeaa2c313e9 /src/client-component.ffi.mjs
parent72bf9e650f66253ea8994fc0036aa7dc2f4bd165 (diff)
downloadlustre-4081a0941fcf55ce60866d4dcd60fc6fc99e7e8b.tar.gz
lustre-4081a0941fcf55ce60866d4dcd60fc6fc99e7e8b.zip
:recycle: Correctly use shadow dom in custom elements.
Diffstat (limited to 'src/client-component.ffi.mjs')
-rw-r--r--src/client-component.ffi.mjs39
1 files changed, 34 insertions, 5 deletions
diff --git a/src/client-component.ffi.mjs b/src/client-component.ffi.mjs
index 0dfc303..ce8607b 100644
--- a/src/client-component.ffi.mjs
+++ b/src/client-component.ffi.mjs
@@ -14,10 +14,19 @@ export function register({ init, update, view, on_attribute_change }, name) {
return new Error(new ComponentAlreadyRegistered(name));
}
- window.customElements.define(
- name,
- makeComponent(init, update, view, on_attribute_change),
- );
+ const Component = makeComponent(init, update, view, on_attribute_change);
+
+ window.customElements.define(name, Component);
+
+ for (const el of document.querySelectorAll(name)) {
+ const replaced = new Component();
+
+ for (const attr of el.attributes) {
+ replaced.setAttribute(attr.name, attr.value);
+ }
+
+ el.replaceWith(replaced);
+ }
return new Ok(undefined);
}
@@ -26,6 +35,7 @@ function makeComponent(init, update, view, on_attribute_change) {
return class LustreClientComponent extends HTMLElement {
#root = document.createElement("div");
#application = null;
+ #shadow = null;
slotContent = [];
@@ -35,6 +45,8 @@ function makeComponent(init, update, view, on_attribute_change) {
constructor() {
super();
+ this.#shadow = this.attachShadow({ mode: "closed" });
+
on_attribute_change[0]?.forEach((decoder, name) => {
Object.defineProperty(this, name, {
get() {
@@ -60,6 +72,15 @@ function makeComponent(init, update, view, on_attribute_change) {
}
connectedCallback() {
+ const sheet = new CSSStyleSheet();
+
+ for (const { cssRules } of document.styleSheets) {
+ for (const rule of cssRules) {
+ sheet.insertRule(rule.cssText);
+ }
+ }
+
+ this.#shadow.adoptedStyleSheets = [sheet];
this.#application = new LustreClientApplication(
init(),
update,
@@ -67,7 +88,7 @@ function makeComponent(init, update, view, on_attribute_change) {
this.#root,
true,
);
- this.appendChild(this.#root);
+ this.#shadow.append(this.#root);
}
attributeChangedCallback(key, _, next) {
@@ -77,5 +98,13 @@ function makeComponent(init, update, view, on_attribute_change) {
disconnectedCallback() {
this.#application.send(new Shutdown());
}
+
+ get adoptedStyleSheets() {
+ return this.#shadow.adoptedStyleSheets;
+ }
+
+ set adoptedStyleSheets(value) {
+ this.#shadow.adoptedStyleSheets = value;
+ }
};
}