diff options
author | Hayleigh Thompson <me@hayleigh.dev> | 2024-06-19 08:56:54 +0100 |
---|---|---|
committer | Hayleigh Thompson <me@hayleigh.dev> | 2024-06-19 08:56:54 +0100 |
commit | 41b48e53bb100b1496cbdd53c82f6c7f556a81f4 (patch) | |
tree | ba35b23e9634e544d323c1ca1ec280a712c11973 /src/server-component.mjs | |
parent | a924297b13039e752c6bbd12d9a8ee429a2d4e63 (diff) | |
download | lustre-41b48e53bb100b1496cbdd53c82f6c7f556a81f4.tar.gz lustre-41b48e53bb100b1496cbdd53c82f6c7f556a81f4.zip |
:recycle: Adopt component styles by copying page <link> and <style> tags.
Diffstat (limited to 'src/server-component.mjs')
-rw-r--r-- | src/server-component.mjs | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/src/server-component.mjs b/src/server-component.mjs index d4cb4a9..855ff9d 100644 --- a/src/server-component.mjs +++ b/src/server-component.mjs @@ -19,6 +19,7 @@ export class LustreServerComponent extends HTMLElement { #root = null; #socket = null; #shadow = null; + #stylesOffset = 0; constructor() { super(); @@ -52,11 +53,13 @@ export class LustreServerComponent extends HTMLElement { for (const link of document.querySelectorAll("link")) { if (link.rel === "stylesheet") { this.#shadow.appendChild(link.cloneNode(true)); + this.#stylesOffset++; } } for (const style of document.querySelectorAll("style")) { this.#shadow.appendChild(style.cloneNode(true)); + this.#stylesOffset++; } this.#root = document.createElement("div"); @@ -152,16 +155,27 @@ export class LustreServerComponent extends HTMLElement { morph(vdom) { this.#root = morph(this.#root, vdom, (handler) => (event) => { + const data = JSON.parse(this.getAttribute("data-lustre-data") || "{}"); const msg = handler(event); + + msg.data = merge(data, msg.data); + this.#socket?.send(JSON.stringify([Constants.event, msg.tag, msg.data])); }); } diff([diff]) { - this.#root = patch(this.#root, diff, (handler) => (event) => { - const msg = handler(event); - this.#socket?.send(JSON.stringify([Constants.event, msg.tag, msg.data])); - }); + this.#root = patch( + this.#root, + diff, + (handler) => (event) => { + const msg = handler(event); + this.#socket?.send( + JSON.stringify([Constants.event, msg.tag, msg.data]), + ); + }, + this.#stylesOffset, + ); } emit([event, data]) { @@ -182,3 +196,15 @@ export class LustreServerComponent extends HTMLElement { } window.customElements.define("lustre-server-component", LustreServerComponent); + +// UTILS ----------------------------------------------------------------------- + +function merge(target, source) { + for (const key in source) { + if (source[key] instanceof Object) + Object.assign(source[key], merge(target[key], source[key])); + } + + Object.assign(target || {}, source); + return target; +} |