-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
56 lines (52 loc) · 1.64 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import './dom-shim.js';
import { LitElementRenderer } from "@lit-labs/ssr/lib/lit-element-renderer.js";
import { getElementRenderer } from "@lit-labs/ssr/lib/element-renderer.js";
/**
* @typedef {import('swtl').CustomElementRenderer} CustomElementRenderer
* @typedef {import('swtl').HtmlResult} HtmlResult
* @typedef {import('swtl').HtmlValue} HtmlValue
* @typedef {import('swtl').Children} Children
* @typedef {import('swtl').Attribute} Attribute
*/
/**
* @param {{
* tag: string,
* children: Children,
* attributes: Attribute[],
* }} args
* @param {(children: Children) => AsyncGenerator<string, void, unknown>} renderChildren
*/
async function* render({ tag, children, attributes }, renderChildren) {
const renderInfo = {
elementRenderers: [LitElementRenderer],
customElementInstanceStack: [],
customElementHostStack: [],
deferHydration: false,
};
const renderer = getElementRenderer(renderInfo, tag);
attributes.forEach(({ name, value }) => {
if (name.startsWith('.')) {
renderer.setProperty(name.slice(1), value);
} else {
renderer.attributeChangedCallback(name, null, /** @type {string} */ (value));
}
});
renderer.connectedCallback();
yield `<${tag}>`;
yield `<template shadowroot="open" shadowrootmode="open">`;
// @ts-expect-error
yield* renderer.renderShadow(renderInfo);
yield `</template>`;
yield* renderChildren(children);
yield `</${tag}>`;
}
/** @type {CustomElementRenderer} */
export const litRenderer = {
name: 'lit',
match({tag}) {
const ctor = customElements.get(tag);
if (!ctor) return false;
return LitElementRenderer.matchesClass(ctor);
},
render
}